0

List firewall rules

by
Published Nov 16, 2023

Fetches firewall rules in a zone. You can filter the results using several optional parameters.

Script cloudflare
  • Submitted by hugo697 Typescript (fetch-only)
    Created 537 days ago
    1
    type Cloudflare = {
    2
      token: string;
    3
      email: string;
    4
      key: string;
    5
    };
    6
    /**
    7
     * List firewall rules
    8
     * Fetches firewall rules in a zone. You can filter the results using several optional parameters.
    9
     */
    10
    export async function main(
    11
      auth: Cloudflare,
    12
      zone_identifier: string,
    13
      description: string | undefined,
    14
      action: string | undefined,
    15
      page: string | undefined,
    16
      per_page: string | undefined,
    17
      id: string | undefined,
    18
      paused: string | undefined
    19
    ) {
    20
      const url = new URL(
    21
        `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/firewall/rules`
    22
      );
    23
      for (const [k, v] of [
    24
        ["description", description],
    25
        ["action", action],
    26
        ["page", page],
    27
        ["per_page", per_page],
    28
        ["id", id],
    29
        ["paused", paused],
    30
      ]) {
    31
        if (v !== undefined && v !== "") {
    32
          url.searchParams.append(k, v);
    33
        }
    34
      }
    35
      const response = await fetch(url, {
    36
        method: "GET",
    37
        headers: {
    38
          "X-AUTH-EMAIL": auth.email,
    39
          "X-AUTH-KEY": auth.key,
    40
          Authorization: "Bearer " + auth.token,
    41
        },
    42
        body: undefined,
    43
      });
    44
      if (!response.ok) {
    45
        const text = await response.text();
    46
        throw new Error(`${response.status} ${text}`);
    47
      }
    48
      return await response.json();
    49
    }
    50