import {z} from 'zod' export type ActivePlayers = 'Y' | 'N'; export type DateFrom = string | null; export type DateTo = string | 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 PORound = string | null; export type PlayerID = string; export type Position = 'null' | 'Guard' | 'Forward' | 'Center' | null; export type Season = string; export type SeasonSegment = 'null' | 'Post All-Star' | 'Pre All-Star' | null; export type SeasonType = string; export type TeamID = string; export type TodaysOpponent = string; export type TodaysPlayers = 'Y' | 'N'; export type VsConference = 'null' | 'East' | 'West' | null; export type VsDivision = 'null' | 'Atlantic' | 'Central' | 'Northwest' | 'Pacific' | 'Southeast' | 'Southwest' | 'East' | 'West' | null; export type GetApiFantasywidgetArgs = {ActivePlayers: ActivePlayers, DateFrom?: DateFrom | undefined, DateTo?: DateTo | undefined, LastNGames?: LastNGames | undefined, LeagueID: LeagueID, Location?: Location | undefined, Month?: Month | undefined, OpponentTeamID?: OpponentTeamID | undefined, PORound?: PORound | undefined, PlayerID: PlayerID, Position?: Position | undefined, Season: Season, SeasonSegment?: SeasonSegment | undefined, SeasonType: SeasonType, TeamID?: TeamID | undefined, TodaysOpponent: TodaysOpponent, TodaysPlayers: TodaysPlayers, VsConference?: VsConference | undefined, VsDivision?: VsDivision | undefined}; export type GetApiFantasywidgetOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiFantasywidgetResponse = z.object({parameters: z.object({ActivePlayers: z.literal("$request.query.ActivePlayers").optional(), DateFrom: z.literal("$request.query.DateFrom").optional(), DateTo: z.literal("$request.query.DateTo").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(), PORound: z.literal("$request.query.PORound").optional(), PlayerID: z.literal("$request.query.PlayerID").optional(), Position: z.literal("$request.query.Position").optional(), Season: z.literal("$request.query.Season").optional(), SeasonSegment: z.literal("$request.query.SeasonSegment").optional(), SeasonType: z.literal("$request.query.SeasonType").optional(), TeamID: z.literal("$request.query.TeamID").optional(), TodaysOpponent: z.literal("$request.query.TodaysOpponent").optional(), TodaysPlayers: z.literal("$request.query.TodaysPlayers").optional(), VsConference: z.literal("$request.query.VsConference").optional(), VsDivision: z.literal("$request.query.VsDivision").optional()}).optional(), resource: z.literal("fantasywidget").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 }; /** * FantasyWidget * * GET /fantasywidget */ export const getApiFantasywidget = async (args: GetApiFantasywidgetArgs, options?: GetApiFantasywidgetOptions) => { 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.ActivePlayers != null) query.append("ActivePlayers", String(args.ActivePlayers)) if (args.DateFrom != null) query.append("DateFrom", String(args.DateFrom)) if (args.DateTo != null) query.append("DateTo", String(args.DateTo)) 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.PORound != null) query.append("PORound", String(args.PORound)) if (args.PlayerID != null) query.append("PlayerID", String(args.PlayerID)) if (args.Position != null) query.append("Position", String(args.Position)) 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.TeamID != null) query.append("TeamID", String(args.TeamID)) if (args.TodaysOpponent != null) query.append("TodaysOpponent", String(args.TodaysOpponent)) if (args.TodaysPlayers != null) query.append("TodaysPlayers", String(args.TodaysPlayers)) if (args.VsConference != null) query.append("VsConference", String(args.VsConference)) if (args.VsDivision != null) query.append("VsDivision", String(args.VsDivision)) const url = `${baseUrl.replace(/\/+$/, '')}/fantasywidget${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(getApiFantasywidgetResponse, await res.json()) };