List Page Rules

Fetches Page Rules in a zone.

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 Page Rules
8
 * Fetches Page Rules in a zone.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  order: "status" | "priority" | undefined,
14
  direction: "asc" | "desc" | undefined,
15
  match: "any" | "all" | undefined,
16
  status: "active" | "disabled" | undefined
17
) {
18
  const url = new URL(
19
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/pagerules`
20
  );
21
  for (const [k, v] of [
22
    ["order", order],
23
    ["direction", direction],
24
    ["match", match],
25
    ["status", status],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      "X-AUTH-EMAIL": auth.email,
35
      "X-AUTH-KEY": auth.key,
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46