import {z} from 'zod' export type GetApiTeamTeamKeyEventsYearSimpleArgs = {'If-None-Match'?: string | undefined, team_key: string, year: number}; export type GetApiTeamTeamKeyEventsYearSimpleOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const eventType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(99), z.literal(100), z.literal(-1)]); export const district = z.object({abbreviation: z.string(), display_name: z.string(), key: z.string(), year: z.number().int(), official_advancement_counts: z.object({dcmp: z.number().int(), cmp: z.number().int()})}); export const eventSimple = z.object({key: z.string(), name: z.string(), event_code: z.string(), event_type: eventType, district: district.nullable(), city: z.string().nullable(), state_prov: z.string().nullable(), country: z.string().nullable(), start_date: z.string(), end_date: z.string(), year: z.number().int()}); export const getApiTeamTeamKeyEventsYearSimpleResponse = z.array(eventSimple); 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 events this team has competed at in the given year. * * GET /team/{team_key}/events/{year}/simple */ export const getApiTeamTeamKeyEventsYearSimple = async (args: GetApiTeamTeamKeyEventsYearSimpleArgs, options?: GetApiTeamTeamKeyEventsYearSimpleOptions) => { 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))}/events/${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(getApiTeamTeamKeyEventsYearSimpleResponse, await res.json()) };