import {z} from 'zod' export type GetApiApiRadioStationListArgs = {code2?: string | undefined}; export type GetApiApiRadioStationListOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiRadioStationListResponse = z.object({obj: z.array(z.object({id: z.number().int().gte(0).lte(2147483647), name: z.string().nullable(), genre: z.string().nullable().optional(), frequency: z.string().nullable().optional(), market: z.string().nullable().optional(), broadcast_area: z.string().nullable().optional(), country: z.string().nullable().optional(), station_state: z.string().nullable().optional(), image_url: z.string().nullable().optional(), aqh: z.number().int().nullable().optional(), platform: z.string().nullable().optional(), facebook_followers: z.number().int().nullable().optional(), facebook_likes: z.number().int().nullable().optional(), instagram_followers: z.number().int().nullable().optional(), twitter_followers: z.number().int().nullable().optional(), wikipedia_views: z.number().int().nullable().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 Radio Station List for Country * * GET /api/radio/station-list */ export const getApiApiRadioStationList = async (args: GetApiApiRadioStationListArgs, options?: GetApiApiRadioStationListOptions) => { 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.code2 != null) query.append("code2", String(args.code2)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/radio/station-list${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(getApiApiRadioStationListResponse, await res.json()) };