import {z} from 'zod' export type GetApiV1TasksTaskIdArgs = {task_id: string}; export type GetApiV1TasksTaskIdOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const taskStatus = z.enum(["PENDING", "QUEUING", "PROCESSING", "SUCCESS", "FAILED", "CANCELED"]); export const taskCategory = z.enum(["TEXT_TO_VIDEO", "IMAGE_TO_VIDEO", "VIDEO_TO_VIDEO", "TEMPLATE_TO_VIDEO", "TALKING_AVATAR"]); export const domoVideoOut = z.object({url: z.string().min(1).max(2083), width: z.number().int(), height: z.number().int()}); export const videoTaskOut = z.object({task_id: z.string(), id: z.string(), status: taskStatus, category: taskCategory, callback_url: z.string().nullable(), seconds: z.number().int(), output_videos: z.array(domoVideoOut), credits: z.number().int(), inputs: z.record(z.string(), z.unknown()), created_at: z.string().nullable()}); export const domoOutVideoTaskOut = z.object({code: z.number().int().optional(), data: videoTaskOut}); 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 Task * * GET /v1/tasks/{task_id} */ export const getApiV1TasksTaskId = async (args: GetApiV1TasksTaskIdArgs, options?: GetApiV1TasksTaskIdOptions) => { const {baseUrl = "https://api.domoai.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tasks/${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(domoOutVideoTaskOut, await res.json()) };