List User Agent Blocking rules

Fetches User Agent Blocking rules in a zone. You can filter the results using several optional parameters.

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