import {z} from 'zod' export type EndPeriod = string; export type EndRange = string; export type GameID = string; export type RangeType = string; export type StartPeriod = string; export type StartRange = string; export type GetApiBoxscoreusagev2Args = {EndPeriod: EndPeriod, EndRange: EndRange, GameID: GameID, RangeType: RangeType, StartPeriod: StartPeriod, StartRange: StartRange}; export type GetApiBoxscoreusagev2Options = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBoxscoreusagev2Response = z.object({parameters: z.object({EndPeriod: z.literal("$request.query.EndPeriod").optional(), EndRange: z.literal("$request.query.EndRange").optional(), GameID: z.literal("$request.query.GameID").optional(), RangeType: z.literal("$request.query.RangeType").optional(), StartPeriod: z.literal("$request.query.StartPeriod").optional(), StartRange: z.literal("$request.query.StartRange").optional()}).optional(), resource: z.literal("boxscoreusagev2").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 }; /** * BoxScoreUsageV2 * * GET /boxscoreusagev2 */ export const getApiBoxscoreusagev2 = async (args: GetApiBoxscoreusagev2Args, options?: GetApiBoxscoreusagev2Options) => { 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.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.RangeType != null) query.append("RangeType", String(args.RangeType)) if (args.StartPeriod != null) query.append("StartPeriod", String(args.StartPeriod)) if (args.StartRange != null) query.append("StartRange", String(args.StartRange)) const url = `${baseUrl.replace(/\/+$/, '')}/boxscoreusagev2${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(getApiBoxscoreusagev2Response, await res.json()) };