import {z} from 'zod' export type GetApiApiPlaylistPlatformIdStatsArgs = {id: string | number, platform: 'spotify' | 'deezer' | 'youtube' | 'soundcloud', since?: string | undefined, until?: string | undefined}; export type GetApiApiPlaylistPlatformIdStatsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPlaylistPlatformIdStatsResponse = z.object({obj: z.array(z.object({id: z.number().int().gte(0).lte(2147483647).nullable().optional(), value: z.number().int().gte(0), timestp: z.union([z.string(), z.string()]), daily_diff: z.number().int().nullable().optional(), weekly_diff: z.number().int().nullable().optional(), weekly_diff_percent: z.number().nullable().optional(), monthly_diff: z.number().int().nullable().optional(), monthly_diff_percent: z.number().nullable().optional(), interpolated: z.boolean().optional(), extrapolated: z.boolean().optional(), flags: z.boolean().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 Playlist Stats Over Time * * GET /api/playlist/{platform}/{id}/stats */ export const getApiApiPlaylistPlatformIdStats = async (args: GetApiApiPlaylistPlatformIdStatsArgs, options?: GetApiApiPlaylistPlatformIdStatsOptions) => { 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.until != null) query.append("until", String(args.until)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/playlist/${encodeURIComponent(String(args.platform))}/${encodeURIComponent(String(args.id))}/stats${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(getApiApiPlaylistPlatformIdStatsResponse, await res.json()) };