import {z} from 'zod' export type GetApiFootballPlayersIdArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, id: number}; export type GetApiFootballPlayersIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const footballDetailedPlayerSummaryPositionDto = z.object({main: z.string().optional(), secondary: z.string().optional()}); export const footballDetailedPlayerSummaryClubDto = z.object({current: z.string().optional(), joinedAt: z.string().optional(), contractExpiry: z.string().optional(), latestContractExtension: z.string().optional()}); export const footballDetailedPlayerSummaryProfileDto = z.object({fullName: z.string().optional(), birthDate: z.string().optional(), deathDate: z.string().optional(), birthPlace: z.string().optional(), citizenship: z.string().optional(), foot: z.string().optional(), height: z.string().optional(), position: footballDetailedPlayerSummaryPositionDto, club: footballDetailedPlayerSummaryClubDto}); export const footballDetailedPlayerSummaryRumourDto = z.object({club: z.string(), rumourDate: z.string(), transferProbability: z.string()}); export const footballDetailedPlayerSummaryRumoursDto = z.object({current: z.array(footballDetailedPlayerSummaryRumourDto), historical: z.array(footballDetailedPlayerSummaryRumourDto)}); export const footballDetailedPlayerSummaryRelatedNewsDto = z.object({url: z.string(), date: z.string(), title: z.string()}); export const footballDetailedPlayerSummaryTransfersDto = z.object({to: z.string(), fee: z.string().optional(), from: z.string(), type: z.enum(["transfer", "loan", "end of loan", "retired"]), season: z.string(), marketValue: z.string().optional(), transferDate: z.string()}); export const footballDetailedPlayerMarketValuesDto = z.object({recordedDate: z.string(), club: z.string(), value: z.number(), age: z.number(), currency: z.string()}); export const footballDetailedPlayerInjuriesDto = z.object({reason: z.string(), season: z.string(), fromDate: z.string(), toDate: z.string(), missedGames: z.number(), absentDurationInDays: z.number().optional()}); export const footballDetailedPlayerSummaryResponseDto = z.object({id: z.number(), name: z.string(), fullName: z.string().optional(), logo: z.string().optional(), profile: footballDetailedPlayerSummaryProfileDto.optional(), rumours: footballDetailedPlayerSummaryRumoursDto, relatedNews: z.array(footballDetailedPlayerSummaryRelatedNewsDto), transfers: z.array(footballDetailedPlayerSummaryTransfersDto), marketValue: z.array(footballDetailedPlayerMarketValuesDto), injuries: z.array(footballDetailedPlayerInjuriesDto)}); export const getApiFootballPlayersIdResponse = z.array(footballDetailedPlayerSummaryResponseDto); 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 summary by player id * * GET /football/players/{id} */ export const getApiFootballPlayersId = async (args: GetApiFootballPlayersIdArgs, options?: GetApiFootballPlayersIdOptions) => { 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))}${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(getApiFootballPlayersIdResponse, await res.json()) };