import {z} from 'zod' export type CreateApiSunoFetchArgs = {body: {ids: Array, action: string}}; export type CreateApiSunoFetchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiSunoFetchResponse = z.object({code: z.string(), message: z.string(), data: z.array(z.object({task_id: z.string().optional(), notify_hook: z.string().optional(), action: z.string().optional(), status: z.string().optional(), fail_reason: z.string().optional(), submit_time: z.number().int().optional(), start_time: z.number().int().optional(), finish_time: z.number().int().optional(), progress: z.string().optional(), data: z.array(z.object({id: z.string(), title: z.string(), status: z.string(), metadata: z.object({tags: z.string(), prompt: z.string(), duration: z.unknown(), error_type: z.unknown(), error_message: z.unknown(), audio_prompt_id: z.unknown(), gpt_description_prompt: z.string()}), audio_url: z.string(), image_url: z.string(), video_url: z.string(), model_name: z.string(), image_large_url: z.string(), major_model_version: z.string()})).optional()}))}); 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 }; /** * Query batch tasks * * POST /suno/fetch */ export const createApiSunoFetch = async (args: CreateApiSunoFetchArgs, options?: CreateApiSunoFetchOptions) => { const {baseUrl = "https://api.deerapi.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/suno/fetch${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(createApiSunoFetchResponse, await res.json()) };