import {z} from 'zod' export type SubmitLyricsRequest = {musixmatch_user_id: string, isrc: string, lyrics: string}; export type CreateApiDistributionV1LyricsArgs = {body: SubmitLyricsRequest}; export type CreateApiDistributionV1LyricsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const lyricsSubmitEnvelope = z.object({message: z.string().optional(), data: z.object({lyrics: z.object({lyrics_request_id: z.number().int().optional(), status: z.enum(["pending", "progress", "success", "failure"]).optional()}).optional()}).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 }; /** * Submit lyrics * * POST /distribution/v1/lyrics */ export const createApiDistributionV1Lyrics = async (args: CreateApiDistributionV1LyricsArgs, options?: CreateApiDistributionV1LyricsOptions) => { const {baseUrl = "https://api.musixmatch.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) query.set("apikey", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/distribution/v1/lyrics${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(lyricsSubmitEnvelope, await res.json()) };