import {z} from 'zod' export type GetApiNhlHighlightsIdArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, id: number}; export type GetApiNhlHighlightsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nhlMatchTeamResponseDto = z.object({id: z.number(), logo: z.string().optional(), name: z.string().optional(), displayName: z.string(), abbreviation: z.string()}); export const nhlMatchScoreResponseDto = z.object({current: z.string().optional(), firstPeriod: z.string().optional(), secondPeriod: z.string().optional(), thirdPeriod: z.string().optional(), overtimePeriod: z.string().optional()}); export const nhlMatchStateResponseDto = z.object({period: z.number().optional(), clock: z.number().optional(), description: z.enum(["Suspended", "Postponed", "Cancelled", "Abandoned", "Finished", "In progress", "End period", "Half time", "Unknown", "Scheduled"]), score: nhlMatchScoreResponseDto, report: z.string().optional()}); export const nhlMatchResponseDto = z.object({id: z.number(), round: z.string(), date: z.string(), league: z.string(), season: z.number(), awayTeam: nhlMatchTeamResponseDto, homeTeam: nhlMatchTeamResponseDto, state: nhlMatchStateResponseDto}); export const nhlHighlightResponseDto = 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: nhlMatchResponseDto, channel: z.string().optional(), source: z.string().optional()}); export const getApiNhlHighlightsIdResponse = z.array(nhlHighlightResponseDto); 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 /nhl/highlights/{id} */ export const getApiNhlHighlightsId = async (args: GetApiNhlHighlightsIdArgs, options?: GetApiNhlHighlightsIdOptions) => { 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(/\/+$/, '')}/nhl/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(getApiNhlHighlightsIdResponse, await res.json()) };