import {z} from 'zod' export type GetApiFootballPlayersIdStatisticsArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, id: number}; export type GetApiFootballPlayersIdStatisticsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const footballDetailedPlayerStatisticsPerCompetitionDto = z.object({club: z.string(), type: z.enum(["national league", "national cup", "international cup"]), league: z.string(), season: z.string(), goals: z.number(), assists: z.number(), ownGoals: z.number(), redCards: z.number(), cleanSheets: z.number(), gamesPlayed: z.number(), yellowCards: z.number(), goalsConceded: z.number(), minutesPlayed: z.number(), substitutedIn: z.number(), substitutedOut: z.number(), penaltiesScored: z.number(), secondYellowCards: z.number()}); export const footballDetailedPlayerStatisticsPerClubDto = z.object({club: z.string(), goals: z.number(), assists: z.number(), ownGoals: z.number(), redCards: z.number(), cleanSheets: z.number(), gamesPlayed: z.number(), yellowCards: z.number(), goalsConceded: z.number(), minutesPlayed: z.number(), substitutedIn: z.number(), substitutedOut: z.number(), penaltiesScored: z.number(), secondYellowCards: z.number()}); export const footballDetailedPlayerStatisticsResponseDto = z.object({id: z.number(), name: z.string(), fullName: z.string().optional(), logo: z.string().optional(), perCompetition: z.array(footballDetailedPlayerStatisticsPerCompetitionDto), perClub: z.array(footballDetailedPlayerStatisticsPerClubDto)}); export const getApiFootballPlayersIdStatisticsResponse = z.array(footballDetailedPlayerStatisticsResponseDto); 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 player statistics by player id * * GET /football/players/{id}/statistics */ export const getApiFootballPlayersIdStatistics = async (args: GetApiFootballPlayersIdStatisticsArgs, options?: GetApiFootballPlayersIdStatisticsOptions) => { 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/players/${encodeURIComponent(String(args.id))}/statistics${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(getApiFootballPlayersIdStatisticsResponse, await res.json()) };