import {z} from 'zod' export type GetApiWs11TrackSubtitleGetArgs = {commontrack_id?: string | undefined, track_id?: string | undefined, track_isrc?: string | undefined, track_spotify_id?: string | undefined, track_itunes_id?: string | undefined, subtitle_format?: string | undefined, f_subtitle_length?: string | undefined, f_subtitle_length_max_deviation?: string | undefined}; export type GetApiWs11TrackSubtitleGetOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWs11TrackSubtitleGetResponse = z.object({message: z.object({body: z.union([z.object({subtitle: z.object({lyrics_copyright: z.string().optional(), pixel_tracking_url: z.string().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()), z.object({})]).optional(), header: z.object({execute_time: z.number().optional(), instrumental: 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 }; /** * track.subtitle.get * * GET /ws/1.1/track.subtitle.get */ export const getApiWs11TrackSubtitleGet = async (args: GetApiWs11TrackSubtitleGetArgs, options?: GetApiWs11TrackSubtitleGetOptions) => { 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.commontrack_id != null) query.append("commontrack_id", String(args.commontrack_id)) if (args.track_id != null) query.append("track_id", String(args.track_id)) if (args.track_isrc != null) query.append("track_isrc", String(args.track_isrc)) if (args.track_spotify_id != null) query.append("track_spotify_id", String(args.track_spotify_id)) if (args.track_itunes_id != null) query.append("track_itunes_id", String(args.track_itunes_id)) if (args.subtitle_format != null) query.append("subtitle_format", String(args.subtitle_format)) 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 (apiKey !== undefined) query.set("apikey", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/ws/1.1/track.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(getApiWs11TrackSubtitleGetResponse, await res.json()) };