import {z} from 'zod' export type DubbingProInput = {$schema?: string | undefined, source_language?: string | undefined, target_language: string, video_url: string, voice_cloning_strength?: number | undefined}; export type CreateApiV1DubbingProArgs = {'X-Veed-Store-IO'?: ('0' | '1') | undefined, 'X-Veed-Media-Expiration-Seconds'?: number | undefined, body: DubbingProInput}; export type CreateApiV1DubbingProOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jobErrorDetail = z.object({field: z.string().optional(), message: z.string(), type: z.string()}); export const jobErrorDubbingErrorCode = z.object({code: z.enum(["input_validation", "content_moderation", "invalid_file", "audio_too_long", "transload_failed", "generation_failed", "timeout", "insufficient_credits"]), details: z.array(jobErrorDetail).nullable().optional(), message: z.string()}); export const file = z.object({content_type: z.string().optional(), file_name: z.string().optional(), file_size: z.number().int().optional(), url: z.string()}); export const dubbing = z.object({video: file}); export const dubbingJob = z.object({credits_charged: z.number().int().optional(), credits_estimated: z.number().int().optional(), error: jobErrorDubbingErrorCode.optional(), job_id: z.string(), result: dubbing.optional(), status: z.enum(["PROCESSING", "COMPLETED", "FAILED", "CANCELLED"])}); export const resourceDubbingJob = z.object({$schema: z.string().optional(), data: dubbingJob}); 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 }; /** * Submit a dubbing pro job * * POST /v1/dubbing-pro */ export const createApiV1DubbingPro = async (args: CreateApiV1DubbingProArgs, options?: CreateApiV1DubbingProOptions) => { const {baseUrl = "https://api.veed.io", 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-Veed-Store-IO'] != null) headers.set("X-Veed-Store-IO", String(args['X-Veed-Store-IO'])) if (args['X-Veed-Media-Expiration-Seconds'] != null) headers.set("X-Veed-Media-Expiration-Seconds", String(args['X-Veed-Media-Expiration-Seconds'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dubbing-pro${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(resourceDubbingJob, await res.json()) };