import {z} from 'zod' export type GetApiRugbyMatchesIdArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, id: number}; export type GetApiRugbyMatchesIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const rugbyCountryResponseDto = z.object({code: z.string(), name: z.string(), logo: z.string()}); export const rugbyTeamResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string()}); export const rugbyMatchLeagueResponseDto = z.object({id: z.number(), season: z.number(), name: z.string(), logo: z.string().optional()}); export const rugbyMatchStateResponseDto = z.object({description: z.enum(["Not started", "First half", "Half time", "Second half", "Extra time", "Break time", "Penalties", "Suspended", "Interrupted", "Finished", "Finished after extra time", "Postponed", "Cancelled", "Abandoned", "Awarded", "Unknown", "To be announced"]), score: 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 detailedMatchRefereeResponseDto = z.object({name: z.string().optional(), nationality: 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 rugbyMatchLineupPlayerDto = z.object({name: z.string(), shortName: z.string(), countryName: z.string().optional(), birth: z.string().optional(), height: z.string().optional(), position: z.enum(["Full Back", "Back", "Forward", "Prop"]).optional(), shirtNumber: z.number()}); export const rugbyMatchLineupsDto = z.object({initialLineup: z.array(rugbyMatchLineupPlayerDto), substitutions: z.array(rugbyMatchLineupPlayerDto)}); export const rugbyMatchLineupsResponseDto = z.object({home: rugbyMatchLineupsDto, away: rugbyMatchLineupsDto}); export const rugbyDetailedMatchResponseDto = z.object({id: z.number(), week: z.string().optional(), date: z.string(), country: rugbyCountryResponseDto, awayTeam: rugbyTeamResponseDto, homeTeam: rugbyTeamResponseDto, league: rugbyMatchLeagueResponseDto, state: rugbyMatchStateResponseDto, venue: detailedMatchVenueResponseDto.optional(), referee: detailedMatchRefereeResponseDto.optional(), forecast: detailedMatchForecastResponseDto.optional(), predictions: predictionResponseDto.optional(), lineups: rugbyMatchLineupsResponseDto.optional()}); export const getApiRugbyMatchesIdResponse = z.array(rugbyDetailedMatchResponseDto); 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 /rugby/matches/{id} */ export const getApiRugbyMatchesId = async (args: GetApiRugbyMatchesIdArgs, options?: GetApiRugbyMatchesIdOptions) => { 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(/\/+$/, '')}/rugby/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(getApiRugbyMatchesIdResponse, await res.json()) };