import {z} from 'zod' export type GetApiRugbyTeamsStatisticsIdArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, id: number, fromDate: string, timezone?: string | undefined}; export type GetApiRugbyTeamsStatisticsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const rugbyTotalGamesResponseDto = z.object({played: z.number(), wins: z.number(), loses: z.number(), draws: z.number()}); export const rugbyTotalPointsResponseDto = z.object({scored: z.number(), received: z.number()}); export const rugbyTotalStatsResponseDto = z.object({games: rugbyTotalGamesResponseDto, points: rugbyTotalPointsResponseDto}); export const rugbyHomeGamesResponseDto = z.object({played: z.number(), wins: z.number(), loses: z.number(), draws: z.number()}); export const rugbyHomePointsResponseDto = z.object({scored: z.number(), received: z.number()}); export const rugbyHomeStatsResponseDto = z.object({games: rugbyHomeGamesResponseDto, points: rugbyHomePointsResponseDto}); export const rugbyAwayGamesResponseDto = z.object({played: z.number(), wins: z.number(), draws: z.number(), loses: z.number()}); export const rugbyAwayPointsResponseDto = z.object({scored: z.number(), received: z.number()}); export const rugbyAwayStatsResponseDto = z.object({games: rugbyAwayGamesResponseDto, points: rugbyAwayPointsResponseDto}); export const rugbyTeamStatsResponseDto = z.object({total: rugbyTotalStatsResponseDto, home: rugbyHomeStatsResponseDto, away: rugbyAwayStatsResponseDto, leagueId: z.number(), leagueName: z.string(), season: z.number()}); export const getApiRugbyTeamsStatisticsIdResponse = z.array(rugbyTeamStatsResponseDto); 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 team statistics * * GET /rugby/teams/statistics/{id} */ export const getApiRugbyTeamsStatisticsId = async (args: GetApiRugbyTeamsStatisticsIdArgs, options?: GetApiRugbyTeamsStatisticsIdOptions) => { 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.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.timezone != null) query.append("timezone", String(args.timezone)) const url = `${baseUrl.replace(/\/+$/, '')}/rugby/teams/statistics/${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(getApiRugbyTeamsStatisticsIdResponse, await res.json()) };