Edits history of script submission #10736 for ' Create a New Firewall (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Create a New Firewall
     * To create a new firewall, send a POST request to `/v2/firewalls`. The request
    must contain at least one inbound or outbound access rule.
    
     */
    export async function main(
      auth: Digitalocean,
      body:
        | ({
            id?: string;
            status?: "waiting" | "succeeded" | "failed";
            created_at?: string;
            pending_changes?: {
              droplet_id?: number;
              removing?: false | true;
              status?: string;
            }[];
            name?: string;
            droplet_ids?: number[];
            tags?: string[] & {};
          } & {
            inbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
              sources: {
                addresses?: string[];
                droplet_ids?: number[];
                load_balancer_uids?: string[];
                kubernetes_ids?: string[];
                tags?: string[] & {};
              } & {};
            }[];
            outbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
              destinations: {
                addresses?: string[];
                droplet_ids?: number[];
                load_balancer_uids?: string[];
                kubernetes_ids?: string[];
                tags?: string[] & {};
              } & {};
            }[];
          } & {} & {})
        | ({
            id?: string;
            status?: "waiting" | "succeeded" | "failed";
            created_at?: string;
            pending_changes?: {
              droplet_id?: number;
              removing?: false | true;
              status?: string;
            }[];
            name?: string;
            droplet_ids?: number[];
            tags?: string[] & {};
          } & {
            inbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
              sources: {
                addresses?: string[];
                droplet_ids?: number[];
                load_balancer_uids?: string[];
                kubernetes_ids?: string[];
                tags?: string[] & {};
              } & {};
            }[];
            outbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
              destinations: {
                addresses?: string[];
                droplet_ids?: number[];
                load_balancer_uids?: string[];
                kubernetes_ids?: string[];
                tags?: string[] & {};
              } & {};
            }[];
          } & {} & {}),
    ) {
      const url = new URL(`https://api.digitalocean.com/v2/firewalls`);
    
      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