1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * List tunnel routes |
8 | * Lists and filters private network routes in an account. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | account_identifier: string, |
13 | comment: string | undefined, |
14 | is_deleted: string | undefined, |
15 | network_subset: string | undefined, |
16 | network_superset: string | undefined, |
17 | existed_at: string | undefined, |
18 | tunnel_id: string | undefined, |
19 | route_id: string | undefined, |
20 | tun_types: string | undefined, |
21 | virtual_network_id: string | undefined, |
22 | per_page: string | undefined, |
23 | page: string | undefined |
24 | ) { |
25 | const url = new URL( |
26 | `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/teamnet/routes` |
27 | ); |
28 | for (const [k, v] of [ |
29 | ["comment", comment], |
30 | ["is_deleted", is_deleted], |
31 | ["network_subset", network_subset], |
32 | ["network_superset", network_superset], |
33 | ["existed_at", existed_at], |
34 | ["tunnel_id", tunnel_id], |
35 | ["route_id", route_id], |
36 | ["tun_types", tun_types], |
37 | ["virtual_network_id", virtual_network_id], |
38 | ["per_page", per_page], |
39 | ["page", page], |
40 | ]) { |
41 | if (v !== undefined && v !== "") { |
42 | url.searchParams.append(k, v); |
43 | } |
44 | } |
45 | const response = await fetch(url, { |
46 | method: "GET", |
47 | headers: { |
48 | "X-AUTH-EMAIL": auth.email, |
49 | "X-AUTH-KEY": auth.key, |
50 | Authorization: "Bearer " + auth.token, |
51 | }, |
52 | body: undefined, |
53 | }); |
54 | if (!response.ok) { |
55 | const text = await response.text(); |
56 | throw new Error(`${response.status} ${text}`); |
57 | } |
58 | return await response.json(); |
59 | } |
60 |
|