import {z} from 'zod' export type GetApiV1DubbingResourceDubbingIdArgs = {dubbing_id: string, 'xi-api-key'?: (string | null) | undefined}; export type GetApiV1DubbingResourceDubbingIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const dubbingMediaReference = z.object({src: z.string(), content_type: z.string(), bucket_name: z.string(), random_path_slug: z.string(), duration_secs: z.number(), is_audio: z.boolean(), url: z.string()}); export const speakerTrack = z.object({id: z.string(), media_ref: dubbingMediaReference, speaker_name: z.string(), voices: z.record(z.string(), z.string()), segments: z.array(z.string())}); export const segmentSubtitleFrame = z.object({start_time: z.number(), end_time: z.number(), lines: z.array(z.string())}); export const dubbedSegment = z.object({start_time: z.number(), end_time: z.number(), text: z.string().nullable(), subtitles: z.array(segmentSubtitleFrame), audio_stale: z.boolean(), media_ref: dubbingMediaReference.nullable()}); export const speakerSegment = z.object({id: z.string(), start_time: z.number(), end_time: z.number(), text: z.string(), subtitles: z.array(segmentSubtitleFrame), dubs: z.record(z.string(), dubbedSegment)}); export const renderType = z.enum(["mp4", "aac", "mp3", "wav", "aaf", "tracks_zip", "clips_zip", "zip"]); export const render = z.object({id: z.string(), version: z.number().int(), language: z.string().nullable(), type: renderType.nullable(), media_ref: dubbingMediaReference.nullable(), status: z.enum(["complete", "processing", "failed"])}); export const dubbingResource = z.object({id: z.string(), version: z.number().int(), source_language: z.string(), target_languages: z.array(z.string()), input: dubbingMediaReference, background: dubbingMediaReference.nullable(), foreground: dubbingMediaReference.nullable(), speaker_tracks: z.record(z.string(), speakerTrack), speaker_segments: z.record(z.string(), speakerSegment), renders: z.record(z.string(), render)}); 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 The Dubbing Resource For An Id. * * GET /v1/dubbing/resource/{dubbing_id} */ export const getApiV1DubbingResourceDubbingId = async (args: GetApiV1DubbingResourceDubbingIdArgs, options: GetApiV1DubbingResourceDubbingIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['xi-api-key'] != null) headers.set("xi-api-key", String(args['xi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dubbing/resource/${encodeURIComponent(String(args.dubbing_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(dubbingResource, await res.json()) };