1 | |
2 | type Tomorrow = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Weather Maps |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Tomorrow, |
11 | zoom: string = "5", |
12 | x: string = "2", |
13 | y: string = "3", |
14 | field: string = "precipitationIntensity", |
15 | time: string = "now", |
16 | format: string = "png", |
17 | gradient: string | undefined, |
18 | accept_encoding: string, |
19 | ) { |
20 | const url = new URL( |
21 | `https://api.tomorrow.io/v4/map/tile/${zoom}/${x}/${y}/${field}/${time}.${format}`, |
22 | ); |
23 | for (const [k, v] of [["gradient", gradient]]) { |
24 | if (v !== undefined && v !== "" && k !== undefined) { |
25 | url.searchParams.append(k, v); |
26 | } |
27 | } |
28 | const response = await fetch(url, { |
29 | method: "GET", |
30 | headers: { |
31 | "accept-encoding": accept_encoding, |
32 | ApiKey: auth.apiKey, |
33 | }, |
34 | body: undefined, |
35 | }); |
36 | if (!response.ok) { |
37 | const text = await response.text(); |
38 | throw new Error(`${response.status} ${text}`); |
39 | } |
40 | return await response.text(); |
41 | } |
42 |
|