import {z} from 'zod' export type ElevenLabsVoiceSettings = {stability?: number | undefined, similarity_boost?: number | undefined, style?: number | undefined, use_speaker_boost?: boolean | undefined}; export type ElevenLabsTTSRequest = {text: string, model_id?: (string | null) | undefined, voice_settings?: (ElevenLabsVoiceSettings | null) | undefined}; export type CreateApi11labsV1TextToSpeechVoiceIdArgs = {voice_id: string, output_format?: (string | null) | undefined, body: ElevenLabsTTSRequest}; export type CreateApi11labsV1TextToSpeechVoiceIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApi11labsV1TextToSpeechVoiceIdResponse = z.unknown(); 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 }; /** * Text To Speech * * POST /11labs/v1/text-to-speech/{voice_id} */ export const createApi11labsV1TextToSpeechVoiceId = async (args: CreateApi11labsV1TextToSpeechVoiceIdArgs, options: CreateApi11labsV1TextToSpeechVoiceIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.output_format != null) query.append("output_format", String(args.output_format)) const url = `${baseUrl.replace(/\/+$/, '')}/11labs/v1/text-to-speech/${encodeURIComponent(String(args.voice_id))}${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(createApi11labsV1TextToSpeechVoiceIdResponse, await res.json()) };