import {z} from 'zod' export type GetApiWs11ChartArtistsGetArgs = {country?: string | undefined, page?: string | undefined, page_size?: string | undefined, format?: string | undefined}; export type GetApiWs11ChartArtistsGetOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWs11ChartArtistsGetResponse = z.object({message: z.object({body: z.union([z.object({artist_list: z.array(z.object({artist: z.object({artist_alias_list: z.array(z.object({artist_alias: z.string().optional()})).optional(), artist_comment: z.string().optional(), artist_country: z.string().optional(), artist_credits: z.object({artist_list: z.array(z.unknown()).optional()}).optional(), artist_id: z.number().optional(), artist_name: z.string().optional(), artist_name_translation_list: z.array(z.unknown()).optional(), artist_twitter_url: z.string().optional(), begin_date: z.string().optional(), begin_date_year: z.string().optional(), end_date: z.string().optional(), end_date_year: z.string().optional(), restricted: z.number().optional(), updated_time: z.string().optional()}).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 }; /** * chart.artists.get * * GET /ws/1.1/chart.artists.get */ export const getApiWs11ChartArtistsGet = async (args: GetApiWs11ChartArtistsGetArgs, options?: GetApiWs11ChartArtistsGetOptions) => { 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.country != null) query.append("country", String(args.country)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.format != null) query.append("format", String(args.format)) if (apiKey !== undefined) query.set("apikey", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/ws/1.1/chart.artists.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(getApiWs11ChartArtistsGetResponse, await res.json()) };