| 1 | import { createZodDto } from '@yikart/common' |
| 2 | import { z } from 'zod' |
| 3 | import { createPlatformConfigSchema } from '../platforms.config' |
| 4 | import { AuthType, PlatformStatus } from '../platforms.interface' |
| 5 | |
| 6 | const douyinMiniAppConfigSchema = z.object({ |
| 7 | clientId: z.string().min(1), |
| 8 | clientSecret: z.string().min(1), |
| 9 | sandbox: z.boolean().default(false), |
| 10 | }) |
| 11 | |
| 12 | export interface DouyinMiniAppConfigValue { |
| 13 | clientId: string |
| 14 | clientSecret: string |
| 15 | sandbox: boolean |
| 16 | } |
| 17 | |
| 18 | export interface DouyinConfigValue { |
| 19 | status: PlatformStatus.Available |
| 20 | clientId: string |
| 21 | clientSecret: string |
| 22 | redirectUri: string |
| 23 | logoUrl: string |
| 24 | authType: AuthType.OAuth2 | AuthType.QrCode |
| 25 | scopes: string[] |
| 26 | miniApp?: DouyinMiniAppConfigValue |
| 27 | } |
| 28 | |
| 29 | const douyinBaseConfigSchema = { |
| 30 | status: z.literal(PlatformStatus.Available), |
| 31 | clientId: z.string(), |
| 32 | clientSecret: z.string(), |
| 33 | redirectUri: z.string(), |
| 34 | logoUrl: z.url(), |
| 35 | scopes: z.array(z.string()).default([]), |
| 36 | } |
| 37 | |
| 38 | const douyinAvailableConfigSchema = z.discriminatedUnion('authType', [ |
| 39 | z.object({ |
| 40 | ...douyinBaseConfigSchema, |
| 41 | authType: z.literal(AuthType.OAuth2), |
| 42 | miniApp: douyinMiniAppConfigSchema.optional(), |
| 43 | }), |
| 44 | z.object({ |
| 45 | ...douyinBaseConfigSchema, |
| 46 | authType: z.literal(AuthType.QrCode), |
| 47 | miniApp: douyinMiniAppConfigSchema, |
| 48 | }), |
| 49 | ]) |
| 50 | |
| 51 | export const douyinConfigSchema = createPlatformConfigSchema(douyinAvailableConfigSchema) |
| 52 | |
| 53 | export type DouyinConfigInput = z.input<typeof douyinAvailableConfigSchema> |
| 54 | |
| 55 | export class DouyinConfig extends createZodDto<DouyinConfigValue, DouyinConfigInput>(douyinAvailableConfigSchema) {} |
| 56 |