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