import {z} from 'zod' export type GetApiHockeyOddsArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, oddsType?: string | undefined, leagueId?: number | undefined, leagueName?: string | undefined, timezone?: string | undefined, bookmakerId?: number | undefined, limit?: number | undefined, offset?: number | undefined, matchId?: number | undefined, bookmakerName?: string | undefined, date?: string | undefined}; export type GetApiHockeyOddsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const hockeyMarketSelectionResponseDto = z.object({odd: z.number(), value: z.string()}); export const hockeyMarketResponseDto = z.object({bookmakerId: z.number(), bookmakerName: z.string().optional(), type: z.string(), market: z.string(), values: z.array(hockeyMarketSelectionResponseDto)}); export const hockeyOddsResponseDto = z.object({matchId: z.number(), odds: z.array(hockeyMarketResponseDto)}); 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 hockeyOddsPaginatedResponseDto = z.object({data: z.array(hockeyOddsResponseDto), 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 odds * * GET /hockey/odds */ export const getApiHockeyOdds = async (args: GetApiHockeyOddsArgs, options?: GetApiHockeyOddsOptions) => { 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.oddsType != null) query.append("oddsType", String(args.oddsType)) if (args.leagueId != null) query.append("leagueId", String(args.leagueId)) if (args.leagueName != null) query.append("leagueName", String(args.leagueName)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args.bookmakerId != null) query.append("bookmakerId", String(args.bookmakerId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.matchId != null) query.append("matchId", String(args.matchId)) if (args.bookmakerName != null) query.append("bookmakerName", String(args.bookmakerName)) if (args.date != null) query.append("date", String(args.date)) const url = `${baseUrl.replace(/\/+$/, '')}/hockey/odds${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(hockeyOddsPaginatedResponseDto, await res.json()) };