List Domains

Lists all Worker Domains for an account.

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 Domains
8
 * Lists all Worker Domains for an account.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  zone_name: string | undefined,
14
  service: string | undefined,
15
  zone_identifier: string | undefined,
16
  hostname: string | undefined,
17
  environment: string | undefined
18
) {
19
  const url = new URL(
20
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/workers/domains`
21
  );
22
  for (const [k, v] of [
23
    ["zone_name", zone_name],
24
    ["service", service],
25
    ["zone_identifier", zone_identifier],
26
    ["hostname", hostname],
27
    ["environment", environment],
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