import {z} from 'zod' export type CreateApiRunwayProGenerateArgs = {'X-Runway-Version'?: string | undefined, body: {callback_url: string, ratio: string, prompt: string, style?: string | undefined, model: string, options?: ({seconds?: number | undefined, motion_vector?: ({x?: number | undefined, y?: number | undefined, z?: number | undefined, r?: number | undefined, bg_x_pan?: number | undefined, bg_y_pan?: number | undefined}) | undefined, image_as_end_frame?: boolean | undefined, flip?: boolean | undefined}) | undefined, image: string, last_image?: string | undefined}}; export type CreateApiRunwayProGenerateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiRunwayProGenerateResponse = 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()}); 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 }; /** * Generate with Reference Image API * * POST /runway/pro/generate */ export const createApiRunwayProGenerate = async (args: CreateApiRunwayProGenerateArgs, options?: CreateApiRunwayProGenerateOptions) => { 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/pro/generate${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(createApiRunwayProGenerateResponse, await res.json()) };