Edits history of script submission #10788 for ' Deleting Droplets by Tag (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Deleting Droplets by Tag
     * To delete **all** Droplets assigned to a specific tag, include the `tag_name`
    query parameter set to the name of the tag in your DELETE request. For
    example,  `/v2/droplets?tag_name=$TAG_NAME`.
    
    A successful request will receive a 204 status code with no body in response.
    This indicates that the request was processed successfully.
    
     */
    export async function main(auth: Digitalocean, tag_name: string | undefined) {
      const url = new URL(`https://api.digitalocean.com/v2/droplets`);
      for (const [k, v] of [["tag_name", tag_name]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "DELETE",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 536 days ago