Edits history of script submission #10846 for ' Initiate A Block Storage Action By Volume Id (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Initiate A Block Storage Action By Volume Id
     * To initiate an action on a block storage volume by Id, send a POST request to
    `~/v2/volumes/$VOLUME_ID/actions`.
     */
    export async function main(
      auth: Digitalocean,
      volume_id: string,
      per_page: string | undefined,
      page: string | undefined,
      body:
        | ({
            type: "attach" | "detach" | "resize";
            region?:
              | "ams1"
              | "ams2"
              | "ams3"
              | "blr1"
              | "fra1"
              | "lon1"
              | "nyc1"
              | "nyc2"
              | "nyc3"
              | "sfo1"
              | "sfo2"
              | "sfo3"
              | "sgp1"
              | "tor1"
              | "syd1";
          } & { droplet_id: number; tags?: string[] })
        | ({
            type: "attach" | "detach" | "resize";
            region?:
              | "ams1"
              | "ams2"
              | "ams3"
              | "blr1"
              | "fra1"
              | "lon1"
              | "nyc1"
              | "nyc2"
              | "nyc3"
              | "sfo1"
              | "sfo2"
              | "sfo3"
              | "sgp1"
              | "tor1"
              | "syd1";
          } & { droplet_id: number })
        | ({
            type: "attach" | "detach" | "resize";
            region?:
              | "ams1"
              | "ams2"
              | "ams3"
              | "blr1"
              | "fra1"
              | "lon1"
              | "nyc1"
              | "nyc2"
              | "nyc3"
              | "sfo1"
              | "sfo2"
              | "sfo3"
              | "sgp1"
              | "tor1"
              | "syd1";
          } & { size_gigabytes: number }),
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/volumes/${volume_id}/actions`,
      );
      for (const [k, v] of [
        ["per_page", per_page],
        ["page", page],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 536 days ago