Get the number of outages for locations.

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
 * Get the number of outages for locations.
8
 *
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  limit: string | undefined,
13
  dateRange:
14
    | "1d"
15
    | "2d"
16
    | "7d"
17
    | "14d"
18
    | "28d"
19
    | "12w"
20
    | "24w"
21
    | "52w"
22
    | "1dControl"
23
    | "2dControl"
24
    | "7dControl"
25
    | "14dControl"
26
    | "28dControl"
27
    | "12wControl"
28
    | "24wControl"
29
    | undefined,
30
  dateStart: string | undefined,
31
  dateEnd: string | undefined,
32
  format: "JSON" | "CSV" | undefined
33
) {
34
  const url = new URL(
35
    `https://api.cloudflare.com/client/v4/radar/annotations/outages/locations`
36
  );
37
  for (const [k, v] of [
38
    ["limit", limit],
39
    ["dateRange", dateRange],
40
    ["dateStart", dateStart],
41
    ["dateEnd", dateEnd],
42
    ["format", format],
43
  ]) {
44
    if (v !== undefined && v !== "") {
45
      url.searchParams.append(k, v);
46
    }
47
  }
48
  const response = await fetch(url, {
49
    method: "GET",
50
    headers: {
51
      "X-AUTH-EMAIL": auth.email,
52
      "X-AUTH-KEY": auth.key,
53
      Authorization: "Bearer " + auth.token,
54
    },
55
    body: undefined,
56
  });
57
  if (!response.ok) {
58
    const text = await response.text();
59
    throw new Error(`${response.status} ${text}`);
60
  }
61
  return await response.json();
62
}
63