1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * Create a Turnstile Widget |
8 | * Lists challenge widgets. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | account_identifier: string, |
13 | page: string | undefined, |
14 | per_page: string | undefined, |
15 | order: "id" | "sitekey" | "name" | "created_on" | "modified_on" | undefined, |
16 | direction: "asc" | "desc" | undefined, |
17 | body: { |
18 | bot_fight_mode?: boolean; |
19 | clearance_level?: |
20 | | "no_clearance" |
21 | | "jschallenge" |
22 | | "managed" |
23 | | "interactive"; |
24 | domains: string[]; |
25 | mode: "non-interactive" | "invisible" | "managed"; |
26 | name: string; |
27 | offlabel?: boolean; |
28 | region?: "world"; |
29 | [k: string]: unknown; |
30 | } |
31 | ) { |
32 | const url = new URL( |
33 | `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/challenges/widgets` |
34 | ); |
35 | for (const [k, v] of [ |
36 | ["page", page], |
37 | ["per_page", per_page], |
38 | ["order", order], |
39 | ["direction", direction], |
40 | ]) { |
41 | if (v !== undefined && v !== "") { |
42 | url.searchParams.append(k, v); |
43 | } |
44 | } |
45 | const response = await fetch(url, { |
46 | method: "POST", |
47 | headers: { |
48 | "X-AUTH-EMAIL": auth.email, |
49 | "X-AUTH-KEY": auth.key, |
50 | "Content-Type": "application/json", |
51 | Authorization: "Bearer " + auth.token, |
52 | }, |
53 | body: JSON.stringify(body), |
54 | }); |
55 | if (!response.ok) { |
56 | const text = await response.text(); |
57 | throw new Error(`${response.status} ${text}`); |
58 | } |
59 | return await response.json(); |
60 | } |
61 |
|