import {z} from 'zod' export type UpsertVideoTranslationsApiRequest = {targetLanguages: Array, translateScriptOnly?: boolean | undefined}; export type UpdateApiV2TranslationsRootVideoIdArgs = {root_video_id: string, body: UpsertVideoTranslationsApiRequest}; export type UpdateApiV2TranslationsRootVideoIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const upsertVideoTranslationsRunningWorkflowApiItem = z.object({language: z.string(), status: z.literal("in_progress").optional(), step: z.literal("translation"), startedByRequest: z.boolean().optional()}); export const upsertVideoTranslationsSuccessApiResponse = z.object({id: z.string(), translations: z.array(upsertVideoTranslationsRunningWorkflowApiItem)}); 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 }; /** * Start/Update a video translation * * PUT /v2/translations/{root_video_id} */ export const updateApiV2TranslationsRootVideoId = async (args: UpdateApiV2TranslationsRootVideoIdArgs, options?: UpdateApiV2TranslationsRootVideoIdOptions) => { const {baseUrl = "https://api.synthesia.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/translations/${encodeURIComponent(String(args.root_video_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(upsertVideoTranslationsSuccessApiResponse, await res.json()) };