List Zones

Lists, searches, sorts, and filters your zones.

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 Zones
8
 * Lists, searches, sorts, and filters your zones.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  name: string | undefined,
13
  status: "initializing" | "pending" | "active" | "moved" | undefined,
14
  account_id: string | undefined,
15
  account_name: string | undefined,
16
  page: string | undefined,
17
  per_page: string | undefined,
18
  order: "name" | "status" | "account.id" | "account.name" | undefined,
19
  direction: "asc" | "desc" | undefined,
20
  match: "any" | "all" | undefined
21
) {
22
  const url = new URL(`https://api.cloudflare.com/client/v4/zones`);
23
  for (const [k, v] of [
24
    ["name", name],
25
    ["status", status],
26
    ["account.id", account_id],
27
    ["account.name", account_name],
28
    ["page", page],
29
    ["per_page", per_page],
30
    ["order", order],
31
    ["direction", direction],
32
    ["match", match],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      "X-AUTH-EMAIL": auth.email,
42
      "X-AUTH-KEY": auth.key,
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53