import {z} from 'zod' export type GetApiApiPlaylistPlatformIdSnapshotArgs = {id: unknown, platform: 'cm' | 'spotify' | 'itunes' | 'applemusic' | 'deezer' | 'shazam' | 'beatport' | 'amazon' | 'youtube' | 'tunefind' | 'soundcloud', storefront?: string | undefined, date: string}; export type GetApiApiPlaylistPlatformIdSnapshotOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPlaylistPlatformIdSnapshotResponse = z.object({obj: z.array(z.object({track_id: z.number().int().gte(0).lte(2147483647).optional(), id: z.number().int().gte(0).lte(2147483647).optional(), added_at: z.string().nullable().optional(), removed_at: z.string().nullable().optional(), position: z.number().int().nullable().optional(), name: z.string().nullable().optional(), isrc: z.string().nullable().optional(), spotify_track_id: z.string().nullable().optional(), spotify_album_id: z.string().nullable().optional(), image_url: z.string().nullable().optional(), spotify_popularity: z.number().int().nullable().optional(), cm_track: z.number().int().gte(0).lte(2147483647).optional(), spotify_artist_names: z.array(z.string().nullable()).nullable().optional(), track_genre: z.string().nullable().optional(), cm_artist: z.array(z.number().int().gte(0).lte(2147483647)).nullable().optional(), artist_names: z.array(z.string().nullable()).nullable().optional(), code2s: z.array(z.string().nullable()).nullable().optional(), artist_images: z.array(z.string().nullable()).nullable().optional(), artist_covers: z.array(z.string().nullable()).nullable().optional(), spotify_artist_ids: z.array(z.string().nullable()).nullable().optional(), spotify_track_ids: z.array(z.string().nullable()).nullable().optional(), spotify_album_ids: z.array(z.string().nullable()).nullable().optional(), spotify_duration_ms: z.number().int().nullable().optional(), album_ids: z.array(z.number().int().gte(0).lte(2147483647)).nullable().optional(), album_names: z.array(z.string().nullable()).nullable().optional(), album_upc: z.array(z.string().nullable()).nullable().optional(), album_label: z.array(z.string().nullable()).nullable().optional(), release_dates: z.array(z.string().nullable()).nullable().optional(), preview_url: z.string().nullable().optional(), storefront: z.string().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 }; /** * Get Playlist Snapshot on a Date * * GET /api/playlist/{platform}/{id}/snapshot */ export const getApiApiPlaylistPlatformIdSnapshot = async (args: GetApiApiPlaylistPlatformIdSnapshotArgs, options?: GetApiApiPlaylistPlatformIdSnapshotOptions) => { 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.storefront != null) query.append("storefront", String(args.storefront)) if (args.date != null) query.append("date", String(args.date)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/playlist/${encodeURIComponent(String(args.platform))}/${encodeURIComponent(String(args.id))}/snapshot${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(getApiApiPlaylistPlatformIdSnapshotResponse, await res.json()) };