import {z} from 'zod' export type CreateApiRunwayFeedArgs = {'X-Runway-Version'?: string | undefined, body: {task_id: string}}; export type CreateApiRunwayFeedOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiRunwayFeedResponse = z.object({code: z.number().int(), msg: z.string(), data: z.object({task_id: z.string(), state: z.string().nullable().optional(), status: z.string().nullable().optional(), prompt: z.string().nullable().optional(), prompt_en: z.string().nullable().optional(), video_url: z.string().nullable().optional(), raw_video_url: z.string().nullable().optional(), poster: z.string().nullable().optional(), last_frame: z.string().nullable().optional(), msg: z.string().nullable().optional(), point: z.string().nullable().optional(), refund: z.string().nullable().optional(), create_time: z.string().nullable().optional(), update_time: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())), exec_time: z.number(), ip: 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 }; /** * Feed Task Query API * * POST /runway/feed */ export const createApiRunwayFeed = async (args: CreateApiRunwayFeedArgs, options?: CreateApiRunwayFeedOptions) => { const {baseUrl = "https://api.deerapi.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Runway-Version'] != null) headers.set("X-Runway-Version", String(args['X-Runway-Version'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/runway/feed${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiRunwayFeedResponse, await res.json()) };