import {z} from 'zod' export type GetApiApiCityIdDomainTopArtistsArgs = {id: number, domain: 'spotify' | 'youtube' | 'shazam' | 'radio' | 'instagram'}; export type GetApiApiCityIdDomainTopArtistsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiCityIdDomainTopArtistsResponse = z.object({obj: z.array(z.object({count: z.union([z.number().int(), z.string()]).nullable().optional(), username: z.string().nullable().optional(), id: z.number().int().gte(0).lte(2147483647), 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(), verified: z.boolean().nullable().optional(), band: z.boolean().nullable().optional(), gender: z.string().nullable().optional(), sp_followers: z.number().int().nullable().optional(), sp_popularity: z.number().int().nullable().optional(), sp_monthly_listeners: z.number().int().nullable().optional(), deezer_fans: z.number().int().nullable().optional(), cm_artist_rank: z.number().int().nullable().optional(), cm_artist_score: z.number().nullable().optional(), primary_genre_smart: z.union([z.number().int(), z.string(), z.object({})]).nullable().optional(), tags: z.array(z.string()).nullable().optional(), spotify_artist_ids: z.array(z.string().nullable()).nullable().optional(), itunes_artist_ids: z.array(z.number().int().nullable()).nullable().optional(), deezer_artist_ids: z.array(z.string().nullable()).nullable().optional(), amazon_artist_ids: z.array(z.string().nullable()).nullable().optional(), latest_release: z.object({cm_album: z.number().int().nullable().optional(), album_name: z.string().nullable().optional(), release_date: z.string().nullable().optional(), upc: z.string().nullable().optional(), label: z.string().nullable().optional(), image_url: z.string().nullable().optional()}).nullable().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 }; /** * List Top Artists in a City * * GET /api/city/{id}/{domain}/top-artists */ export const getApiApiCityIdDomainTopArtists = async (args: GetApiApiCityIdDomainTopArtistsArgs, options?: GetApiApiCityIdDomainTopArtistsOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/city/${encodeURIComponent(String(args.id))}/${encodeURIComponent(String(args.domain))}/top-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(getApiApiCityIdDomainTopArtistsResponse, await res.json()) };