1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * Retrieve discovered operations on a zone |
8 | * Retrieve the most up to date view of discovered operations |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | zone_id: string, |
13 | page: string | undefined, |
14 | per_page: string | undefined, |
15 | host: string | undefined, |
16 | method: string | undefined, |
17 | endpoint: string | undefined, |
18 | direction: "asc" | "desc" | undefined, |
19 | order: |
20 | | "host" |
21 | | "method" |
22 | | "endpoint" |
23 | | "traffic_stats.requests" |
24 | | "traffic_stats.last_updated" |
25 | | undefined, |
26 | diff: string | undefined, |
27 | origin: "ML" | "SessionIdentifier" | undefined, |
28 | state: "review" | "saved" | "ignored" | undefined |
29 | ) { |
30 | const url = new URL( |
31 | `https://api.cloudflare.com/client/v4/zones/${zone_id}/api_gateway/discovery/operations` |
32 | ); |
33 | for (const [k, v] of [ |
34 | ["page", page], |
35 | ["per_page", per_page], |
36 | ["host", host], |
37 | ["method", method], |
38 | ["endpoint", endpoint], |
39 | ["direction", direction], |
40 | ["order", order], |
41 | ["diff", diff], |
42 | ["origin", origin], |
43 | ["state", state], |
44 | ]) { |
45 | if (v !== undefined && v !== "") { |
46 | url.searchParams.append(k, v); |
47 | } |
48 | } |
49 | const response = await fetch(url, { |
50 | method: "GET", |
51 | headers: { |
52 | "X-AUTH-EMAIL": auth.email, |
53 | "X-AUTH-KEY": auth.key, |
54 | Authorization: "Bearer " + auth.token, |
55 | }, |
56 | body: undefined, |
57 | }); |
58 | if (!response.ok) { |
59 | const text = await response.text(); |
60 | throw new Error(`${response.status} ${text}`); |
61 | } |
62 | return await response.json(); |
63 | } |
64 |
|