1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * Search URL scans |
8 | * Search scans by date and webpages' requests, including full URL (after redirects), hostname, and path. A successful scan will appear in search results a few minutes after finishing but may take much longer if the system in under load. By default, only successfully completed scans will appear in search results, unless searching by `scanId`. Please take into account that older scans may be removed from the search index at an unspecified time. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | accountId: string, |
13 | scanId: string | undefined, |
14 | limit: string | undefined, |
15 | next_cursor: string | undefined, |
16 | date_start: string | undefined, |
17 | date_end: string | undefined, |
18 | url: string | undefined, |
19 | hostname: string | undefined, |
20 | path: string | undefined, |
21 | page_url: string | undefined, |
22 | page_hostname: string | undefined, |
23 | page_path: string | undefined, |
24 | account_scans: string | undefined |
25 | ) { |
26 | const url_ = new URL( |
27 | `https://api.cloudflare.com/client/v4/accounts/${accountId}/urlscanner/scan` |
28 | ); |
29 | for (const [k, v] of [ |
30 | ["scanId", scanId], |
31 | ["limit", limit], |
32 | ["next_cursor", next_cursor], |
33 | ["date_start", date_start], |
34 | ["date_end", date_end], |
35 | ["url", url], |
36 | ["hostname", hostname], |
37 | ["path", path], |
38 | ["page_url", page_url], |
39 | ["page_hostname", page_hostname], |
40 | ["page_path", page_path], |
41 | ["account_scans", account_scans], |
42 | ]) { |
43 | if (v !== undefined && v !== "") { |
44 | url_.searchParams.append(k, v); |
45 | } |
46 | } |
47 | const response = await fetch(url_, { |
48 | method: "GET", |
49 | headers: { |
50 | "X-AUTH-EMAIL": auth.email, |
51 | "X-AUTH-KEY": auth.key, |
52 | Authorization: "Bearer " + auth.token, |
53 | }, |
54 | body: undefined, |
55 | }); |
56 | if (!response.ok) { |
57 | const text = await response.text(); |
58 | throw new Error(`${response.status} ${text}`); |
59 | } |
60 | return await response.json(); |
61 | } |
62 |
|