import {z} from 'zod' export type Conference = 'null' | 'East' | 'West' | null; export type DateFrom = string | null; export type DateTo = string | null; export type Division = 'null' | 'Atlantic' | 'Central' | 'Northwest' | 'Pacific' | 'Southeast' | 'Southwest' | null; export type GameSegment = 'null' | 'First Half' | 'Overtime' | 'Second Half' | null; export type LastNGames = string | null; export type LeagueID = string; export type Location = 'null' | 'Home' | 'Road' | null; export type MeasureType = 'Base' | 'Advanced' | 'Misc' | 'Four Factors' | 'Scoring' | 'Opponent' | 'Usage' | 'Defense'; export type Month = string | null; export type OpponentTeamID = string | null; export type Outcome = 'null' | 'W' | 'L' | null; export type PaceAdjust = 'Y' | 'N'; export type PerMode = string; export type Period = string; export type PlayerIDList = string; export type PlusMinus = 'Y' | 'N'; export type Rank = 'Y' | 'N'; export type Season = string; export type SeasonSegment = 'null' | 'Post All-Star' | 'Pre All-Star' | null; export type SeasonType = string; export type ShotClockRange = 'null' | '24-22' | '22-18 Very Early' | '18-15 Early' | '15-7 Average' | '7-4 Late' | '4-0 Very Late' | 'ShotClock Off' | null; export type VsConference = 'null' | 'East' | 'West' | null; export type VsDivision = 'null' | 'Atlantic' | 'Central' | 'Northwest' | 'Pacific' | 'Southeast' | 'Southwest' | 'East' | 'West' | null; export type VsPlayerIDList = string; export type GetApiPlayercompareArgs = {Conference?: Conference | undefined, DateFrom?: DateFrom | undefined, DateTo?: DateTo | undefined, Division?: Division | undefined, GameSegment: GameSegment, LastNGames?: LastNGames | undefined, LeagueID: LeagueID, Location?: Location | undefined, MeasureType: MeasureType, Month?: Month | undefined, OpponentTeamID?: OpponentTeamID | undefined, Outcome?: Outcome | undefined, PaceAdjust: PaceAdjust, PerMode: PerMode, Period: Period, PlayerIDList: PlayerIDList, PlusMinus: PlusMinus, Rank: Rank, Season: Season, SeasonSegment?: SeasonSegment | undefined, SeasonType: SeasonType, ShotClockRange?: ShotClockRange | undefined, VsConference?: VsConference | undefined, VsDivision?: VsDivision | undefined, VsPlayerIDList: VsPlayerIDList}; export type GetApiPlayercompareOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPlayercompareResponse = z.object({parameters: z.object({Conference: z.literal("$request.query.Conference").optional(), DateFrom: z.literal("$request.query.DateFrom").optional(), DateTo: z.literal("$request.query.DateTo").optional(), Division: z.literal("$request.query.Division").optional(), GameSegment: z.literal("$request.query.GameSegment").optional(), LastNGames: z.literal("$request.query.LastNGames").optional(), LeagueID: z.literal("$request.query.LeagueID").optional(), Location: z.literal("$request.query.Location").optional(), MeasureType: z.literal("$request.query.MeasureType").optional(), Month: z.literal("$request.query.Month").optional(), OpponentTeamID: z.literal("$request.query.OpponentTeamID").optional(), Outcome: z.literal("$request.query.Outcome").optional(), PaceAdjust: z.literal("$request.query.PaceAdjust").optional(), PerMode: z.literal("$request.query.PerMode").optional(), Period: z.literal("$request.query.Period").optional(), PlayerIDList: z.literal("$request.query.PlayerIDList").optional(), PlusMinus: z.literal("$request.query.PlusMinus").optional(), Rank: z.literal("$request.query.Rank").optional(), Season: z.literal("$request.query.Season").optional(), SeasonSegment: z.literal("$request.query.SeasonSegment").optional(), SeasonType: z.literal("$request.query.SeasonType").optional(), ShotClockRange: z.literal("$request.query.ShotClockRange").optional(), VsConference: z.literal("$request.query.VsConference").optional(), VsDivision: z.literal("$request.query.VsDivision").optional(), VsPlayerIDList: z.literal("$request.query.VsPlayerIDList").optional()}).optional(), resource: z.literal("playercompare").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 }; /** * PlayerCompare * * GET /playercompare */ export const getApiPlayercompare = async (args: GetApiPlayercompareArgs, options?: GetApiPlayercompareOptions) => { 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.Conference != null) query.append("Conference", String(args.Conference)) if (args.DateFrom != null) query.append("DateFrom", String(args.DateFrom)) if (args.DateTo != null) query.append("DateTo", String(args.DateTo)) if (args.Division != null) query.append("Division", String(args.Division)) if (args.GameSegment != null) query.append("GameSegment", String(args.GameSegment)) if (args.LastNGames != null) query.append("LastNGames", String(args.LastNGames)) if (args.LeagueID != null) query.append("LeagueID", String(args.LeagueID)) if (args.Location != null) query.append("Location", String(args.Location)) if (args.MeasureType != null) query.append("MeasureType", String(args.MeasureType)) if (args.Month != null) query.append("Month", String(args.Month)) if (args.OpponentTeamID != null) query.append("OpponentTeamID", String(args.OpponentTeamID)) if (args.Outcome != null) query.append("Outcome", String(args.Outcome)) if (args.PaceAdjust != null) query.append("PaceAdjust", String(args.PaceAdjust)) if (args.PerMode != null) query.append("PerMode", String(args.PerMode)) if (args.Period != null) query.append("Period", String(args.Period)) if (args.PlayerIDList != null) query.append("PlayerIDList", String(args.PlayerIDList)) if (args.PlusMinus != null) query.append("PlusMinus", String(args.PlusMinus)) if (args.Rank != null) query.append("Rank", String(args.Rank)) if (args.Season != null) query.append("Season", String(args.Season)) if (args.SeasonSegment != null) query.append("SeasonSegment", String(args.SeasonSegment)) if (args.SeasonType != null) query.append("SeasonType", String(args.SeasonType)) if (args.ShotClockRange != null) query.append("ShotClockRange", String(args.ShotClockRange)) if (args.VsConference != null) query.append("VsConference", String(args.VsConference)) if (args.VsDivision != null) query.append("VsDivision", String(args.VsDivision)) if (args.VsPlayerIDList != null) query.append("VsPlayerIDList", String(args.VsPlayerIDList)) const url = `${baseUrl.replace(/\/+$/, '')}/playercompare${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(getApiPlayercompareResponse, await res.json()) };