import {z} from 'zod' export type GetApiKlingV1ActionAction2TaskIdArgs = {action: string, action2: string, task_id: string}; export type GetApiKlingV1ActionAction2TaskIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiKlingV1ActionAction2TaskIdResponse = z.object({code: z.number().int(), message: z.string(), request_id: z.string(), data: z.object({task_id: z.string(), task_status: z.string(), created_at: z.number().int(), updated_at: z.number().int(), task_info: z.object({parent_video: z.object({id: z.string(), url: z.string(), duration: z.string()})}), task_result: z.object({videos: z.array(z.object({id: z.string().optional(), url: z.string().optional(), duration: z.string().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 }; /** * DeerAPI Kling Task Query * * GET /kling/v1/{action}/{action2}/{task_id} */ export const getApiKlingV1ActionAction2TaskId = async (args: GetApiKlingV1ActionAction2TaskIdArgs, options?: GetApiKlingV1ActionAction2TaskIdOptions) => { const {baseUrl = "https://api.deerapi.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/kling/v1/${encodeURIComponent(String(args.action))}/${encodeURIComponent(String(args.action2))}/${encodeURIComponent(String(args.task_id))}${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(getApiKlingV1ActionAction2TaskIdResponse, await res.json()) };