import {z} from 'zod' export type GetApiHandballHead2HeadArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, teamIdOne: number, teamIdTwo: number}; export type GetApiHandballHead2HeadOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const handballCountryResponseDto = z.object({code: z.string(), name: z.string(), logo: z.string()}); export const handballTeamResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string()}); export const handballMatchLeagueResponseDto = z.object({id: z.number(), season: z.number(), name: z.string(), logo: z.string().optional()}); export const handballMatchScoreResponseDto = z.object({current: z.string().optional(), firstHalf: z.string().optional(), secondHalf: z.string().optional()}); export const handballMatchStateResponseDto = z.object({description: z.enum(["Not started", "First half", "Half time", "Second half", "Extra time", "Break time", "Penalties", "Suspended", "Interrupted", "Finished", "Postponed", "Cancelled", "Abandoned", "Awarded", "Unknown", "To be announced", "Finished after penalties", "Finished after extra time"]), score: handballMatchScoreResponseDto}); export const handballMatchResponseDto = z.object({id: z.number(), week: z.object({}).optional(), date: z.string(), country: handballCountryResponseDto, awayTeam: handballTeamResponseDto, homeTeam: handballTeamResponseDto, league: handballMatchLeagueResponseDto, state: handballMatchStateResponseDto}); export const getApiHandballHead2HeadResponse = z.array(handballMatchResponseDto); 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 head 2 head games * * GET /handball/head-2-head */ export const getApiHandballHead2Head = async (args: GetApiHandballHead2HeadArgs, options?: GetApiHandballHead2HeadOptions) => { 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.teamIdOne != null) query.append("teamIdOne", String(args.teamIdOne)) if (args.teamIdTwo != null) query.append("teamIdTwo", String(args.teamIdTwo)) const url = `${baseUrl.replace(/\/+$/, '')}/handball/head-2-head${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(getApiHandballHead2HeadResponse, await res.json()) };