import {z} from 'zod' export type GetApiNbaMatchesIdArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, id: number}; export type GetApiNbaMatchesIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nbaMatchScoreResponseDto = z.object({awayTeam: z.array(z.number()), homeTeam: z.array(z.number())}); export const nbaMatchStateResponseDto = z.object({clock: z.number(), score: nbaMatchScoreResponseDto, period: z.number(), description: z.enum(["Suspended", "Postponed", "Cancelled", "Abandoned", "Finished", "Scheduled", "In progress", "End period", "Half time", "Unknown"])}); export const nbaTeamInfoResponseDto = z.object({id: z.number(), displayName: z.string(), name: z.string(), logo: z.string().optional(), abbreviation: z.string()}); export const venueDetails = z.object({city: z.string().optional(), name: z.string().optional(), state: z.string().optional()}); export const detailedMatchForecastResponseDto = z.object({status: z.string().optional(), temperature: z.string().optional()}); export const matchStatisticsItem = z.object({name: z.string(), value: z.number()}); export const matchTeamStatistics = z.object({statistics: z.array(matchStatisticsItem)}); export const matchStatistics = z.object({awayTeam: matchTeamStatistics, homeTeam: matchTeamStatistics}); export const event = z.object({team: nbaTeamInfoResponseDto, clock: z.string().optional(), period: z.string().optional(), description: z.string().optional(), isScoringPlay: z.boolean().optional(), isShootingPlay: z.boolean().optional()}); export const predictionProbabilitiesDto = z.object({home: z.string(), draw: z.string().optional(), away: z.string()}); export const predictionDto = z.object({type: z.string(), modelType: z.string(), generatedAt: z.string(), description: z.string(), probabilities: predictionProbabilitiesDto}); export const predictionResponseDto = z.object({prematch: z.array(predictionDto), live: z.array(predictionDto)}); export const nbaDetailedMatchResponseDto = z.object({id: z.number(), league: z.enum(["NBA", "NCAA"]), season: z.number(), state: nbaMatchStateResponseDto, homeTeam: nbaTeamInfoResponseDto, awayTeam: nbaTeamInfoResponseDto, date: z.string(), venue: venueDetails.optional(), forecast: detailedMatchForecastResponseDto.optional(), matchStatistics: matchStatistics.optional(), events: z.array(event).optional(), predictions: predictionResponseDto.optional()}); export const getApiNbaMatchesIdResponse = z.array(nbaDetailedMatchResponseDto); 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 match by id * * GET /nba/matches/{id} */ export const getApiNbaMatchesId = async (args: GetApiNbaMatchesIdArgs, options?: GetApiNbaMatchesIdOptions) => { 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'])) const url = `${baseUrl.replace(/\/+$/, '')}/nba/matches/${encodeURIComponent(String(args.id))}${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(getApiNbaMatchesIdResponse, await res.json()) };