import {z} from 'zod' export type CreateApiApiV2TopSongsArgs = {offset?: number | undefined, limit?: number | undefined, cursor?: string | undefined, body: {sort?: ({platform?: string | undefined, metricType?: string | undefined, period?: string | undefined, sortBy?: string | undefined, order?: string | undefined}) | undefined, filters?: (Array<{type?: string | undefined, data?: ({platform?: string | undefined, metricType?: string | undefined, min?: number | undefined, max?: number | undefined}) | undefined}>) | undefined}}; export type CreateApiApiV2TopSongsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const topRelated = z.object({updatedAt: z.string().optional()}); export const songLightReleaseDate = z.object({uuid: z.string().optional(), name: z.string().optional(), creditName: z.string().optional(), imageUrl: z.string().optional(), releaseDate: z.string().optional()}); export const periodMetric = z.object({volume: z.number().int().optional(), percent: z.number().optional()}); export const platformMetric = z.object({platformCode: z.string().optional(), metricType: z.string().optional(), total: z.number().int().optional(), week: periodMetric.optional(), month: periodMetric.optional(), quarter: periodMetric.optional()}); export const audio = z.object({acousticness: z.number().optional(), danceability: z.number().optional(), energy: z.number().optional(), instrumentalness: z.number().optional(), key: z.number().int().optional(), liveness: z.number().optional(), loudness: z.number().optional(), mode: z.number().int().optional(), speechiness: z.number().optional(), tempo: z.number().optional(), timeSignature: z.number().int().optional(), valence: z.number().optional()}); export const metricSong = z.object({song: songLightReleaseDate.optional(), audience: z.array(platformMetric).optional(), popularity: z.array(platformMetric).optional(), custom: z.array(platformMetric).optional(), duration: z.number().int().optional(), audio: audio.optional()}); export const cursorPage = z.object({offset: z.number().int().optional(), limit: z.number().int().optional(), next: z.string().optional(), previous: z.string().optional(), total: z.number().int().optional(), cursor: z.string().optional()}); export const error = z.object({key: z.string().optional(), code: z.number().int().optional(), message: z.string().optional()}); export const topSongCollectionResponse = z.object({related: topRelated.optional(), items: z.array(metricSong).optional(), page: cursorPage.optional(), errors: z.array(error).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 songs (premium) * * POST /api/v2/top/songs */ export const createApiApiV2TopSongs = async (args: CreateApiApiV2TopSongsArgs, options: CreateApiApiV2TopSongsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/top/songs${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(topSongCollectionResponse, await res.json()) };