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