import {z} from 'zod' export type GetApiApiV2ChannelsWorksLinkInfoArgs = {platform: 'douyin' | 'xhs' | 'wxSph' | 'KWAI' | 'youtube' | 'wxGzh' | 'bilibili' | 'twitter' | 'tiktok' | 'facebook' | 'instagram' | 'threads' | 'pinterest' | 'linkedin' | 'google_business', link: string, accountId?: string | undefined}; export type GetApiApiV2ChannelsWorksLinkInfoOptions = {baseUrl?: string | undefined, apikeyHeaderXApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV2ChannelsWorksLinkInfoResponse = z.object({data: z.object({platform: z.string().optional(), work: z.object({id: z.string().optional(), url: z.string().optional(), mediaType: z.string().optional()}).optional(), snapshots: z.array(z.record(z.string(), z.unknown())).optional(), extra: z.object({dataId: z.string().optional(), uniqueId: z.string().optional(), type: z.string().optional(), videoType: z.string().optional(), resolvedUrl: z.string().optional()}).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/works/link-info */ export const getApiApiV2ChannelsWorksLinkInfo = async (args: GetApiApiV2ChannelsWorksLinkInfoArgs, options?: GetApiApiV2ChannelsWorksLinkInfoOptions) => { const {baseUrl = "https://aitoearn.cn", fetch: fetchFn = fetch, apikeyHeaderXApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.platform != null) query.append("platform", String(args.platform)) if (args.link != null) query.append("link", String(args.link)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (apikeyHeaderXApiKey !== undefined) headers.set("X-Api-Key", apikeyHeaderXApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/channels/works/link-info${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(getApiApiV2ChannelsWorksLinkInfoResponse, await res.json()) };