import {z} from 'zod' export type GetApiFootballBoxScoreMatchIdArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, matchId: number}; export type GetApiFootballBoxScoreMatchIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const footballTeamBoxScoreResponseDto = z.object({id: z.number(), logo: z.string(), name: z.string()}); export const playerBoxScoreStatisticsDto = z.object({goalsScored: z.number(), goalsSaved: z.number(), goalsConceded: z.number(), assists: z.number(), dribblesTotal: z.number(), dribblesSuccessful: z.number(), dribblesFailed: z.number(), dribbleSuccessRate: z.string(), fouledByOthers: z.number(), fouledOthers: z.number(), tacklesTotal: z.number(), interceptionsTotal: z.number(), duelsTotal: z.number(), duelsWon: z.number(), duelsLost: z.number(), duelSuccessRate: z.string(), cardsRed: z.number(), cardsYellow: z.number(), cardsSecondYellow: z.number(), passesAccuracy: z.string(), passesSuccessful: z.number(), passesFailed: z.number(), passesTotal: z.number(), passesKey: z.number(), penaltiesScored: z.number(), penaltiesMissed: z.number(), penaltiesTotal: z.number(), penaltiesAccuracy: z.string(), shotsOnTarget: z.number(), shotsOffTarget: z.number(), shotsTotal: z.number(), shotsAccuracy: z.string(), expectedGoals: z.number().optional(), expectedAssists: z.number().optional(), expectedGoalsOnTarget: z.number().optional(), expectedGoalsOnTargetConceded: z.number().optional(), expectedGoalsPrevented: z.number().optional()}); export const footballPlayerBoxScore = z.object({id: z.number(), name: z.string(), fullName: z.string().optional(), logo: z.string().optional(), matchRating: z.string().optional(), shirtNumber: z.number().optional(), isCaptain: z.boolean(), position: z.enum(["Goalkeeper", "Defender", "Midfielder", "Forward"]).optional(), minutesPlayed: z.number(), isSubstitute: z.boolean(), offsides: z.number(), statistics: z.array(playerBoxScoreStatisticsDto)}); export const footballPlayerMatchStatisticsResponse = z.object({team: footballTeamBoxScoreResponseDto, players: z.array(footballPlayerBoxScore)}); export const getApiFootballBoxScoreMatchIdResponse = z.array(footballPlayerMatchStatisticsResponse); 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 }; /** * Retrieve box scores for specified match id. * * GET /football/box-score/{matchId} */ export const getApiFootballBoxScoreMatchId = async (args: GetApiFootballBoxScoreMatchIdArgs, options?: GetApiFootballBoxScoreMatchIdOptions) => { 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(/\/+$/, '')}/football/box-score/${encodeURIComponent(String(args.matchId))}${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(getApiFootballBoxScoreMatchIdResponse, await res.json()) };