import {z} from 'zod' export type GetApiV2DubbingDubbingProjectIdArgs = {dubbing_project_id: string, targetLanguages?: Array | undefined}; export type GetApiV2DubbingDubbingProjectIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getDubbingProjectVideosResponseUploading = z.object({id: z.string(), status: z.literal("uploading").optional()}); export const dubbingOutputLanguage = z.enum(["af", "sq", "am", "ar", "ar-DZ", "ar-BH", "ar-EG", "ar-IQ", "ar-JO", "ar-KW", "ar-LY", "ar-MA", "ar-QA", "ar-SA", "ar-SY", "ar-TN", "ar-AE", "ar-YE", "hy", "as", "az", "eu", "bn", "bs", "bg", "ca", "zh", "zh-CN", "zh-HK", "zh-TW", "hr", "cs", "da", "nl", "nl-BE", "nl-NL", "en", "en-AU", "en-CA", "en-GB", "en-HK", "en-IN", "en-IE", "en-KE", "en-MX", "en-NZ", "en-NG", "en-PH", "en-SG", "en-ZA", "en-TZ", "en-US", "et", "fil", "fi", "fr", "fr-BE", "fr-CA", "fr-FR", "fr-CH", "gl", "ka", "de", "de-AT", "de-DE", "de-CH", "el", "gu", "he", "hi", "hu", "is", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "ko", "lo", "lv", "lt", "mk", "ms", "ml", "mt", "mr", "mn", "ne", "no", "nb", "nn", "or", "pa", "ps", "fa", "pl", "pt", "pt-BR", "pt-PT", "ro", "ru", "sr", "si", "sk", "sl", "so", "es", "es-AR", "es-BO", "es-CL", "es-CO", "es-CR", "es-CU", "es-DO", "es-EC", "es-SV", "es-GQ", "es-GT", "es-HN", "es-LA", "es-MX", "es-NI", "es-PA", "es-PY", "es-PE", "es-PR", "es-ES", "es-US", "es-UY", "es-VE", "su", "sw", "sv", "ta", "ta-IN", "ta-SG", "ta-LK", "te", "th", "tr", "uk", "ur", "uz", "vi", "cy", "zu"]); export const dubbedAssetInProgress = z.object({id: z.string(), language: dubbingOutputLanguage, status: z.literal("in_progress").optional()}); export const dubbedAssetThumbnail = z.object({image: z.string().nullable().optional(), gif: z.string().nullable().optional()}); export const dubbedAssetCaptions = z.object({srt: z.string().nullable().optional(), vtt: z.string().nullable().optional()}); export const dubbedAssetComplete = z.object({id: z.string(), title: z.string(), description: z.string(), language: dubbingOutputLanguage, status: z.literal("complete").optional(), createdAt: z.number().int().nullable().optional(), lastUpdatedAt: z.number().int().nullable().optional(), visibility: z.enum(["public", "private"]), download: z.string(), thumbnail: dubbedAssetThumbnail, captions: dubbedAssetCaptions}); export const publicApiImportedAndDubbedVideosErrorCode = z.enum(["moderation", "video_duration_limit_reached", "invalid_target_language", "processing_error"]); export const dubbedAssetError = z.object({id: z.string(), language: dubbingOutputLanguage, status: z.literal("error").optional(), errorCode: publicApiImportedAndDubbedVideosErrorCode}); export const getDubbingProjectVideosResponseInProgress = z.object({id: z.string(), status: z.literal("in_progress").optional(), dubbedAssets: z.array(z.union([dubbedAssetInProgress, dubbedAssetComplete, dubbedAssetError])).optional()}); export const getDubbingProjectVideosResponseComplete = z.object({id: z.string(), status: z.literal("complete").optional(), dubbedAssets: z.array(z.union([dubbedAssetInProgress, dubbedAssetComplete, dubbedAssetError])).optional()}); export const getDubbingProjectVideosResponseError = z.object({id: z.string(), status: z.literal("error").optional(), errorCode: publicApiImportedAndDubbedVideosErrorCode}); export const getApiV2DubbingDubbingProjectIdResponse = z.union([getDubbingProjectVideosResponseUploading, getDubbingProjectVideosResponseInProgress, getDubbingProjectVideosResponseComplete, getDubbingProjectVideosResponseError]); 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 dubbing project status and videos * * GET /v2/dubbing/{dubbing_project_id} */ export const getApiV2DubbingDubbingProjectId = async (args: GetApiV2DubbingDubbingProjectIdArgs, options?: GetApiV2DubbingDubbingProjectIdOptions) => { const {baseUrl = "https://api.synthesia.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.targetLanguages ?? []) query.append("targetLanguages", String(item)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/dubbing/${encodeURIComponent(String(args.dubbing_project_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(getApiV2DubbingDubbingProjectIdResponse, await res.json()) };