import {z} from 'zod' export type GetApiFootballMatchesIdArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, id: number}; export type GetApiFootballMatchesIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const footballCountryResponseDto = z.object({code: z.string(), name: z.string(), logo: z.string()}); export const footballShotsDto = z.object({playerName: z.string(), time: z.string(), outcome: z.string(), goalTarget: z.string()}); export const footballTopPlayerStatisticsResponse = z.object({name: z.string(), value: z.object({})}); export const footballTopPlayersResponse = z.object({position: z.string(), name: z.string(), statistics: z.array(footballTopPlayerStatisticsResponse)}); export const footballDetailedTeamResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string(), shots: z.array(footballShotsDto).optional(), topPlayers: z.array(footballTopPlayersResponse).optional()}); export const footballMatchLeagueResponseDto = z.object({id: z.number(), season: z.number(), name: z.string(), logo: z.string().optional()}); export const footballMatchScoreResponseDto = z.object({current: z.string().optional(), penalties: z.string().optional()}); export const footballMatchStateResponseDto = z.object({description: z.enum(["To be announced", "Not started", "First half", "Half time", "Second half", "Extra time", "Break time", "Penalties", "Suspended", "Interrupted", "Finished", "Finished after penalties", "Finished after extra time", "Postponed", "Cancelled", "Abandoned", "Awarded", "In progress", "Unknown"]), clock: z.number().optional(), score: footballMatchScoreResponseDto}); export const footballLiveEventTeamDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string()}); export const footballLiveEventDto = z.object({team: footballLiveEventTeamDto, time: z.string(), type: z.enum(["Goal", "Own Goal", "Penalty", "Missed Penalty", "Yellow Card", "Red Card", "Substitution", "VAR Goal Confirmed", "VAR Goal Cancelled", "VAR Penalty", "VAR Penalty Cancelled", "VAR Goal Cancelled - Offside"]), playerId: z.number().optional(), player: z.string().optional(), assistingPlayerId: z.number().optional(), assist: z.string().optional(), substituted: z.string().optional()}); export const footballTeamResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string()}); export const footballMatchStatisticsResponseItemDto = z.object({value: z.number(), displayName: z.string()}); export const footballMatchStatisticsResponseDto = z.object({team: footballTeamResponseDto, statistics: z.array(footballMatchStatisticsResponseItemDto)}); export const detailedMatchRefereeResponseDto = z.object({name: z.string().optional(), nationality: z.string().optional()}); export const detailedMatchVenueResponseDto = z.object({city: z.string().optional(), name: z.string().optional(), country: z.string().optional(), capacity: z.string().optional()}); export const detailedMatchForecastResponseDto = z.object({status: z.string().optional(), temperature: z.string().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 newsDto = z.object({url: z.string(), image: z.string(), title: z.string(), datePublished: z.string()}); export const footballDetailedMatchResponseDto = z.object({id: z.number(), round: z.string(), date: z.string(), country: footballCountryResponseDto, awayTeam: footballDetailedTeamResponseDto, homeTeam: footballDetailedTeamResponseDto, league: footballMatchLeagueResponseDto, state: footballMatchStateResponseDto, events: z.array(footballLiveEventDto), statistics: z.array(footballMatchStatisticsResponseDto), referee: detailedMatchRefereeResponseDto.optional(), venue: detailedMatchVenueResponseDto.optional(), forecast: detailedMatchForecastResponseDto.optional(), predictions: predictionResponseDto.optional(), news: z.array(newsDto).optional()}); export const getApiFootballMatchesIdResponse = z.array(footballDetailedMatchResponseDto); 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 /football/matches/{id} */ export const getApiFootballMatchesId = async (args: GetApiFootballMatchesIdArgs, options?: GetApiFootballMatchesIdOptions) => { 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/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(getApiFootballMatchesIdResponse, await res.json()) };