import {z} from 'zod' export type GetApiRugbyHighlightsArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, countryCode?: string | undefined, countryName?: string | undefined, leagueName?: string | undefined, leagueId?: number | undefined, date?: string | undefined, timezone?: string | undefined, season?: number | undefined, matchId?: number | undefined, homeTeamId?: number | undefined, awayTeamId?: number | undefined, homeTeamName?: string | undefined, awayTeamName?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiRugbyHighlightsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const rugbyCountryResponseDto = z.object({code: z.string(), name: z.string(), logo: z.string()}); export const rugbyTeamResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string()}); export const rugbyMatchLeagueResponseDto = z.object({id: z.number(), season: z.number(), name: z.string(), logo: z.string().optional()}); export const rugbyMatchStateResponseDto = z.object({description: z.enum(["Not started", "First half", "Half time", "Second half", "Extra time", "Break time", "Penalties", "Suspended", "Interrupted", "Finished", "Finished after extra time", "Postponed", "Cancelled", "Abandoned", "Awarded", "Unknown", "To be announced"]), score: z.string().optional()}); export const rugbyMatchResponseDto = z.object({id: z.number(), week: z.string().optional(), date: z.string(), country: rugbyCountryResponseDto, awayTeam: rugbyTeamResponseDto, homeTeam: rugbyTeamResponseDto, league: rugbyMatchLeagueResponseDto, state: rugbyMatchStateResponseDto}); export const rugbyHighlightResponseDto = z.object({id: z.number(), type: z.string(), imgUrl: z.string().optional(), title: z.string(), description: z.string().optional(), url: z.string(), embedUrl: z.string().optional(), match: rugbyMatchResponseDto, channel: z.string().optional(), source: z.string().optional()}); export const paginationResponseDto = z.object({totalCount: z.number(), offset: z.number(), limit: z.number()}); export const planResponseDto = z.object({tier: z.string(), message: z.string()}); export const rugbyHighlightPaginatedResponseDto = z.object({data: z.array(rugbyHighlightResponseDto), pagination: paginationResponseDto, plan: planResponseDto}); 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 all highlights * * GET /rugby/highlights */ export const getApiRugbyHighlights = async (args: GetApiRugbyHighlightsArgs, options?: GetApiRugbyHighlightsOptions) => { 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.countryCode != null) query.append("countryCode", String(args.countryCode)) if (args.countryName != null) query.append("countryName", String(args.countryName)) if (args.leagueName != null) query.append("leagueName", String(args.leagueName)) if (args.leagueId != null) query.append("leagueId", String(args.leagueId)) if (args.date != null) query.append("date", String(args.date)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args.season != null) query.append("season", String(args.season)) if (args.matchId != null) query.append("matchId", String(args.matchId)) if (args.homeTeamId != null) query.append("homeTeamId", String(args.homeTeamId)) if (args.awayTeamId != null) query.append("awayTeamId", String(args.awayTeamId)) if (args.homeTeamName != null) query.append("homeTeamName", String(args.homeTeamName)) if (args.awayTeamName != null) query.append("awayTeamName", String(args.awayTeamName)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/rugby/highlights${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(rugbyHighlightPaginatedResponseDto, await res.json()) };