0

Weather Forecast

by
Published Oct 17, 2025
Script tomorrow Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Tomorrow = {
3
  apiKey: string;
4
};
5
/**
6
 * Weather Forecast
7
 *
8
 */
9
export async function main(
10
  auth: Tomorrow,
11
  location: string | undefined,
12
  timesteps: string | undefined,
13
  units: string | undefined,
14
  accept_encoding: string,
15
) {
16
  const url = new URL(`https://api.tomorrow.io/v4/weather/forecast`);
17
  for (const [k, v] of [
18
    ["location", location],
19
    ["timesteps", timesteps],
20
    ["units", units],
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,
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