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