import {z} from 'zod' export type GetApiVolleyballLeaguesArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, limit?: number | undefined, offset?: number | undefined, season?: number | undefined, leagueName?: string | undefined, countryCode?: string | undefined, countryName?: string | undefined}; export type GetApiVolleyballLeaguesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const volleyballCountryResponseDto = z.object({code: z.string(), name: z.string(), logo: z.string()}); export const volleyballSeasonResponseDto = z.object({season: z.number()}); export const volleyballLeagueResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string(), country: volleyballCountryResponseDto, seasons: z.array(volleyballSeasonResponseDto)}); export const paginationResponseDto = z.object({totalCount: z.number(), offset: z.number(), limit: z.number()}); export const planResponseDto = z.object({tier: z.string(), message: z.string()}); export const volleyballLeaguePaginatedResponseDto = z.object({data: z.array(volleyballLeagueResponseDto), pagination: paginationResponseDto, plan: planResponseDto}); 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 all leagues * * GET /volleyball/leagues */ export const getApiVolleyballLeagues = async (args: GetApiVolleyballLeaguesArgs, options?: GetApiVolleyballLeaguesOptions) => { const {baseUrl = "https://sports.highlightly.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-rapidapi-host'] != null) headers.set("x-rapidapi-host", String(args['x-rapidapi-host'])) if (args['x-rapidapi-key'] != null) headers.set("x-rapidapi-key", String(args['x-rapidapi-key'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.season != null) query.append("season", String(args.season)) if (args.leagueName != null) query.append("leagueName", String(args.leagueName)) if (args.countryCode != null) query.append("countryCode", String(args.countryCode)) if (args.countryName != null) query.append("countryName", String(args.countryName)) const url = `${baseUrl.replace(/\/+$/, '')}/volleyball/leagues${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(volleyballLeaguePaginatedResponseDto, await res.json()) };