import {z} from 'zod' export type GetApiNhlMatchesArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, league?: string | undefined, date?: string | undefined, timezone?: string | undefined, season?: number | undefined, homeTeamId?: number | undefined, awayTeamId?: number | undefined, homeTeamName?: string | undefined, awayTeamName?: string | undefined, homeTeamAbbreviation?: string | undefined, awayTeamAbbreviation?: string | undefined, homeTeamDisplayName?: string | undefined, awayTeamDisplayName?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiNhlMatchesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nhlMatchTeamResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string().optional(), displayName: z.string(), abbreviation: z.string()}); export const nhlMatchScoreResponseDto = z.object({current: z.string().optional(), firstPeriod: z.string().optional(), secondPeriod: z.string().optional(), thirdPeriod: z.string().optional(), overtimePeriod: z.string().optional()}); export const nhlMatchStateResponseDto = z.object({period: z.number().optional(), clock: z.number().optional(), description: z.enum(["Suspended", "Postponed", "Cancelled", "Abandoned", "Finished", "In progress", "End period", "Half time", "Unknown", "Scheduled"]), score: nhlMatchScoreResponseDto, report: z.string().optional()}); export const nhlMatchResponseDto = z.object({id: z.number(), round: z.string(), date: z.string(), league: z.string(), season: z.number(), awayTeam: nhlMatchTeamResponseDto, homeTeam: nhlMatchTeamResponseDto, state: nhlMatchStateResponseDto}); 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 nhlMatchPaginatedResponseDto = z.object({data: z.array(nhlMatchResponseDto), 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 matches * * GET /nhl/matches */ export const getApiNhlMatches = async (args: GetApiNhlMatchesArgs, options?: GetApiNhlMatchesOptions) => { 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.league != null) query.append("league", String(args.league)) if (args.date != null) query.append("date", String(args.date)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args.season != null) query.append("season", String(args.season)) if (args.homeTeamId != null) query.append("homeTeamId", String(args.homeTeamId)) if (args.awayTeamId != null) query.append("awayTeamId", String(args.awayTeamId)) if (args.homeTeamName != null) query.append("homeTeamName", String(args.homeTeamName)) if (args.awayTeamName != null) query.append("awayTeamName", String(args.awayTeamName)) if (args.homeTeamAbbreviation != null) query.append("homeTeamAbbreviation", String(args.homeTeamAbbreviation)) if (args.awayTeamAbbreviation != null) query.append("awayTeamAbbreviation", String(args.awayTeamAbbreviation)) if (args.homeTeamDisplayName != null) query.append("homeTeamDisplayName", String(args.homeTeamDisplayName)) if (args.awayTeamDisplayName != null) query.append("awayTeamDisplayName", String(args.awayTeamDisplayName)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/nhl/matches${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(nhlMatchPaginatedResponseDto, await res.json()) };