import {z} from 'zod' export type GetApiApiV2PerformanceStatsArgs = {'X-RapidApi-Key'?: string | undefined}; export type GetApiApiV2PerformanceStatsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV2PerformanceStatsResponse = z.object({data: z.object({market: z.string().optional(), accuracy: z.object({yesterday: z.number().optional(), last_7_days: z.number().optional(), last_30_days: z.number().optional(), last_14_days: z.number().optional()}).optional(), details: z.object({yesterday: z.object({total: z.number().int().optional(), lost: z.number().int().optional(), pending: z.number().int().optional(), won: z.number().int().optional(), postponed: z.number().int().optional()}).optional(), last_7_days: z.object({total: z.number().int().optional(), lost: z.number().int().optional(), pending: z.number().int().optional(), won: z.number().int().optional(), postponed: z.number().int().optional()}).optional(), last_30_days: z.object({total: z.number().int().optional(), lost: z.number().int().optional(), pending: z.number().int().optional(), won: z.number().int().optional(), postponed: z.number().int().optional()}).optional(), last_14_days: z.object({total: z.number().int().optional(), lost: z.number().int().optional(), pending: z.number().int().optional(), won: z.number().int().optional(), postponed: z.number().int().optional()}).optional()}).optional()}).optional()}); 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 }; /** * Returns predictions accuracy in the last 1, 7, 14, 30 days. * * GET /api/v2/performance-stats */ export const getApiApiV2PerformanceStats = async (args: GetApiApiV2PerformanceStatsArgs, options?: GetApiApiV2PerformanceStatsOptions) => { const {baseUrl = "https://football-prediction-api.p.rapidapi.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-RapidApi-Key'] != null) headers.set("X-RapidApi-Key", String(args['X-RapidApi-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/performance-stats${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(getApiApiV2PerformanceStatsResponse, await res.json()) };