import {z} from 'zod' export type GetApiApiChartsApplemusicAlbumsArgs = {date?: string | undefined, latest?: boolean | undefined, country_code: string, genre?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiApiChartsApplemusicAlbumsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiChartsApplemusicAlbumsResponse = z.object({obj: z.object({length: z.number().int().gte(0), data: z.array(z.object({rank: z.number().int().nullable().optional(), pre_rank: z.number().int().nullable().optional(), peak_rank: z.number().int().nullable().optional(), peak_date: z.string().nullable().optional(), rank_1d_change: z.number().int().nullable().optional(), rank_7d_change: z.number().int().nullable().optional(), chart_name: z.string().nullable().optional(), chart_type: z.string().nullable().optional(), code2: z.string().nullable().optional(), timestp: z.string().nullable().optional(), duration: z.string().nullable().optional(), id: z.number().int().nullable().optional(), name: z.string().nullable().optional(), image_url: z.string().nullable().optional(), upc: z.string().nullable().optional(), release_date: z.string().nullable().optional(), label: z.string().nullable().optional(), genre: z.string().nullable().optional(), added_at: z.string().nullable().optional(), country: z.string().nullable().optional(), itunes: z.number().int().nullable().optional(), itunes_album_id: z.union([z.string(), z.number(), z.array(z.unknown())]).nullable().optional(), cm_album: z.number().int().nullable().optional(), cm_artist: z.array(z.number().int().gte(0).lte(2147483647)).nullable().optional(), artist_names: z.array(z.string().nullable()).nullable().optional(), itunes_artist_ids: z.array(z.string().nullable()).nullable().optional(), itunes_artist_names: z.array(z.string().nullable()).nullable().optional(), storefronts: z.array(z.string().nullable()).nullable().optional(), velocity: z.number().nullable().optional(), time_on_chart: z.union([z.string(), z.number()]).nullable().optional(), rankStats: z.array(z.object({timestp: z.string().nullable().optional(), rank: z.number().int().nullable().optional()})).nullable().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 }; /** * Get Apple Music Album Charts * * GET /api/charts/applemusic/albums */ export const getApiApiChartsApplemusicAlbums = async (args: GetApiApiChartsApplemusicAlbumsArgs, options?: GetApiApiChartsApplemusicAlbumsOptions) => { const {baseUrl = "https://api.chartmetric.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) if (args.latest != null) query.append("latest", String(args.latest)) if (args.country_code != null) query.append("country_code", String(args.country_code)) if (args.genre != null) query.append("genre", String(args.genre)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/charts/applemusic/albums${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(getApiApiChartsApplemusicAlbumsResponse, await res.json()) };