import {z} from 'zod' export type GetApiApiChartsSpotifyArtistsArgs = {date?: string | undefined, interval: 'daily' | 'weekly' | 'monthly', type: 'monthly_listeners' | 'popularity' | 'followers' | 'playlist_count' | 'playlist_reach', latest?: boolean | undefined, offset?: number | undefined}; export type GetApiApiChartsSpotifyArtistsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiChartsSpotifyArtistsResponse = z.object({obj: z.object({length: z.number().int().gte(0), data: z.array(z.object({id: z.number().int().nullable().optional(), name: z.string().nullable().optional(), image_url: z.string().nullable().optional(), isni: z.string().nullable().optional(), code2: z.string().nullable().optional(), hometown_city: z.string().nullable().optional(), current_city: z.string().nullable().optional(), sp_followers: z.number().int().gte(0).nullable().optional(), sp_popularity: z.number().int().nullable().optional(), sp_monthly_listeners: z.number().int().gte(0).nullable().optional(), deezer_fans: z.number().int().gte(0).nullable().optional(), tags: z.array(z.unknown()).nullable().optional(), spotify_artist_ids: z.array(z.union([z.number().int(), z.string()]).nullable()).nullable().optional(), itunes_artist_ids: z.array(z.union([z.number().int(), z.string()]).nullable()).nullable().optional(), deezer_artist_ids: z.array(z.union([z.number().int(), z.string()]).nullable()).nullable().optional(), amazon_artist_ids: z.array(z.union([z.number().int(), z.string()]).nullable()).nullable().optional(), cm_artist_rank: z.number().int().nullable().optional(), cm_artist_score: z.number().nullable().optional(), timestp: z.string().nullable().optional(), monthly_playlist_reach: z.number().int().gte(0).nullable().optional(), rank: z.number().int().nullable().optional(), change: z.number().int().nullable().optional(), streak: z.number().int().nullable().optional(), peak_date: z.string().nullable().optional(), peak_rank: z.number().int().nullable().optional(), time_on_chart: z.union([z.number(), z.string()]).nullable().optional(), rankStats: z.array(z.object({date: z.string().nullable().optional(), rank: z.union([z.number(), z.string()]).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 Spotify Artist Charts * * GET /api/charts/spotify/artists */ export const getApiApiChartsSpotifyArtists = async (args: GetApiApiChartsSpotifyArtistsArgs, options?: GetApiApiChartsSpotifyArtistsOptions) => { 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.interval != null) query.append("interval", String(args.interval)) if (args.type != null) query.append("type", String(args.type)) if (args.latest != null) query.append("latest", String(args.latest)) if (args.offset != null) query.append("offset", String(args.offset)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/charts/spotify/artists${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(getApiApiChartsSpotifyArtistsResponse, await res.json()) };