0

List Zone Lockdown rules

by
Published Nov 16, 2023

Fetches Zone Lockdown rules. You can filter the results using several optional parameters.

Script cloudflare Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * List Zone Lockdown rules
8
 * Fetches Zone Lockdown rules. 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
  modified_on: string | undefined,
16
  ip: string | undefined,
17
  priority: string | undefined,
18
  uri_search: string | undefined,
19
  ip_range_search: string | undefined,
20
  per_page: string | undefined,
21
  created_on: string | undefined,
22
  description_search: string | undefined,
23
  ip_search: string | undefined
24
) {
25
  const url = new URL(
26
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/firewall/lockdowns`
27
  );
28
  for (const [k, v] of [
29
    ["page", page],
30
    ["description", description],
31
    ["modified_on", modified_on],
32
    ["ip", ip],
33
    ["priority", priority],
34
    ["uri_search", uri_search],
35
    ["ip_range_search", ip_range_search],
36
    ["per_page", per_page],
37
    ["created_on", created_on],
38
    ["description_search", description_search],
39
    ["ip_search", ip_search],
40
  ]) {
41
    if (v !== undefined && v !== "") {
42
      url.searchParams.append(k, v);
43
    }
44
  }
45
  const response = await fetch(url, {
46
    method: "GET",
47
    headers: {
48
      "X-AUTH-EMAIL": auth.email,
49
      "X-AUTH-KEY": auth.key,
50
      Authorization: "Bearer " + auth.token,
51
    },
52
    body: undefined,
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.json();
59
}
60