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 | * Update DNS Record |
28 | * Patch a DNS record: only the fields you set are changed. |
29 | */ |
30 | export async function main( |
31 | auth: RT.Cloudflare, |
32 | zone_id: DynSelect_zone_id, |
33 | dns_record_id: string, |
34 | body: { |
35 | type?: string |
36 | name?: string |
37 | content?: string |
38 | ttl?: number |
39 | proxied?: "keep" | "true" | "false" |
40 | priority?: number |
41 | comment?: string |
42 | } |
43 | ) { |
44 | |
45 | const { proxied, ...rest } = body |
46 | const patch: { [key: string]: any } = Object.fromEntries( |
47 | Object.entries(rest).filter(([, v]) => v != null && v !== "") |
48 | ) |
49 | if (proxied === "true" || proxied === "false") { |
50 | patch.proxied = proxied === "true" |
51 | } |
52 |
|
53 | const response = await fetch( |
54 | `https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${dns_record_id}`, |
55 | { |
56 | method: "PATCH", |
57 | headers: { |
58 | Authorization: `Bearer ${auth.token}`, |
59 | "Content-Type": "application/json", |
60 | Accept: "application/json", |
61 | }, |
62 | body: JSON.stringify(patch), |
63 | } |
64 | ) |
65 |
|
66 | if (!response.ok) { |
67 | throw new Error(`${response.status} ${await response.text()}`) |
68 | } |
69 |
|
70 | return await response.json() |
71 | } |
72 |
|