import {z} from 'zod' export type GetApiNbaHighlightsArgs = {'x-rapidapi-host': string, 'x-rapidapi-key': string, leagueName?: string | 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, homeTeamAbbreviation?: string | undefined, awayTeamAbbreviation?: string | undefined, homeTeamDisplayName?: string | undefined, awayTeamDisplayName?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiNbaHighlightsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nbaTeamInfoResponseDto = z.object({id: z.number(), displayName: z.string(), name: z.string(), logo: z.string().optional(), abbreviation: z.string()}); export const nbaMatchBaseInfo = z.object({id: z.number(), league: z.enum(["NBA", "NCAA"]), homeTeam: nbaTeamInfoResponseDto, awayTeam: nbaTeamInfoResponseDto}); export const nbaHighlightResponseDto = z.object({id: z.number(), match: nbaMatchBaseInfo, type: z.string(), imgUrl: z.string().optional(), title: z.string().optional(), description: z.string().optional(), url: z.string(), embedUrl: z.string(), 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 nbaHighlightPaginatedResponseDto = z.object({data: z.array(nbaHighlightResponseDto), 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 /nba/highlights */ export const getApiNbaHighlights = async (args: GetApiNbaHighlightsArgs, options?: GetApiNbaHighlightsOptions) => { 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.leagueName != null) query.append("leagueName", String(args.leagueName)) 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.homeTeamAbbreviation != null) query.append("homeTeamAbbreviation", String(args.homeTeamAbbreviation)) if (args.awayTeamAbbreviation != null) query.append("awayTeamAbbreviation", String(args.awayTeamAbbreviation)) if (args.homeTeamDisplayName != null) query.append("homeTeamDisplayName", String(args.homeTeamDisplayName)) if (args.awayTeamDisplayName != null) query.append("awayTeamDisplayName", String(args.awayTeamDisplayName)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/nba/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(nbaHighlightPaginatedResponseDto, await res.json()) };