import {z} from 'zod' export type GetApiApiV2ChannelsPlatformsPlatformPublishOptionsArgs = {platform: 'bilibili' | 'youtube' | 'pinterest' | 'twitter' | 'tiktok' | 'facebook' | 'instagram' | 'threads' | 'linkedin' | 'xhs' | 'wxSph' | 'douyin' | 'KWAI' | 'wxGzh'}; export type GetApiApiV2ChannelsPlatformsPlatformPublishOptionsOptions = {baseUrl?: string | undefined, apikeyHeaderXApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV2ChannelsPlatformsPlatformPublishOptionsResponse = z.object({data: z.array(z.object({field: z.string().optional(), label: z.string().optional(), description: z.string().optional(), valueType: z.enum(["list", "tree"]).optional(), requiresAccount: z.boolean().optional(), filterSchema: z.record(z.string(), z.unknown()).optional(), createSchema: z.record(z.string(), z.unknown()).optional()})).optional(), code: z.number().int().optional(), message: z.string().optional(), requestId: z.string().optional(), timestamp: z.number().int().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * 平台发布选项 * * GET /api/v2/channels/platforms/{platform}/publish-options */ export const getApiApiV2ChannelsPlatformsPlatformPublishOptions = async (args: GetApiApiV2ChannelsPlatformsPlatformPublishOptionsArgs, options?: GetApiApiV2ChannelsPlatformsPlatformPublishOptionsOptions) => { const {baseUrl = "https://aitoearn.cn", fetch: fetchFn = fetch, apikeyHeaderXApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apikeyHeaderXApiKey !== undefined) headers.set("X-Api-Key", apikeyHeaderXApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/channels/platforms/${encodeURIComponent(String(args.platform))}/publish-options${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV2ChannelsPlatformsPlatformPublishOptionsResponse, await res.json()) };