import {z} from 'zod' export type GetApiHockeyHighlightsIdArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, id: number}; export type GetApiHockeyHighlightsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const hockeyCountryResponseDto = z.object({code: z.string(), name: z.string(), logo: z.string()}); export const hockeyTeamResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string()}); export const hockeyMatchLeagueResponseDto = z.object({id: z.number(), season: z.number(), name: z.string(), logo: z.string().optional()}); export const hockeyMatchScoreResponseDto = z.object({current: z.string().optional(), overTime: z.string().optional(), penalties: z.string().optional(), firstPeriod: z.string().optional(), secondPeriod: z.string().optional(), thirdPeriod: z.string().optional()}); export const hockeyMatchStateResponseDto = z.object({description: z.enum(["Not started", "1st period", "2nd period", "3rd period", "Over time", "Penalties", "Break time", "Awarded", "Postponed", "Cancelled", "Interrupted", "Abandoned", "Finished", "To be announced", "Finished after penalties", "Finished after over time", "Unknown"]), score: hockeyMatchScoreResponseDto}); export const hockeyMatchResponseDto = z.object({id: z.number(), week: z.string().optional(), date: z.string(), country: hockeyCountryResponseDto, awayTeam: hockeyTeamResponseDto, homeTeam: hockeyTeamResponseDto, league: hockeyMatchLeagueResponseDto, state: hockeyMatchStateResponseDto}); export const hockeyHighlightResponseDto = 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: hockeyMatchResponseDto, channel: z.string().optional(), source: z.string().optional()}); export const getApiHockeyHighlightsIdResponse = z.array(hockeyHighlightResponseDto); 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 highlight by id * * GET /hockey/highlights/{id} */ export const getApiHockeyHighlightsId = async (args: GetApiHockeyHighlightsIdArgs, options?: GetApiHockeyHighlightsIdOptions) => { 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(/\/+$/, '')}/hockey/highlights/${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(getApiHockeyHighlightsIdResponse, await res.json()) };