import {z} from 'zod' export type GetApiCricketLastFiveGamesArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, teamId: number}; export type GetApiCricketLastFiveGamesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const cricketCountryResponseDto = z.object({code: z.string(), name: z.string(), logo: z.string()}); export const cricketTeamResponseDto = z.object({id: z.string(), logo: z.string().optional(), name: z.string(), abbreviation: z.string()}); export const cricketMatchLeagueResponseDto = z.object({id: z.string(), season: z.number(), name: z.string(), logo: z.string().optional()}); export const cricketMatchScoreTeamAwayDto = z.object({info: z.string().optional(), score: z.string().optional()}); export const cricketMatchScoreTeamHomeDto = z.object({info: z.string().optional(), score: z.string().optional()}); export const cricketMatchScoreTeamsDto = z.object({away: cricketMatchScoreTeamAwayDto, home: cricketMatchScoreTeamHomeDto}); export const cricketMatchStateResponseDto = z.object({description: z.enum(["Finished", "Unknown", "No live coverage", "In play", "Stumps", "Lunch", "Innings break", "Drinks", "Timeout", "Tea", "Scheduled", "Match delayed", "Cancelled", "Abandoned", "Postponed"]), report: z.string().optional(), teams: cricketMatchScoreTeamsDto}); export const cricketMatchResponseDto = z.object({id: z.string(), endDate: z.string(), startDate: z.string(), startTime: z.string().optional(), country: cricketCountryResponseDto, awayTeam: cricketTeamResponseDto, homeTeam: cricketTeamResponseDto, league: cricketMatchLeagueResponseDto, format: z.string().optional(), dayType: z.enum(["SINGLE", "MULTI"]).optional(), state: cricketMatchStateResponseDto}); export const getApiCricketLastFiveGamesResponse = z.array(cricketMatchResponseDto); 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 last five finished games for a specific team id * * GET /cricket/last-five-games */ export const getApiCricketLastFiveGames = async (args: GetApiCricketLastFiveGamesArgs, options?: GetApiCricketLastFiveGamesOptions) => { 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.teamId != null) query.append("teamId", String(args.teamId)) const url = `${baseUrl.replace(/\/+$/, '')}/cricket/last-five-games${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(getApiCricketLastFiveGamesResponse, await res.json()) };