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