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 |
|