import {z} from 'zod' export type GetApiTeamTeamKeyHistoryArgs = {'If-None-Match'?: string | undefined, team_key: string}; export type GetApiTeamTeamKeyHistoryOptions = {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 awardType = 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(8), z.literal(9), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(17), z.literal(18), z.literal(19), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(24), z.literal(25), z.literal(26), z.literal(27), z.literal(28), z.literal(29), z.literal(30), z.literal(31), z.literal(32), z.literal(33), z.literal(34), z.literal(35), z.literal(36), z.literal(37), z.literal(38), z.literal(39), z.literal(40), z.literal(41), z.literal(42), z.literal(43), z.literal(44), z.literal(45), z.literal(46), z.literal(47), z.literal(48), z.literal(49), z.literal(50), z.literal(51), z.literal(52), z.literal(53), z.literal(54), z.literal(55), z.literal(56), z.literal(57), z.literal(58), z.literal(59), z.literal(60), z.literal(61), z.literal(62), z.literal(63), z.literal(64), z.literal(65), z.literal(66), z.literal(67), z.literal(68), z.literal(69), z.literal(70), z.literal(71), z.literal(72), z.literal(73), z.literal(74), z.literal(75), z.literal(76), z.literal(77), z.literal(78), z.literal(79), z.literal(80), z.literal(81), z.literal(82), z.literal(83)]); export const awardRecipient = z.object({team_key: z.string().nullable(), awardee: z.string().nullable()}); export const award = z.object({name: z.string(), award_type: awardType, event_key: z.string(), recipient_list: z.array(awardRecipient), year: z.number().int()}); export const history = z.object({events: z.array(event), awards: z.array(award)}); 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 the history for the team referenced by the given key, including their events and awards. * * GET /team/{team_key}/history */ export const getApiTeamTeamKeyHistory = async (args: GetApiTeamTeamKeyHistoryArgs, options?: GetApiTeamTeamKeyHistoryOptions) => { 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))}/history${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(history, await res.json()) };