import {z} from 'zod' export type Text2VideoModel = 't2v-2.4-faster' | 't2v-2.4-advanced'; export type Text2VideoStyle = 'japanese_anime' | 'realistic' | 'pixel' | 'cartoon_game' | 'flat_color_anime' | '90s_style'; export type AspectRatio = '16:9' | '9:16' | '1:1' | '4:3' | '3:4'; export type CreateText2VideoTaskIn = {callback_url?: (string | null) | undefined, prompt: string, model?: Text2VideoModel | undefined, style?: (Text2VideoStyle | null) | undefined, seconds: number, aspect_ratio?: AspectRatio | undefined}; export type CreateApiV1VideoText2videoArgs = {body: CreateText2VideoTaskIn}; export type CreateApiV1VideoText2videoOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const taskOut = z.object({task_id: z.string()}); export const domoOutTaskOut = z.object({code: z.number().int().optional(), data: taskOut}); 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 }; /** * Text To Video * * POST /v1/video/text2video */ export const createApiV1VideoText2video = async (args: CreateApiV1VideoText2videoArgs, options?: CreateApiV1VideoText2videoOptions) => { const {baseUrl = "https://api.domoai.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/video/text2video${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(domoOutTaskOut, await res.json()) };