import {z} from 'zod' export type Counter = string; export type DateFrom = string | null; export type DateTo = string | null; export type Direction = 'ASC' | 'DESC'; export type LeagueID = string; export type PlayerOrTeam = 'Player' | 'Team'; export type Season = string; export type SeasonType = string; export type Sorter = 'FGM' | 'FGA' | 'FGPCT' | 'FG3M' | 'FG3A' | 'FG3PCT' | 'FTM' | 'FTA' | 'FTPCT' | 'OREB' | 'DREB' | 'AST' | 'STL' | 'BLK' | 'TOV' | 'REB' | 'PTS' | 'DATE'; export type GetApiLeaguegamelogArgs = {Counter: Counter, DateFrom?: DateFrom | undefined, DateTo?: DateTo | undefined, Direction: Direction, LeagueID: LeagueID, PlayerOrTeam: PlayerOrTeam, Season: Season, SeasonType: SeasonType, Sorter: Sorter}; export type GetApiLeaguegamelogOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiLeaguegamelogResponse = z.object({parameters: z.object({Counter: z.literal("$request.query.Counter").optional(), DateFrom: z.literal("$request.query.DateFrom").optional(), DateTo: z.literal("$request.query.DateTo").optional(), Direction: z.literal("$request.query.Direction").optional(), LeagueID: z.literal("$request.query.LeagueID").optional(), PlayerOrTeam: z.literal("$request.query.PlayerOrTeam").optional(), Season: z.literal("$request.query.Season").optional(), SeasonType: z.literal("$request.query.SeasonType").optional(), Sorter: z.literal("$request.query.Sorter").optional()}).optional(), resource: z.literal("leaguegamelog").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 }; /** * LeagueGameLog * * GET /leaguegamelog */ export const getApiLeaguegamelog = async (args: GetApiLeaguegamelogArgs, options?: GetApiLeaguegamelogOptions) => { 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.Counter != null) query.append("Counter", String(args.Counter)) if (args.DateFrom != null) query.append("DateFrom", String(args.DateFrom)) if (args.DateTo != null) query.append("DateTo", String(args.DateTo)) if (args.Direction != null) query.append("Direction", String(args.Direction)) if (args.LeagueID != null) query.append("LeagueID", String(args.LeagueID)) if (args.PlayerOrTeam != null) query.append("PlayerOrTeam", String(args.PlayerOrTeam)) if (args.Season != null) query.append("Season", String(args.Season)) if (args.SeasonType != null) query.append("SeasonType", String(args.SeasonType)) if (args.Sorter != null) query.append("Sorter", String(args.Sorter)) const url = `${baseUrl.replace(/\/+$/, '')}/leaguegamelog${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(getApiLeaguegamelogResponse, await res.json()) };