import {z} from 'zod' export type GetApiWs11ArtistAlbumsGetArgs = {artist_id: string, g_album_name?: string | undefined, s_release_date?: string | undefined, page?: string | undefined, page_size?: string | undefined}; export type GetApiWs11ArtistAlbumsGetOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWs11ArtistAlbumsGetResponse = z.object({message: z.object({body: z.union([z.object({album_list: z.array(z.object({album: z.object({album_id: z.number().optional(), album_name: z.string().optional(), album_release_date: z.string().optional(), artist_id: z.number().optional(), artist_name: z.string().optional(), restricted: z.number().optional(), updated_time: z.string().optional()}).optional()})).optional()}), z.array(z.unknown()), z.string()]).optional(), header: z.object({available: z.number().optional(), 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 }; /** * artist.albums.get * * GET /ws/1.1/artist.albums.get */ export const getApiWs11ArtistAlbumsGet = async (args: GetApiWs11ArtistAlbumsGetArgs, options?: GetApiWs11ArtistAlbumsGetOptions) => { 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.artist_id != null) query.append("artist_id", String(args.artist_id)) if (args.g_album_name != null) query.append("g_album_name", String(args.g_album_name)) if (args.s_release_date != null) query.append("s_release_date", String(args.s_release_date)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (apiKey !== undefined) query.set("apikey", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/ws/1.1/artist.albums.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(getApiWs11ArtistAlbumsGetResponse, await res.json()) };