| 1 | import { z } from 'zod' |
| 2 | import { PlatformStatus } from './platforms.interface' |
| 3 | |
| 4 | export function createPlatformConfigSchema<T extends z.ZodTypeAny>( |
| 5 | availableSchema: T, |
| 6 | placeholderShape: z.ZodRawShape = {}, |
| 7 | ) { |
| 8 | const schema = z.union([ |
| 9 | z.object({ |
| 10 | ...placeholderShape, |
| 11 | status: z.literal(PlatformStatus.Hidden), |
| 12 | logoUrl: z.string().default(''), |
| 13 | }), |
| 14 | z.object({ |
| 15 | ...placeholderShape, |
| 16 | status: z.literal(PlatformStatus.Unavailable), |
| 17 | logoUrl: z.url(), |
| 18 | }), |
| 19 | z.object({ |
| 20 | ...placeholderShape, |
| 21 | status: z.literal(PlatformStatus.ComingSoon), |
| 22 | logoUrl: z.url(), |
| 23 | }), |
| 24 | availableSchema, |
| 25 | ]) |
| 26 | |
| 27 | return schema as unknown as T |
| 28 | } |
| 29 | |
| 30 | export interface PlatformConfigWithStatus { |
| 31 | status: PlatformStatus |
| 32 | logoUrl?: string |
| 33 | } |
| 34 | |
| 35 | export interface AvailablePlatformConfig { |
| 36 | status: PlatformStatus.Available |
| 37 | logoUrl: string |
| 38 | } |
| 39 | |
| 40 | export interface PlaceholderPlatformConfig { |
| 41 | status: PlatformStatus.Unavailable | PlatformStatus.ComingSoon |
| 42 | logoUrl: string |
| 43 | } |
| 44 | |
| 45 | export interface VisiblePlatformConfig { |
| 46 | status: PlatformStatus.Available | PlatformStatus.Unavailable | PlatformStatus.ComingSoon |
| 47 | logoUrl: string |
| 48 | } |
| 49 | |
| 50 | export function isAvailablePlatformConfig<T extends PlatformConfigWithStatus>( |
| 51 | platformConfig: T | undefined, |
| 52 | ): platformConfig is T & AvailablePlatformConfig { |
| 53 | return platformConfig?.status === PlatformStatus.Available |
| 54 | } |
| 55 | |
| 56 | export function isPlaceholderPlatformConfig<T extends PlatformConfigWithStatus>( |
| 57 | platformConfig: T | undefined, |
| 58 | ): platformConfig is T & PlaceholderPlatformConfig { |
| 59 | return platformConfig?.status === PlatformStatus.Unavailable |
| 60 | || platformConfig?.status === PlatformStatus.ComingSoon |
| 61 | } |
| 62 | |
| 63 | export function isVisiblePlatformConfig<T extends PlatformConfigWithStatus>( |
| 64 | platformConfig: T | undefined, |
| 65 | ): platformConfig is T & VisiblePlatformConfig { |
| 66 | return isAvailablePlatformConfig(platformConfig) || isPlaceholderPlatformConfig(platformConfig) |
| 67 | } |
| 68 |