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