Edits history of script submission #5358 for ' Create an IP Access rule (cloudflare)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Cloudflare = {
      token: string;
      email: string;
      key: string;
    };
    /**
     * Create an IP Access rule
     * Creates a new IP Access rule for an account. The rule will apply to all zones in the account.
    
    Note: To create an IP Access rule that applies to a single zone, refer to the [IP Access rules for a zone](#ip-access-rules-for-a-zone) endpoints.
     */
    export async function main(
      auth: Cloudflare,
      account_identifier: string,
      body: {
        configuration:
          | { target?: "ip"; value?: string; [k: string]: unknown }
          | { target?: "ip6"; value?: string; [k: string]: unknown }
          | { target?: "ip_range"; value?: string; [k: string]: unknown }
          | { target?: "asn"; value?: string; [k: string]: unknown }
          | { target?: "country"; value?: string; [k: string]: unknown };
        mode:
          | "block"
          | "challenge"
          | "whitelist"
          | "js_challenge"
          | "managed_challenge";
        notes?: string;
        [k: string]: unknown;
      }
    ) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/firewall/access_rules/rules`
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "X-AUTH-EMAIL": auth.email,
          "X-AUTH-KEY": auth.key,
          "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 383 days ago

  • nativets
    type Cloudflare = {
      token: string;
      email: string;
      key: string;
    };
    /**
     * Create an IP Access rule
     * Creates a new IP Access rule for an account. The rule will apply to all zones in the account.
    
    Note: To create an IP Access rule that applies to a single zone, refer to the [IP Access rules for a zone](#ip-access-rules-for-a-zone) endpoints.
     */
    export async function main(
      auth: Cloudflare,
      account_identifier: string,
      body: {
        configuration:
          | { target?: "ip"; value?: string; [k: string]: unknown }
          | { target?: "ip6"; value?: string; [k: string]: unknown }
          | { target?: "ip_range"; value?: string; [k: string]: unknown }
          | { target?: "asn"; value?: string; [k: string]: unknown }
          | { target?: "country"; value?: string; [k: string]: unknown };
        mode:
          | "block"
          | "challenge"
          | "whitelist"
          | "js_challenge"
          | "managed_challenge";
        notes?: string;
        [k: string]: unknown;
      }
    ) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/firewall/access_rules/rules`
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "X-AUTH-EMAIL": auth.email,
          "X-AUTH-KEY": auth.key,
          "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 920 days ago