List Organizations

Lists organizations the user is associated with.

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