1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * Get Datasets |
8 | * Get a list of datasets. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | limit: string | undefined, |
13 | offset: string | undefined, |
14 | datasetType: "RANKING_BUCKET" | "REPORT" | undefined, |
15 | format: "JSON" | "CSV" | undefined |
16 | ) { |
17 | const url = new URL(`https://api.cloudflare.com/client/v4/radar/datasets`); |
18 | for (const [k, v] of [ |
19 | ["limit", limit], |
20 | ["offset", offset], |
21 | ["datasetType", datasetType], |
22 | ["format", format], |
23 | ]) { |
24 | if (v !== undefined && v !== "") { |
25 | url.searchParams.append(k, v); |
26 | } |
27 | } |
28 | const response = await fetch(url, { |
29 | method: "GET", |
30 | headers: { |
31 | "X-AUTH-EMAIL": auth.email, |
32 | "X-AUTH-KEY": auth.key, |
33 | Authorization: "Bearer " + auth.token, |
34 | }, |
35 | body: undefined, |
36 | }); |
37 | if (!response.ok) { |
38 | const text = await response.text(); |
39 | throw new Error(`${response.status} ${text}`); |
40 | } |
41 | return await response.json(); |
42 | } |
43 |
|