import {z} from 'zod' export type GetApiApiV2SongUuidStreamingPlatformArgs = {uuid: string, platform: string, startDate?: string | undefined, endDate?: string | undefined, offset?: number | undefined, limit?: number | undefined, sort?: string | undefined}; export type GetApiApiV2SongUuidStreamingPlatformOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const songLight = z.object({uuid: z.string().optional(), name: z.string().optional(), creditName: z.string().optional(), imageUrl: z.string().optional()}); export const streamingAccount2 = z.object({song: songLight.optional(), platform: z.string().optional(), lastCrawlDate: z.string().optional()}); export const cityPlot = z.object({date: z.string().optional(), value: z.number().int().optional(), countryName: z.string().optional(), countryCode: z.string().optional(), region: z.string().optional(), cityName: z.string().optional()}); export const countryPlot = z.object({date: z.string().optional(), value: z.number().int().optional(), countryName: z.string().optional(), countryCode: z.string().optional()}); export const streamingPlot = z.object({date: z.string().optional(), value: z.number().int().optional(), cityPlots: z.array(cityPlot).optional(), countryPlots: z.array(countryPlot).optional()}); export const offsetPage = 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()}); export const error = z.object({key: z.string().optional(), code: z.number().int().optional(), message: z.string().optional()}); export const streamingPlotCollectionResponse2 = z.object({related: streamingAccount2.optional(), items: z.array(streamingPlot).optional(), page: offsetPage.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 local streaming audience * * GET /api/v2/song/{uuid}/streaming/{platform} */ export const getApiApiV2SongUuidStreamingPlatform = async (args: GetApiApiV2SongUuidStreamingPlatformArgs, options: GetApiApiV2SongUuidStreamingPlatformOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/song/${encodeURIComponent(String(args.uuid))}/streaming/${encodeURIComponent(String(args.platform))}${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(streamingPlotCollectionResponse2, await res.json()) };