import {z} from 'zod' export type GetApiTeamTeamKeyEventsArgs = {'If-None-Match'?: string | undefined, team_key: string}; export type GetApiTeamTeamKeyEventsOptions = {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 webcastStatus = z.enum(["unknown", "online", "offline"]); export const webcast = z.object({type: z.enum(["youtube", "twitch", "ustream", "iframe", "html5", "rtmp", "livestream", "direct_link", "mms", "justin", "stemtv", "dacast"]), channel: z.string(), date: z.string().nullable().optional(), file: z.string().nullable().optional(), status: webcastStatus.nullable().optional(), stream_title: z.string().nullable().optional(), viewer_count: z.number().int().nullable().optional()}); export const playoffType = z.union([z.literal(1), z.literal(0), z.literal(2), z.literal(9), z.literal(3), z.literal(4), z.literal(5), z.literal(10), z.literal(11), z.literal(6), z.literal(7), z.literal(8)]); export const event = 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(), short_name: z.string().nullable(), event_type_string: z.string(), week: z.number().int().nullable(), address: z.string().nullable(), postal_code: z.string().nullable(), gmaps_place_id: z.string().nullable(), gmaps_url: z.string().nullable(), lat: z.number().nullable(), lng: z.number().nullable(), location_name: z.string().nullable(), timezone: z.string().nullable(), website: z.string().nullable(), first_event_id: z.string().nullable(), first_event_code: z.string().nullable(), webcasts: z.array(webcast), division_keys: z.array(z.string()), parent_event_key: z.string().nullable(), playoff_type: playoffType.nullable(), playoff_type_string: z.string().nullable(), remap_teams: z.record(z.string(), z.string()).nullable()}); export const getApiTeamTeamKeyEventsResponse = z.array(event); 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 list of all events this team has competed at. * * GET /team/{team_key}/events */ export const getApiTeamTeamKeyEvents = async (args: GetApiTeamTeamKeyEventsArgs, options?: GetApiTeamTeamKeyEventsOptions) => { 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${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(getApiTeamTeamKeyEventsResponse, await res.json()) };