import {z} from 'zod' export type GetApiApiChartsPlatformCountriesArgs = {platform: 'airplay' | 'amazon' | 'applemusic' | 'deezer' | 'itunes' | 'shazam' | 'soundcloud' | 'spotify' | 'tiktok' | 'youtube', chart_type?: ('tracks' | 'artists' | 'tracks' | 'albums' | 'albums' | 'tracks' | 'videos' | 'tracks' | 'artists' | 'tracks' | 'trends' | 'videos' | 'shorts') | undefined, duration?: ('daily' | 'weekly' | 'daily' | 'weekly') | undefined, type?: ('popular_track' | 'new_track' | 'popular_album' | 'new_album' | 'top' | 'daily') | undefined}; export type GetApiApiChartsPlatformCountriesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiChartsPlatformCountriesResponse = z.object({obj: z.object({countries: z.union([z.array(z.union([z.string(), z.object({})])), z.object({}), 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 Chart Countries for Platform * * GET /api/charts/{platform}/countries */ export const getApiApiChartsPlatformCountries = async (args: GetApiApiChartsPlatformCountriesArgs, options?: GetApiApiChartsPlatformCountriesOptions) => { 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.chart_type != null) query.append("chart_type", String(args.chart_type)) if (args.duration != null) query.append("duration", String(args.duration)) if (args.type != null) query.append("type", String(args.type)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/charts/${encodeURIComponent(String(args.platform))}/countries${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(getApiApiChartsPlatformCountriesResponse, await res.json()) };