import {z} from 'zod' export type LocalizeTargetLanguage = 'en' | 'de' | 'es' | 'fr' | 'ja' | 'pt' | 'zh' | 'hi' | 'it' | 'ko' | 'nl' | 'pl' | 'ru' | 'sv' | 'tr' | 'ar' | 'he' | 'ta' | 'te' | 'th'; export type Gender = 'male' | 'female'; export type LocalizeEnglishDialect = 'au' | 'in' | 'so' | 'uk' | 'us'; export type LocalizeSpanishDialect = 'mx' | 'pe'; export type LocalizePortugueseDialect = 'br' | 'eu'; export type LocalizeFrenchDialect = 'eu' | 'ca'; export type LocalizeDialect = LocalizeEnglishDialect | LocalizeSpanishDialect | LocalizePortugueseDialect | LocalizeFrenchDialect; export type LocalizeVoiceRequest = {voice_id: string, name: string, tagline?: string | undefined, description: string, language: LocalizeTargetLanguage, original_speaker_gender: Gender, dialect?: LocalizeDialect | undefined, access?: ({type: 'private' | 'public'}) | undefined}; export type CreateApiVoicesLocalizeArgs = {'Cartesia-Version': '2026-03-01', body: LocalizeVoiceRequest}; export type CreateApiVoicesLocalizeOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const voiceId = z.string(); export const voiceLanguage = z.string(); export const voiceMetadata = z.object({id: voiceId, access: z.object({type: z.enum(["private", "public"]), visibility: z.enum(["owner", "all"])}), name: z.string(), tagline: z.string(), description: z.string(), created_at: z.string(), language: voiceLanguage, is_public: z.boolean(), user_id: z.string()}); 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 }; /** * Localize Voice * * POST /voices/localize */ export const createApiVoicesLocalize = async (args: CreateApiVoicesLocalizeArgs, options?: CreateApiVoicesLocalizeOptions) => { const {baseUrl = "https://api.cartesia.ai", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Cartesia-Version'] != null) headers.set("Cartesia-Version", String(args['Cartesia-Version'])) if (APIKeyAuth !== undefined) headers.set('authorization', `Bearer ${APIKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/voices/localize${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(voiceMetadata, await res.json()) };