List WAF packages

Fetches WAF packages for a zone. **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 packages
8
 * Fetches WAF packages for a zone.
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
  zone_identifier: string,
15
  page: string | undefined,
16
  per_page: string | undefined,
17
  order: "name" | undefined,
18
  direction: "asc" | "desc" | undefined,
19
  match: "any" | "all" | undefined,
20
  name: string | undefined
21
) {
22
  const url = new URL(
23
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/firewall/waf/packages`
24
  );
25
  for (const [k, v] of [
26
    ["page", page],
27
    ["per_page", per_page],
28
    ["order", order],
29
    ["direction", direction],
30
    ["match", match],
31
    ["name", name],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "GET",
39
    headers: {
40
      "X-AUTH-EMAIL": auth.email,
41
      "X-AUTH-KEY": auth.key,
42
      Authorization: "Bearer " + auth.token,
43
    },
44
    body: undefined,
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52