1 | |
2 |
|
3 | export type DynSelect_zone_id = string |
4 |
|
5 | |
6 | |
7 | export async function zone_id(auth: RT.Cloudflare) { |
8 | const response = await fetch( |
9 | "https://api.cloudflare.com/client/v4/zones?per_page=50", |
10 | { |
11 | headers: { |
12 | Authorization: `Bearer ${auth.token}`, |
13 | Accept: "application/json", |
14 | }, |
15 | } |
16 | ) |
17 | if (!response.ok) { |
18 | throw new Error(`${response.status} ${await response.text()}`) |
19 | } |
20 | const { result } = (await response.json()) as { |
21 | result: { id: string; name: string }[] |
22 | } |
23 | return result.map((z) => ({ value: z.id, label: z.name })) |
24 | } |
25 |
|
26 | |
27 | * Purge Cache |
28 | * Purge a zone's cached content: everything, or only the given URLs. Leave files empty to purge everything. |
29 | */ |
30 | export async function main( |
31 | auth: RT.Cloudflare, |
32 | zone_id: DynSelect_zone_id, |
33 | files: string[] | undefined |
34 | ) { |
35 | const body = files?.length ? { files } : { purge_everything: true } |
36 |
|
37 | const response = await fetch( |
38 | `https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache`, |
39 | { |
40 | method: "POST", |
41 | headers: { |
42 | Authorization: `Bearer ${auth.token}`, |
43 | "Content-Type": "application/json", |
44 | Accept: "application/json", |
45 | }, |
46 | body: JSON.stringify(body), |
47 | } |
48 | ) |
49 |
|
50 | if (!response.ok) { |
51 | throw new Error(`${response.status} ${await response.text()}`) |
52 | } |
53 |
|
54 | return await response.json() |
55 | } |
56 |
|