0
List Healthcheck Events
One script reply has been approved by the moderators Verified

List origin health changes.

Created by hugo697 297 days ago Viewed 8986 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 297 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * List Healthcheck Events
8
 * List origin health changes.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  until: string | undefined,
13
  pool_name: string | undefined,
14
  origin_healthy: string | undefined,
15
  identifier: string | undefined,
16
  since: string | undefined,
17
  origin_name: string | undefined,
18
  pool_healthy: string | undefined
19
) {
20
  const url = new URL(
21
    `https://api.cloudflare.com/client/v4/user/load_balancing_analytics/events`
22
  );
23
  for (const [k, v] of [
24
    ["until", until],
25
    ["pool_name", pool_name],
26
    ["origin_healthy", origin_healthy],
27
    ["identifier", identifier],
28
    ["since", since],
29
    ["origin_name", origin_name],
30
    ["pool_healthy", pool_healthy],
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