import {z} from 'zod' export type GetApiTeamTeamKeyMatchesYearSimpleArgs = {'If-None-Match'?: string | undefined, team_key: string, year: number}; export type GetApiTeamTeamKeyMatchesYearSimpleOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const compLevel = z.enum(["pm", "qm", "ef", "qf", "sf", "f"]); export const matchAlliance = z.object({score: z.number().int(), team_keys: z.array(z.string()), surrogate_team_keys: z.array(z.string()), dq_team_keys: z.array(z.string())}); export const allianceColor = z.enum(["red", "blue", ""]); export const matchSimple = z.object({key: z.string(), comp_level: compLevel, set_number: z.number().int(), match_number: z.number().int(), alliances: z.object({red: matchAlliance, blue: matchAlliance}), winning_alliance: allianceColor, event_key: z.string(), time: z.number().int().nullable(), predicted_time: z.number().int().nullable(), actual_time: z.number().int().nullable()}); export const getApiTeamTeamKeyMatchesYearSimpleResponse = z.array(matchSimple); 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 }; /** * Gets a short-form list of matches for the given team and year. * * GET /team/{team_key}/matches/{year}/simple */ export const getApiTeamTeamKeyMatchesYearSimple = async (args: GetApiTeamTeamKeyMatchesYearSimpleArgs, options?: GetApiTeamTeamKeyMatchesYearSimpleOptions) => { const {baseUrl = "https://www.thebluealliance.com/api/v3", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['If-None-Match'] != null) headers.set("If-None-Match", String(args['If-None-Match'])) if (apiKey !== undefined) headers.set("X-TBA-Auth-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/team/${encodeURIComponent(String(args.team_key))}/matches/${encodeURIComponent(String(args.year))}/simple${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(getApiTeamTeamKeyMatchesYearSimpleResponse, await res.json()) };