tbarequest 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +36 -0
  3. package/dist/index.d.ts +4 -0
  4. package/dist/index.js +46 -0
  5. package/dist/types/endpoints/districts.d.ts +173 -0
  6. package/dist/types/endpoints/districts.js +50 -0
  7. package/dist/types/endpoints/events.d.ts +13351 -0
  8. package/dist/types/endpoints/events.js +138 -0
  9. package/dist/types/endpoints/index.d.ts +26 -0
  10. package/dist/types/endpoints/index.js +25 -0
  11. package/dist/types/endpoints/insights.d.ts +34 -0
  12. package/dist/types/endpoints/insights.js +12 -0
  13. package/dist/types/endpoints/matches.d.ts +12944 -0
  14. package/dist/types/endpoints/matches.js +12 -0
  15. package/dist/types/endpoints/regionalAdvancements.d.ts +37 -0
  16. package/dist/types/endpoints/regionalAdvancements.js +12 -0
  17. package/dist/types/endpoints/status.d.ts +34 -0
  18. package/dist/types/endpoints/status.js +16 -0
  19. package/dist/types/endpoints/teams.d.ts +26447 -0
  20. package/dist/types/endpoints/teams.js +139 -0
  21. package/dist/types/index.d.ts +7 -0
  22. package/dist/types/index.js +1 -0
  23. package/dist/types/schemas/districts.d.ts +26 -0
  24. package/dist/types/schemas/districts.js +26 -0
  25. package/dist/types/schemas/events.d.ts +233 -0
  26. package/dist/types/schemas/events.js +107 -0
  27. package/dist/types/schemas/insights.d.ts +26 -0
  28. package/dist/types/schemas/insights.js +22 -0
  29. package/dist/types/schemas/matches.d.ts +14877 -0
  30. package/dist/types/schemas/matches.js +573 -0
  31. package/dist/types/schemas/regionalAdvancements.d.ts +29 -0
  32. package/dist/types/schemas/regionalAdvancements.js +23 -0
  33. package/dist/types/schemas/status.d.ts +5 -0
  34. package/dist/types/schemas/status.js +5 -0
  35. package/dist/types/schemas/teams.d.ts +51 -0
  36. package/dist/types/schemas/teams.js +41 -0
  37. package/dist/utils.d.ts +11 -0
  38. package/dist/utils.js +9 -0
  39. package/package.json +26 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Michael Kelbick
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # TBArequest
2
+
3
+ > Unofficial wrapper to request data from The Blue Alliance API
4
+
5
+
6
+ > [!WARNING]
7
+ > This package relies on a beta version of Zod, so it might be unstable until Zod 4.0 comes out.
8
+
9
+ ## Install
10
+ ```bash
11
+ npm install tbarequest
12
+ ```
13
+
14
+ ## Usage
15
+ Use the `createTBACaller` function to create a function that can call the API.
16
+
17
+ ```ts
18
+ import { createTBACaller } from "./index";
19
+
20
+ const tba = createTBACaller("api_key")
21
+
22
+ const status = tba("/status")
23
+ const team = tba("/teams/{team_key}", 1014)
24
+ ```
25
+
26
+ The `tba` function can call an endpoint and require all necessary arguments, and then return the results that are type-safe.
27
+
28
+ Find all the endpoints [here](https://www.thebluealliance.com/apidocs/v3).
29
+
30
+ ## Coverage
31
+ The wrapper covers all endpoints besides endpoints relating to Zebra Motionworks and Timeseries.
32
+ The endpoints not covered include:
33
+ * /event/{event_key}/matches/timeseries
34
+ * /event/{event_key}/matches/timeseries
35
+ * /match/{match_key}/timeseries
36
+ * /match/{match_key}/zebra_motionworks
@@ -0,0 +1,4 @@
1
+ import { z } from "zod";
2
+ import { Result } from "./utils.js";
3
+ import { TBAEndpoint, TBAEndpoints } from "./types/endpoints/index.js";
4
+ export declare function createTBACaller(api_key: string): <T extends TBAEndpoint>(endpoint: T, ...args: z.infer<TBAEndpoints[T]["arguments"]>) => Promise<Result<z.infer<TBAEndpoints[T]["schema"]>>>;
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ import { tryCatch } from "./utils.js";
2
+ import { endpoints } from "./types/endpoints/index.js";
3
+ /*
4
+ * Creates a function that can call the TBA api.
5
+ * @see https://www.thebluealliance.com/apidocs/v3
6
+ * @param api_key The api key given to you from The Blue Alliance.
7
+ * @returns A function that can request from any endpoint in the TBA api. Go to the TBA api docs to see more information on endpoints and arguments required.
8
+ */
9
+ export function createTBACaller(api_key) {
10
+ return async (endpoint, ...args) => await TBA(endpoint, api_key, ...args);
11
+ }
12
+ async function TBA(endpoint, api_key, ...args) {
13
+ let numArg = -1;
14
+ // Fills all the arguments that are needed.
15
+ const filledEndpoint = endpoint.replace(/{(.+?)}/g, () => {
16
+ numArg++;
17
+ return args[numArg].toString();
18
+ });
19
+ const result = await tryCatch(fetch(`https://www.thebluealliance.com/api/v3${filledEndpoint}`, {
20
+ headers: {
21
+ "X-TBA-Auth-Key": api_key,
22
+ },
23
+ }));
24
+ if (result.error)
25
+ return {
26
+ data: null, error: result.error,
27
+ };
28
+ if (result.data.status !== 200)
29
+ return {
30
+ data: null,
31
+ error: new Error(`${result.data.status} - ${result.data.statusText}`),
32
+ };
33
+ const json = await tryCatch(result.data.json());
34
+ if (json.error)
35
+ return {
36
+ data: null,
37
+ error: new Error(`JSON didn't parse for endpoint ${endpoint}. Please contact the developers of the TBArequest package with this error: ${json.error.message}`),
38
+ };
39
+ const schema = endpoints[endpoint].schema.safeParse(json.data);
40
+ if (!schema.success)
41
+ return {
42
+ data: null,
43
+ error: new Error(`Schema for endpoint ${endpoint} didn't work. Please contact the developers of the TBArequest package with this error: ${schema.error.message}`),
44
+ };
45
+ return { data: schema.data, error: null };
46
+ }
@@ -0,0 +1,173 @@
1
+ import { z } from "zod";
2
+ export declare const districtEndpoints: {
3
+ "/districts/{year}": {
4
+ schema: z.ZodArray<z.ZodObject<{
5
+ abbreviation: z.ZodString;
6
+ display_name: z.ZodString;
7
+ key: z.ZodString;
8
+ year: z.ZodInt;
9
+ }, {}>>;
10
+ arguments: z.ZodTuple<[z.ZodInt], null>;
11
+ };
12
+ "/district/{district_abbreviation}/history": {
13
+ schema: z.ZodArray<z.ZodObject<{
14
+ abbreviation: z.ZodString;
15
+ display_name: z.ZodString;
16
+ key: z.ZodString;
17
+ year: z.ZodInt;
18
+ }, {}>>;
19
+ arguments: z.ZodTuple<[z.ZodString], null>;
20
+ };
21
+ "/district/{district_key}/events": {
22
+ schema: z.ZodArray<z.ZodObject<{
23
+ key: z.ZodString;
24
+ name: z.ZodString;
25
+ city: z.ZodNullable<z.ZodString>;
26
+ state_prov: z.ZodNullable<z.ZodString>;
27
+ country: z.ZodNullable<z.ZodString>;
28
+ postal_code: z.ZodNullable<z.ZodString>;
29
+ website: z.ZodNullable<z.ZodString>;
30
+ year: z.ZodInt;
31
+ event_code: z.ZodString;
32
+ event_type: z.ZodInt;
33
+ district: z.ZodNullable<z.ZodObject<{
34
+ abbreviation: z.ZodString;
35
+ display_name: z.ZodString;
36
+ key: z.ZodString;
37
+ year: z.ZodInt;
38
+ }, {}>>;
39
+ start_date: z.coerce.ZodCoercedDate;
40
+ end_date: z.coerce.ZodCoercedDate;
41
+ webcasts: z.ZodArray<z.ZodObject<{
42
+ type: z.ZodEnum<{
43
+ youtube: "youtube";
44
+ twitch: "twitch";
45
+ ustream: "ustream";
46
+ iframe: "iframe";
47
+ html5: "html5";
48
+ rtmp: "rtmp";
49
+ livestream: "livestream";
50
+ direct_link: "direct_link";
51
+ mms: "mms";
52
+ justin: "justin";
53
+ stemtv: "stemtv";
54
+ dacast: "dacast";
55
+ }>;
56
+ channel: z.ZodString;
57
+ date: z.ZodOptional<z.ZodNullable<z.coerce.ZodCoercedDate>>;
58
+ file: z.ZodOptional<z.ZodNullable<z.ZodString>>;
59
+ }, {}>>;
60
+ division_keys: z.ZodArray<z.ZodString>;
61
+ short_name: z.ZodNullable<z.ZodString>;
62
+ event_type_string: z.ZodString;
63
+ week: z.ZodNullable<z.ZodNumber>;
64
+ address: z.ZodNullable<z.ZodString>;
65
+ gmaps_place_id: z.ZodNullable<z.ZodString>;
66
+ gmaps_url: z.ZodNullable<z.ZodString>;
67
+ lat: z.ZodNullable<z.ZodNumber>;
68
+ lng: z.ZodNullable<z.ZodNumber>;
69
+ location_name: z.ZodNullable<z.ZodString>;
70
+ timezone: z.ZodNullable<z.ZodString>;
71
+ first_event_id: z.ZodNullable<z.ZodString>;
72
+ first_event_code: z.ZodNullable<z.ZodString>;
73
+ parent_event_key: z.ZodNullable<z.ZodString>;
74
+ playoff_type: z.ZodNullable<z.ZodInt>;
75
+ playoff_type_string: z.ZodNullable<z.ZodString>;
76
+ }, {}>>;
77
+ arguments: z.ZodTuple<[z.ZodString], null>;
78
+ };
79
+ "/district/{district_key}/awards": {
80
+ schema: z.ZodArray<z.ZodObject<{
81
+ name: z.ZodString;
82
+ award_type: z.ZodInt;
83
+ event_key: z.ZodString;
84
+ recipient_list: z.ZodArray<z.ZodObject<{
85
+ team_key: z.ZodNullable<z.ZodString>;
86
+ awardee: z.ZodNullable<z.ZodString>;
87
+ }, {}>>;
88
+ year: z.ZodInt;
89
+ }, {}>>;
90
+ arguments: z.ZodTuple<[z.ZodString], null>;
91
+ };
92
+ "/district/{district_key}/events/simple": {
93
+ schema: z.ZodArray<z.ZodObject<{
94
+ key: z.ZodString;
95
+ name: z.ZodString;
96
+ event_code: z.ZodString;
97
+ event_type: z.ZodInt;
98
+ district: z.ZodNullable<z.ZodObject<{
99
+ abbreviation: z.ZodString;
100
+ display_name: z.ZodString;
101
+ key: z.ZodString;
102
+ year: z.ZodInt;
103
+ }, {}>>;
104
+ city: z.ZodNullable<z.ZodString>;
105
+ state_prov: z.ZodNullable<z.ZodString>;
106
+ country: z.ZodNullable<z.ZodString>;
107
+ start_date: z.coerce.ZodCoercedDate;
108
+ end_date: z.coerce.ZodCoercedDate;
109
+ year: z.ZodInt;
110
+ }, {}>>;
111
+ arguments: z.ZodTuple<[z.ZodString], null>;
112
+ };
113
+ "/district/{district_key}/events/keys": {
114
+ schema: z.ZodArray<z.ZodString>;
115
+ arguments: z.ZodTuple<[z.ZodString], null>;
116
+ };
117
+ "/district/{district_key}/teams": {
118
+ schema: z.ZodArray<z.ZodObject<{
119
+ key: z.ZodString;
120
+ team_number: z.ZodInt;
121
+ nickname: z.ZodString;
122
+ name: z.ZodString;
123
+ city: z.ZodNullable<z.ZodString>;
124
+ state_prov: z.ZodNullable<z.ZodString>;
125
+ country: z.ZodNullable<z.ZodString>;
126
+ postal_code: z.ZodNullable<z.ZodString>;
127
+ website: z.ZodOptional<z.ZodNullable<z.ZodString>>;
128
+ rookie_year: z.ZodNullable<z.ZodNumber>;
129
+ }, {}>>;
130
+ arguments: z.ZodTuple<[z.ZodString], null>;
131
+ };
132
+ "/district/{district_key}/teams/simple": {
133
+ schema: z.ZodArray<z.ZodObject<{
134
+ key: z.ZodString;
135
+ team_number: z.ZodInt;
136
+ nickname: z.ZodString;
137
+ name: z.ZodString;
138
+ city: z.ZodNullable<z.ZodString>;
139
+ state_prov: z.ZodNullable<z.ZodString>;
140
+ country: z.ZodNullable<z.ZodString>;
141
+ }, {}>>;
142
+ arguments: z.ZodTuple<[z.ZodString], null>;
143
+ };
144
+ "/district/{district_key}/teams/keys": {
145
+ schema: z.ZodArray<z.ZodString>;
146
+ arguments: z.ZodTuple<[z.ZodString], null>;
147
+ };
148
+ "/district/{district_key}/rankings": {
149
+ schema: z.ZodNullable<z.ZodArray<z.ZodObject<{
150
+ team_key: z.ZodString;
151
+ rank: z.ZodInt;
152
+ rookie_bonus: z.ZodOptional<z.ZodInt>;
153
+ point_total: z.ZodNumber;
154
+ event_points: z.ZodOptional<z.ZodArray<z.ZodObject<{
155
+ district_cmp: z.ZodBoolean;
156
+ total: z.ZodNumber;
157
+ alliance_points: z.ZodNumber;
158
+ elim_points: z.ZodNumber;
159
+ award_points: z.ZodNumber;
160
+ event_key: z.ZodString;
161
+ qual_points: z.ZodNumber;
162
+ }, {}>>>;
163
+ }, {}>>>;
164
+ arguments: z.ZodTuple<[z.ZodString], null>;
165
+ };
166
+ "/district/{district_key}/advancement": {
167
+ schema: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodObject<{
168
+ dcmp: z.ZodBoolean;
169
+ cmp: z.ZodBoolean;
170
+ }, {}>>>;
171
+ arguments: z.ZodTuple<[z.ZodString], null>;
172
+ };
173
+ };
@@ -0,0 +1,50 @@
1
+ import { z } from "zod";
2
+ import { District_Advancement, District_List, District_Ranking } from "../schemas/districts.js";
3
+ import { Event, Event_Simple } from "../schemas/events.js";
4
+ import { Award, Team, Team_Simple } from "../schemas/teams.js";
5
+ export const districtEndpoints = {
6
+ "/districts/{year}": {
7
+ schema: z.array(District_List),
8
+ arguments: z.tuple([z.int()]),
9
+ },
10
+ "/district/{district_abbreviation}/history": {
11
+ schema: z.array(District_List),
12
+ arguments: z.tuple([z.string()]),
13
+ },
14
+ "/district/{district_key}/events": {
15
+ schema: z.array(Event),
16
+ arguments: z.tuple([z.string()]),
17
+ },
18
+ "/district/{district_key}/awards": {
19
+ schema: z.array(Award),
20
+ arguments: z.tuple([z.string()]),
21
+ },
22
+ "/district/{district_key}/events/simple": {
23
+ schema: z.array(Event_Simple),
24
+ arguments: z.tuple([z.string()]),
25
+ },
26
+ "/district/{district_key}/events/keys": {
27
+ schema: z.array(z.string()),
28
+ arguments: z.tuple([z.string()]),
29
+ },
30
+ "/district/{district_key}/teams": {
31
+ schema: z.array(Team),
32
+ arguments: z.tuple([z.string()]),
33
+ },
34
+ "/district/{district_key}/teams/simple": {
35
+ schema: z.array(Team_Simple),
36
+ arguments: z.tuple([z.string()]),
37
+ },
38
+ "/district/{district_key}/teams/keys": {
39
+ schema: z.array(z.string()),
40
+ arguments: z.tuple([z.string()]),
41
+ },
42
+ "/district/{district_key}/rankings": {
43
+ schema: z.array(District_Ranking).nullable(),
44
+ arguments: z.tuple([z.string()]),
45
+ },
46
+ "/district/{district_key}/advancement": {
47
+ schema: z.record(z.string(), District_Advancement).nullable(),
48
+ arguments: z.tuple([z.string()]),
49
+ },
50
+ };