import {z} from 'zod' export type GetApiApiTrackIdPlatformStatsModeArgs = {platform: 'shazam' | 'spotify' | 'youtube' | 'tiktok' | 'genius' | 'soundcloud' | 'chartmetric' | 'line' | 'melon' | 'radio', id: number | string | number, mode: 'highest-playcounts' | 'most-history', since?: string | undefined, type?: ('total_spins' | 'listen_count' | 'artist_likes' | 'likes_melon' | 'streams' | 'popularity' | 'score' | 'rank' | 'posts' | 'views' | 'comments' | 'sound_posts' | 'sound_views' | 'sound_likes' | 'sound_comments' | string) | undefined, until?: string | undefined, latest?: boolean | undefined, interpolated?: boolean | undefined, isDomainId?: boolean | undefined, extrapolated?: ('before' | 'after' | 'both') | undefined}; export type GetApiApiTrackIdPlatformStatsModeOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiTrackIdPlatformStatsModeResponse = z.object({obj: z.array(z.object({domain: z.string(), track_domain_id: z.string().nullable().optional(), type: z.string().nullable().optional(), data: z.array(z.object({value: z.union([z.number(), z.string()]).nullable().optional(), timestp: z.string().nullable().optional(), daily_diff: z.number().nullable().optional(), interpolated: z.boolean().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 Track Stats by Platform * * GET /api/track/{id}/{platform}/stats/{mode} */ export const getApiApiTrackIdPlatformStatsMode = async (args: GetApiApiTrackIdPlatformStatsModeArgs, options?: GetApiApiTrackIdPlatformStatsModeOptions) => { 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.since != null) query.append("since", String(args.since)) if (args.type != null) query.append("type", String(args.type)) if (args.until != null) query.append("until", String(args.until)) if (args.latest != null) query.append("latest", String(args.latest)) if (args.interpolated != null) query.append("interpolated", String(args.interpolated)) if (args.isDomainId != null) query.append("isDomainId", String(args.isDomainId)) if (args.extrapolated != null) query.append("extrapolated", String(args.extrapolated)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/track/${encodeURIComponent(String(args.id))}/${encodeURIComponent(String(args.platform))}/stats/${encodeURIComponent(String(args.mode))}${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(getApiApiTrackIdPlatformStatsModeResponse, await res.json()) };