1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * By Time |
8 | * Retrieves a list of aggregate metrics grouped by time interval. |
9 |
|
10 | See [Analytics API properties](https://developers.cloudflare.com/dns/reference/analytics-api-properties/) for detailed information about the available query parameters. |
11 | */ |
12 | export async function main( |
13 | auth: Cloudflare, |
14 | identifier: string, |
15 | account_identifier: string, |
16 | metrics: string | undefined, |
17 | dimensions: string | undefined, |
18 | since: string | undefined, |
19 | until: string | undefined, |
20 | limit: string | undefined, |
21 | sort: string | undefined, |
22 | filters: string | undefined, |
23 | time_delta: |
24 | | "all" |
25 | | "auto" |
26 | | "year" |
27 | | "quarter" |
28 | | "month" |
29 | | "week" |
30 | | "day" |
31 | | "hour" |
32 | | "dekaminute" |
33 | | "minute" |
34 | | undefined |
35 | ) { |
36 | const url = new URL( |
37 | `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/dns_firewall/${identifier}/dns_analytics/report/bytime` |
38 | ); |
39 | for (const [k, v] of [ |
40 | ["metrics", metrics], |
41 | ["dimensions", dimensions], |
42 | ["since", since], |
43 | ["until", until], |
44 | ["limit", limit], |
45 | ["sort", sort], |
46 | ["filters", filters], |
47 | ["time_delta", time_delta], |
48 | ]) { |
49 | if (v !== undefined && v !== "") { |
50 | url.searchParams.append(k, v); |
51 | } |
52 | } |
53 | const response = await fetch(url, { |
54 | method: "GET", |
55 | headers: { |
56 | "X-AUTH-EMAIL": auth.email, |
57 | "X-AUTH-KEY": auth.key, |
58 | Authorization: "Bearer " + auth.token, |
59 | }, |
60 | body: undefined, |
61 | }); |
62 | if (!response.ok) { |
63 | const text = await response.text(); |
64 | throw new Error(`${response.status} ${text}`); |
65 | } |
66 | return await response.json(); |
67 | } |
68 |
|