Edits history of script submission #21016 for ' Retrieve a Route (tomorrow)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Tomorrow = {
      apiKey: string;
    };
    /**
     * Retrieve a Route
     *
     */
    export async function main(
      auth: Tomorrow,
      legs: string | undefined,
      fields: string | undefined,
      startTime: string | undefined,
      timestep: string | undefined,
      units: string | undefined,
      timezone: string | undefined,
      Accept_Encoding: string,
    ) {
      const url = new URL(`https://api.tomorrow.io/v4/route`);
      for (const [k, v] of [
        ["legs", legs],
        ["fields", fields],
        ["startTime", startTime],
        ["timestep", timestep],
        ["units", units],
        ["timezone", timezone],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        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.json();
    }
    

    Submitted by hugo697 235 days ago