import {z} from 'zod' export type GetApiWs11MatcherSubtitleGetArgs = {q_track: string, q_artist: string, f_subtitle_length?: string | undefined, f_subtitle_length_max_deviation?: string | undefined, track_isrc?: string | undefined}; export type GetApiWs11MatcherSubtitleGetOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWs11MatcherSubtitleGetResponse = z.object({message: z.object({body: z.union([z.object({subtitle: z.object({lyrics_copyright: z.string().optional(), pixel_tracking_url: z.string().optional(), region_restriction: z.object({allowed: z.array(z.string()).optional(), blocked: z.array(z.unknown()).optional()}).optional(), script_tracking_url: z.string().optional(), subtitle_body: z.string().optional(), subtitle_id: z.number().optional(), subtitle_language: z.string().optional(), subtitle_language_description: z.string().optional(), subtitle_length: z.number().optional(), updated_time: z.string().optional()}).optional()}), z.array(z.unknown())]).optional(), header: z.object({execute_time: z.number().optional(), status_code: z.number().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 }; /** * matcher.subtitle.get * * GET /ws/1.1/matcher.subtitle.get */ export const getApiWs11MatcherSubtitleGet = async (args: GetApiWs11MatcherSubtitleGetArgs, options?: GetApiWs11MatcherSubtitleGetOptions) => { const {baseUrl = "https://api.musixmatch.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q_track != null) query.append("q_track", String(args.q_track)) if (args.q_artist != null) query.append("q_artist", String(args.q_artist)) if (args.f_subtitle_length != null) query.append("f_subtitle_length", String(args.f_subtitle_length)) if (args.f_subtitle_length_max_deviation != null) query.append("f_subtitle_length_max_deviation", String(args.f_subtitle_length_max_deviation)) if (args.track_isrc != null) query.append("track_isrc", String(args.track_isrc)) if (apiKey !== undefined) query.set("apikey", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/ws/1.1/matcher.subtitle.get${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiWs11MatcherSubtitleGetResponse, await res.json()) };