Edits history of script submission #10739 for ' Create a New Load Balancer (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Create a New Load Balancer
     * To create a new load balancer instance, send a POST request to
    `/v2/load_balancers`.
    
    You can specify the Droplets that will sit behind the load balancer using one
    of two methods:
    
    * Set `droplet_ids` to a list of specific Droplet IDs.
    * Set `tag` to the name of a tag. All Droplets with this tag applied will be
      assigned to the load balancer. Additional Droplets will be automatically
      assigned as they are tagged.
    
    These methods are mutually exclusive.
    
     */
    export async function main(
      auth: Digitalocean,
      body:
        | ({ droplet_ids?: number[] } & {
            region?:
              | "ams1"
              | "ams2"
              | "ams3"
              | "blr1"
              | "fra1"
              | "lon1"
              | "nyc1"
              | "nyc2"
              | "nyc3"
              | "sfo1"
              | "sfo2"
              | "sfo3"
              | "sgp1"
              | "tor1"
              | "syd1";
          } & {
            id?: string;
            name?: string;
            project_id?: string;
            ip?: string;
            size_unit?: number;
            size?: "lb-small" | "lb-medium" | "lb-large";
            algorithm?: "round_robin" | "least_connections";
            status?: "new" | "active" | "errored";
            created_at?: string;
            forwarding_rules: {
              entry_protocol: "http" | "https" | "http2" | "http3" | "tcp" | "udp";
              entry_port: number;
              target_protocol: "http" | "https" | "http2" | "tcp" | "udp";
              target_port: number;
              certificate_id?: string;
              tls_passthrough?: false | true;
            }[];
            health_check?: {
              protocol?: "http" | "https" | "tcp";
              port?: number;
              path?: string;
              check_interval_seconds?: number;
              response_timeout_seconds?: number;
              unhealthy_threshold?: number;
              healthy_threshold?: number;
            };
            sticky_sessions?: {
              type?: "cookies" | "none";
              cookie_name?: string;
              cookie_ttl_seconds?: number;
            };
            redirect_http_to_https?: false | true;
            enable_proxy_protocol?: false | true;
            enable_backend_keepalive?: false | true;
            http_idle_timeout_seconds?: number;
            vpc_uuid?: string;
            disable_lets_encrypt_dns_records?: false | true;
            firewall?: { deny?: string[]; allow?: string[] };
            network?: "EXTERNAL" | "INTERNAL";
          })
        | ({ tag?: string } & {
            region?:
              | "ams1"
              | "ams2"
              | "ams3"
              | "blr1"
              | "fra1"
              | "lon1"
              | "nyc1"
              | "nyc2"
              | "nyc3"
              | "sfo1"
              | "sfo2"
              | "sfo3"
              | "sgp1"
              | "tor1"
              | "syd1";
          } & {
            id?: string;
            name?: string;
            project_id?: string;
            ip?: string;
            size_unit?: number;
            size?: "lb-small" | "lb-medium" | "lb-large";
            algorithm?: "round_robin" | "least_connections";
            status?: "new" | "active" | "errored";
            created_at?: string;
            forwarding_rules: {
              entry_protocol: "http" | "https" | "http2" | "http3" | "tcp" | "udp";
              entry_port: number;
              target_protocol: "http" | "https" | "http2" | "tcp" | "udp";
              target_port: number;
              certificate_id?: string;
              tls_passthrough?: false | true;
            }[];
            health_check?: {
              protocol?: "http" | "https" | "tcp";
              port?: number;
              path?: string;
              check_interval_seconds?: number;
              response_timeout_seconds?: number;
              unhealthy_threshold?: number;
              healthy_threshold?: number;
            };
            sticky_sessions?: {
              type?: "cookies" | "none";
              cookie_name?: string;
              cookie_ttl_seconds?: number;
            };
            redirect_http_to_https?: false | true;
            enable_proxy_protocol?: false | true;
            enable_backend_keepalive?: false | true;
            http_idle_timeout_seconds?: number;
            vpc_uuid?: string;
            disable_lets_encrypt_dns_records?: false | true;
            firewall?: { deny?: string[]; allow?: string[] };
            network?: "EXTERNAL" | "INTERNAL";
          }),
    ) {
      const url = new URL(`https://api.digitalocean.com/v2/load_balancers`);
    
      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