import {z} from 'zod' export type DefPlayerID = string; export type DefTeamID = string | null; export type LeagueID = string; export type OffPlayerID = string; export type OffTeamID = string | null; export type PerMode = string; export type Season = string; export type SeasonType = string; export type GetApiMatchupsrollupArgs = {DefPlayerID?: DefPlayerID | undefined, DefTeamID?: DefTeamID | undefined, LeagueID: LeagueID, OffPlayerID?: OffPlayerID | undefined, OffTeamID?: OffTeamID | undefined, PerMode: PerMode, Season: Season, SeasonType: SeasonType}; export type GetApiMatchupsrollupOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMatchupsrollupResponse = z.object({parameters: z.object({DefPlayerID: z.literal("$request.query.DefPlayerID").optional(), DefTeamID: z.literal("$request.query.DefTeamID").optional(), LeagueID: z.literal("$request.query.LeagueID").optional(), OffPlayerID: z.literal("$request.query.OffPlayerID").optional(), OffTeamID: z.literal("$request.query.OffTeamID").optional(), PerMode: z.literal("$request.query.PerMode").optional(), Season: z.literal("$request.query.Season").optional(), SeasonType: z.literal("$request.query.SeasonType").optional()}).optional(), resource: z.literal("matchupsrollup").optional(), resultSets: z.array(z.unknown()).optional()}); 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 }; /** * MatchupsRollup * * GET /matchupsrollup */ export const getApiMatchupsrollup = async (args: GetApiMatchupsrollupArgs, options?: GetApiMatchupsrollupOptions) => { const {baseUrl = "https://stats.nba.com/stats", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.DefPlayerID != null) query.append("DefPlayerID", String(args.DefPlayerID)) if (args.DefTeamID != null) query.append("DefTeamID", String(args.DefTeamID)) if (args.LeagueID != null) query.append("LeagueID", String(args.LeagueID)) if (args.OffPlayerID != null) query.append("OffPlayerID", String(args.OffPlayerID)) if (args.OffTeamID != null) query.append("OffTeamID", String(args.OffTeamID)) if (args.PerMode != null) query.append("PerMode", String(args.PerMode)) if (args.Season != null) query.append("Season", String(args.Season)) if (args.SeasonType != null) query.append("SeasonType", String(args.SeasonType)) const url = `${baseUrl.replace(/\/+$/, '')}/matchupsrollup${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(getApiMatchupsrollupResponse, await res.json()) };