Edits history of script submission #10851 for ' Initiate an Image Action (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Initiate an Image Action
     * The following actions are available on an Image.
    
    ## Convert an Image to a Snapshot
    
    To convert an image, for example, a backup to a snapshot, send a POST request
    to `/v2/images/$IMAGE_ID/actions`. Set the `type` attribute to `convert`.
    
    ## Transfer an Image
    
    To transfer an image to another region, send a POST request to
    `/v2/images/$IMAGE_ID/actions`. Set the `type` attribute to `transfer` and set
    `region` attribute to the slug identifier of the region you wish to transfer
    to.
    
     */
    export async function main(
      auth: Digitalocean,
      image_id: string,
      body:
        | { type: "convert" | "transfer" }
        | ({ type: "convert" | "transfer" } & {
            region:
              | "ams1"
              | "ams2"
              | "ams3"
              | "blr1"
              | "fra1"
              | "lon1"
              | "nyc1"
              | "nyc2"
              | "nyc3"
              | "sfo1"
              | "sfo2"
              | "sfo3"
              | "sgp1"
              | "tor1"
              | "syd1";
          }),
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/images/${image_id}/actions`,
      );
    
      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