import {z} from 'zod' export type GetApiApiTrackIdMilestonesArgs = {id: number, limit?: number | undefined, offset?: number | undefined, 'stars[]'?: Array | undefined, fromDaysAgo?: number | undefined, sortColumn?: ('date' | 'stars') | undefined, sortOrderDesc?: boolean | undefined, 'platforms[]'?: (Array<'amazon' | 'apple_music' | 'beatport' | 'chartmetric' | 'deezer' | 'instagram' | 'itunes' | 'pandora' | 'shazam' | 'soundcloud' | 'spotify' | 'tiktok' | 'youtube'>) | undefined, grouped?: boolean | undefined}; export type GetApiApiTrackIdMilestonesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiTrackIdMilestonesResponse = z.object({obj: z.object({insights: z.array(z.object({id: z.union([z.number(), z.string()]).optional(), date: z.string().nullable().optional(), summary: z.string().nullable().optional(), artist_name: z.string().nullable().optional(), track_name: z.string().nullable().optional(), platform: z.string().nullable().optional(), stars: z.number().int().nullable().optional()})), count: z.number().int().gte(0).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 }; /** * List Milestones for a Track * * GET /api/track/{id}/milestones */ export const getApiApiTrackIdMilestones = async (args: GetApiApiTrackIdMilestonesArgs, options?: GetApiApiTrackIdMilestonesOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) for (const item of args['stars[]'] ?? []) query.append("stars[]", String(item)) if (args.fromDaysAgo != null) query.append("fromDaysAgo", String(args.fromDaysAgo)) if (args.sortColumn != null) query.append("sortColumn", String(args.sortColumn)) if (args.sortOrderDesc != null) query.append("sortOrderDesc", String(args.sortOrderDesc)) for (const item of args['platforms[]'] ?? []) query.append("platforms[]", String(item)) if (args.grouped != null) query.append("grouped", String(args.grouped)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/track/${encodeURIComponent(String(args.id))}/milestones${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(getApiApiTrackIdMilestonesResponse, await res.json()) };