Edits history of script submission #10701 for ' Add Droplets to a Load Balancer (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Add Droplets to a Load Balancer
     * To assign a Droplet to a load balancer instance, send a POST request to
    `/v2/load_balancers/$LOAD_BALANCER_ID/droplets`.
     */
    export async function main(
      auth: Digitalocean,
      lb_id: string,
      body: { droplet_ids: number[] },
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/load_balancers/${lb_id}/droplets`,
      );
    
      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