1 | |
2 | type Tomorrow = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Retrieve Events (Basic) |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Tomorrow, |
11 | location: string | undefined, |
12 | insights: string | undefined, |
13 | buffer: string | undefined, |
14 | Accept_Encoding?: string, |
15 | ) { |
16 | const url = new URL(`https://api.tomorrow.io/v4/events`); |
17 | for (const [k, v] of [ |
18 | ["location", location], |
19 | ["insights", insights], |
20 | ["buffer", buffer], |
21 | ]) { |
22 | if (v !== undefined && v !== "" && k !== undefined) { |
23 | url.searchParams.append(k, v); |
24 | } |
25 | } |
26 | const response = await fetch(url, { |
27 | method: "GET", |
28 | headers: { |
29 | ...(Accept_Encoding ? { "Accept-Encoding": Accept_Encoding } : {}), |
30 | ApiKey: auth.apiKey, |
31 | }, |
32 | body: undefined, |
33 | }); |
34 | if (!response.ok) { |
35 | const text = await response.text(); |
36 | throw new Error(`${response.status} ${text}`); |
37 | } |
38 | return await response.json(); |
39 | } |
40 |
|