import {z} from 'zod' export type ReplaceRequest = {music_id: string, sound_prompt: string, lyrics_text: string, instrumental?: boolean | undefined, starts?: number | undefined, ends?: number | undefined, mv?: ('FUZZ-2.0 Pro' | 'FUZZ-2.0' | 'FUZZ-2.0 Raw' | 'FUZZ-1.1 Pro' | 'FUZZ-1.0 Pro' | 'FUZZ-1.0' | 'FUZZ-1.1' | 'FUZZ-0.8' | 'Lyria 3 preview') | undefined, title?: string | undefined, cover_art_url?: string | undefined, seed?: string | undefined, lyrics_strength?: number | undefined, sound_strength?: number | undefined, weirdness?: number | undefined, is_storage?: boolean | undefined, hook_url?: string | undefined}; export type CreateApiProducerV1ReplaceArgs = {body: ReplaceRequest}; export type CreateApiProducerV1ReplaceOptions = {baseUrl?: string | undefined, CustomApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiProducerV1ReplaceResponse = z.object({status: z.string(), message: z.string(), data: z.object({})}); 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 }; /** * Replace some segments of the generated audio * * POST /producer/v1/replace */ export const createApiProducerV1Replace = async (args: CreateApiProducerV1ReplaceArgs, options?: CreateApiProducerV1ReplaceOptions) => { const {baseUrl = "https://api.ttapi.io", fetch: fetchFn = fetch, CustomApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (CustomApiKey !== undefined) headers.set("TT-API-KEY", CustomApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/producer/v1/replace${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(createApiProducerV1ReplaceResponse, await res.json()) };