import {z} from 'zod' export type CreateApiApiTrackPlaylistStreamEstimatesArgs = {date?: string | undefined, since?: string | undefined, until?: string | undefined, body: {ids: Array}}; export type CreateApiApiTrackPlaylistStreamEstimatesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiTrackPlaylistStreamEstimatesResponse = z.object({obj: z.array(z.object({cm_track: z.number().int().gte(0).lte(2147483647), status: z.enum(["available", "not_found"]), estimates: z.array(z.object({cm_track: z.number().int().gte(0).lte(2147483647), timestp: z.string(), playlist_stream_pct: z.number().gte(0).lte(100), playlist_count: z.number().int().gte(0), expected_playlist_streams: z.number().gte(0), streams_gained_in_period: z.number().gte(0), expected_editorial: z.number().gte(0), expected_chart: z.number().gte(0), expected_user_other: z.number().gte(0)}))}))}); 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 Batch Playlist Stream Estimates * * POST /api/track/playlist-stream-estimates */ export const createApiApiTrackPlaylistStreamEstimates = async (args: CreateApiApiTrackPlaylistStreamEstimatesArgs, options?: CreateApiApiTrackPlaylistStreamEstimatesOptions) => { const {baseUrl = "https://api.chartmetric.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) 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/track/playlist-stream-estimates${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiTrackPlaylistStreamEstimatesResponse, await res.json()) };