import {z} from 'zod' export type GetApiApiPlaylistPlatformIdTracksTrackIdArgs = {id: string | number, platform: 'spotify', trackId: number}; export type GetApiApiPlaylistPlatformIdTracksTrackIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPlaylistPlatformIdTracksTrackIdResponse = z.object({obj: z.object({found: z.boolean(), span: z.enum(["", "current", "past"]).nullable().optional(), position: z.number().int().nullable().optional(), peak_position: z.number().int().nullable().optional(), added_at: z.string().nullable().optional(), removed_at: z.string().nullable().optional(), followers: z.number().int().nullable().optional(), playlist_type: z.object({editorial: z.boolean().optional(), personalized: z.boolean().optional(), fullyPersonalized: z.boolean().optional(), chart: z.boolean().optional(), thisIs: z.boolean().optional(), newMusicFriday: z.boolean().optional(), radio: z.boolean().optional(), brand: z.boolean().optional(), majorCurator: z.boolean().optional(), popularIndie: z.boolean().optional(), indie: z.boolean().optional(), audiobook: z.boolean().optional()}).nullable().optional(), stints: z.array(z.object({added_at: z.string().optional(), removed_at: z.string().nullable().optional(), position: z.number().int().nullable().optional()})).optional(), total_stints: z.number().int().gte(0).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 a Track's Placement History on a Playlist * * GET /api/playlist/{platform}/{id}/tracks/{trackId} */ export const getApiApiPlaylistPlatformIdTracksTrackId = async (args: GetApiApiPlaylistPlatformIdTracksTrackIdArgs, options?: GetApiApiPlaylistPlatformIdTracksTrackIdOptions) => { 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/playlist/${encodeURIComponent(String(args.platform))}/${encodeURIComponent(String(args.id))}/tracks/${encodeURIComponent(String(args.trackId))}${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(getApiApiPlaylistPlatformIdTracksTrackIdResponse, await res.json()) };