List WAF rules

Fetches WAF rules in a WAF package. **Note:** Applies only to the [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * List WAF rules
8
 * Fetches WAF rules in a WAF package.
9

10
**Note:** Applies only to the [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).
11
 */
12
export async function main(
13
  auth: Cloudflare,
14
  package_id: string,
15
  zone_id: string,
16
  mode: "DIS" | "CHL" | "BLK" | "SIM" | undefined,
17
  group_id: string | undefined,
18
  page: string | undefined,
19
  per_page: string | undefined,
20
  order: "priority" | "group_id" | "description" | undefined,
21
  direction: "asc" | "desc" | undefined,
22
  match: "any" | "all" | undefined,
23
  description: string | undefined,
24
  priority: string | undefined
25
) {
26
  const url = new URL(
27
    `https://api.cloudflare.com/client/v4/zones/${zone_id}/firewall/waf/packages/${package_id}/rules`
28
  );
29
  for (const [k, v] of [
30
    ["mode", mode],
31
    ["group_id", group_id],
32
    ["page", page],
33
    ["per_page", per_page],
34
    ["order", order],
35
    ["direction", direction],
36
    ["match", match],
37
    ["description", description],
38
    ["priority", priority],
39
  ]) {
40
    if (v !== undefined && v !== "") {
41
      url.searchParams.append(k, v);
42
    }
43
  }
44
  const response = await fetch(url, {
45
    method: "GET",
46
    headers: {
47
      "X-AUTH-EMAIL": auth.email,
48
      "X-AUTH-KEY": auth.key,
49
      Authorization: "Bearer " + auth.token,
50
    },
51
    body: undefined,
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59