Edits history of script submission #21024 for ' Weather Maps (tomorrow)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Tomorrow = {
      apiKey: string;
    };
    /**
     * Weather Maps
     *
     */
    export async function main(
      auth: Tomorrow,
      zoom: string = "5",
      x: string = "2",
      y: string = "3",
      field: string = "precipitationIntensity",
      time: string = "now",
      format: string = "png",
      gradient: string | undefined,
      accept_encoding: string,
    ) {
      const url = new URL(
        `https://api.tomorrow.io/v4/map/tile/${zoom}/${x}/${y}/${field}/${time}.${format}`,
      );
      for (const [k, v] of [["gradient", gradient]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          "accept-encoding": accept_encoding,
          ApiKey: auth.apiKey,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.text();
    }
    

    Submitted by hugo697 235 days ago