Get latest Internet outages and anomalies.

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