import {z} from 'zod' export type AheadBehind = 'Ahead or Behind' | 'Behind or Tied' | 'Ahead or Tied'; export type ClutchTime = 'Last 5 Minutes' | 'Last 4 Minutes' | 'Last 3 Minutes' | 'Last 2 Minutes' | 'Last 1 Minute' | 'Last 30 Seconds' | 'Last 10 Seconds'; export type ContextFilter = string | null; export type ContextMeasure = 'PTS' | 'FGM' | 'FGA' | 'FGPCT' | 'FG3M' | 'FG3A' | 'FG3PCT' | 'PF' | 'EFGPCT' | 'TSPCT' | 'PTSFB' | 'PTSOFFTOV' | 'PTS2NDCHANCE' | 'PF'; export type DateFrom = string | null; export type DateTo = string | null; export type EndPeriod = string; export type EndRange = string; export type GameID = string; 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 Month = string | null; export type OpponentTeamID = string | null; export type Outcome = 'null' | 'W' | 'L' | null; export type Period = string; export type PlayerID = string; export type PlayerPosition = 'null' | 'F' | 'C' | 'G' | 'C-F' | 'F-C' | 'F-G' | 'G-F' | null; export type PointDiff = string; export type Position = 'null' | 'Guard' | 'Forward' | 'Center' | null; export type RangeType = string; export type RookieYear = string | null; export type Season = string; export type SeasonSegment = 'null' | 'Post All-Star' | 'Pre All-Star' | null; export type SeasonType = string; export type StartPeriod = string; export type StartRange = string; export type TeamID = string; export type VsConference = 'null' | 'East' | 'West' | null; export type VsDivision = 'null' | 'Atlantic' | 'Central' | 'Northwest' | 'Pacific' | 'Southeast' | 'Southwest' | 'East' | 'West' | null; export type GetApiShotchartdetailArgs = {AheadBehind: AheadBehind, ClutchTime: ClutchTime, ContextFilter?: ContextFilter | undefined, ContextMeasure: ContextMeasure, DateFrom?: DateFrom | undefined, DateTo?: DateTo | undefined, EndPeriod: EndPeriod, EndRange: EndRange, GameID: GameID, GameSegment: GameSegment, LastNGames?: LastNGames | undefined, LeagueID: LeagueID, Location?: Location | undefined, Month?: Month | undefined, OpponentTeamID?: OpponentTeamID | undefined, Outcome?: Outcome | undefined, Period: Period, PlayerID: PlayerID, PlayerPosition?: PlayerPosition | undefined, PointDiff: PointDiff, Position?: Position | undefined, RangeType: RangeType, RookieYear?: RookieYear | undefined, Season: Season, SeasonSegment?: SeasonSegment | undefined, SeasonType: SeasonType, StartPeriod: StartPeriod, StartRange: StartRange, TeamID?: TeamID | undefined, VsConference?: VsConference | undefined, VsDivision?: VsDivision | undefined}; export type GetApiShotchartdetailOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiShotchartdetailResponse = z.object({parameters: z.object({AheadBehind: z.literal("$request.query.AheadBehind").optional(), ClutchTime: z.literal("$request.query.ClutchTime").optional(), ContextFilter: z.literal("$request.query.ContextFilter").optional(), ContextMeasure: z.literal("$request.query.ContextMeasure").optional(), DateFrom: z.literal("$request.query.DateFrom").optional(), DateTo: z.literal("$request.query.DateTo").optional(), EndPeriod: z.literal("$request.query.EndPeriod").optional(), EndRange: z.literal("$request.query.EndRange").optional(), GameID: z.literal("$request.query.GameID").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(), Month: z.literal("$request.query.Month").optional(), OpponentTeamID: z.literal("$request.query.OpponentTeamID").optional(), Outcome: z.literal("$request.query.Outcome").optional(), Period: z.literal("$request.query.Period").optional(), PlayerID: z.literal("$request.query.PlayerID").optional(), PlayerPosition: z.literal("$request.query.PlayerPosition").optional(), PointDiff: z.literal("$request.query.PointDiff").optional(), Position: z.literal("$request.query.Position").optional(), RangeType: z.literal("$request.query.RangeType").optional(), RookieYear: z.literal("$request.query.RookieYear").optional(), Season: z.literal("$request.query.Season").optional(), SeasonSegment: z.literal("$request.query.SeasonSegment").optional(), SeasonType: z.literal("$request.query.SeasonType").optional(), StartPeriod: z.literal("$request.query.StartPeriod").optional(), StartRange: z.literal("$request.query.StartRange").optional(), TeamID: z.literal("$request.query.TeamID").optional(), VsConference: z.literal("$request.query.VsConference").optional(), VsDivision: z.literal("$request.query.VsDivision").optional()}).optional(), resource: z.literal("shotchartdetail").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 }; /** * ShotChartDetail * * GET /shotchartdetail */ export const getApiShotchartdetail = async (args: GetApiShotchartdetailArgs, options?: GetApiShotchartdetailOptions) => { 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.AheadBehind != null) query.append("AheadBehind", String(args.AheadBehind)) if (args.ClutchTime != null) query.append("ClutchTime", String(args.ClutchTime)) if (args.ContextFilter != null) query.append("ContextFilter", String(args.ContextFilter)) if (args.ContextMeasure != null) query.append("ContextMeasure", String(args.ContextMeasure)) if (args.DateFrom != null) query.append("DateFrom", String(args.DateFrom)) if (args.DateTo != null) query.append("DateTo", String(args.DateTo)) if (args.EndPeriod != null) query.append("EndPeriod", String(args.EndPeriod)) if (args.EndRange != null) query.append("EndRange", String(args.EndRange)) if (args.GameID != null) query.append("GameID", String(args.GameID)) 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.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.Period != null) query.append("Period", String(args.Period)) if (args.PlayerID != null) query.append("PlayerID", String(args.PlayerID)) if (args.PlayerPosition != null) query.append("PlayerPosition", String(args.PlayerPosition)) if (args.PointDiff != null) query.append("PointDiff", String(args.PointDiff)) if (args.Position != null) query.append("Position", String(args.Position)) if (args.RangeType != null) query.append("RangeType", String(args.RangeType)) if (args.RookieYear != null) query.append("RookieYear", String(args.RookieYear)) 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.StartPeriod != null) query.append("StartPeriod", String(args.StartPeriod)) if (args.StartRange != null) query.append("StartRange", String(args.StartRange)) if (args.TeamID != null) query.append("TeamID", String(args.TeamID)) if (args.VsConference != null) query.append("VsConference", String(args.VsConference)) if (args.VsDivision != null) query.append("VsDivision", String(args.VsDivision)) const url = `${baseUrl.replace(/\/+$/, '')}/shotchartdetail${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(getApiShotchartdetailResponse, await res.json()) };