1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * Patch Load Balancer |
8 | * Apply changes to an existing load balancer, overwriting the supplied properties. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | identifier: string, |
13 | identifier1: string, |
14 | body: { |
15 | adaptive_routing?: { |
16 | failover_across_pools?: boolean; |
17 | [k: string]: unknown; |
18 | }; |
19 | country_pools?: { [k: string]: unknown }; |
20 | default_pools?: string[]; |
21 | description?: string; |
22 | enabled?: boolean; |
23 | fallback_pool?: { [k: string]: unknown }; |
24 | location_strategy?: { |
25 | mode?: "pop" | "resolver_ip"; |
26 | prefer_ecs?: "always" | "never" | "proximity" | "geo"; |
27 | [k: string]: unknown; |
28 | }; |
29 | name?: string; |
30 | pop_pools?: { [k: string]: unknown }; |
31 | proxied?: boolean; |
32 | random_steering?: { |
33 | default_weight?: number; |
34 | pool_weights?: { [k: string]: unknown }; |
35 | [k: string]: unknown; |
36 | }; |
37 | region_pools?: { [k: string]: unknown }; |
38 | rules?: { |
39 | condition?: string; |
40 | disabled?: boolean; |
41 | fixed_response?: { |
42 | content_type?: string; |
43 | location?: string; |
44 | message_body?: string; |
45 | status_code?: number; |
46 | [k: string]: unknown; |
47 | }; |
48 | name?: string; |
49 | overrides?: { |
50 | adaptive_routing?: { |
51 | failover_across_pools?: boolean; |
52 | [k: string]: unknown; |
53 | }; |
54 | country_pools?: { [k: string]: unknown }; |
55 | default_pools?: string[]; |
56 | fallback_pool?: { [k: string]: unknown }; |
57 | location_strategy?: { |
58 | mode?: "pop" | "resolver_ip"; |
59 | prefer_ecs?: "always" | "never" | "proximity" | "geo"; |
60 | [k: string]: unknown; |
61 | }; |
62 | pop_pools?: { [k: string]: unknown }; |
63 | random_steering?: { |
64 | default_weight?: number; |
65 | pool_weights?: { [k: string]: unknown }; |
66 | [k: string]: unknown; |
67 | }; |
68 | region_pools?: { [k: string]: unknown }; |
69 | session_affinity?: "none" | "cookie" | "ip_cookie" | "header" | '""'; |
70 | session_affinity_attributes?: { |
71 | drain_duration?: number; |
72 | headers?: string & string[]; |
73 | require_all_headers?: boolean; |
74 | samesite?: "Auto" | "Lax" | "None" | "Strict"; |
75 | secure?: "Auto" | "Always" | "Never"; |
76 | zero_downtime_failover?: "none" | "temporary" | "sticky"; |
77 | [k: string]: unknown; |
78 | }; |
79 | session_affinity_ttl?: number; |
80 | steering_policy?: |
81 | | "off" |
82 | | "geo" |
83 | | "random" |
84 | | "dynamic_latency" |
85 | | "proximity" |
86 | | "least_outstanding_requests" |
87 | | "least_connections" |
88 | | '""'; |
89 | ttl?: number; |
90 | [k: string]: unknown; |
91 | }; |
92 | priority?: number; |
93 | terminates?: boolean; |
94 | }[]; |
95 | session_affinity?: "none" | "cookie" | "ip_cookie" | "header" | '""'; |
96 | session_affinity_attributes?: { |
97 | drain_duration?: number; |
98 | headers?: string & string[]; |
99 | require_all_headers?: boolean; |
100 | samesite?: "Auto" | "Lax" | "None" | "Strict"; |
101 | secure?: "Auto" | "Always" | "Never"; |
102 | zero_downtime_failover?: "none" | "temporary" | "sticky"; |
103 | [k: string]: unknown; |
104 | }; |
105 | session_affinity_ttl?: number; |
106 | steering_policy?: |
107 | | "off" |
108 | | "geo" |
109 | | "random" |
110 | | "dynamic_latency" |
111 | | "proximity" |
112 | | "least_outstanding_requests" |
113 | | "least_connections" |
114 | | '""'; |
115 | ttl?: number; |
116 | [k: string]: unknown; |
117 | } |
118 | ) { |
119 | const url = new URL( |
120 | `https://api.cloudflare.com/client/v4/zones/${identifier1}/load_balancers/${identifier}` |
121 | ); |
122 |
|
123 | const response = await fetch(url, { |
124 | method: "PATCH", |
125 | headers: { |
126 | "X-AUTH-EMAIL": auth.email, |
127 | "X-AUTH-KEY": auth.key, |
128 | "Content-Type": "application/json", |
129 | Authorization: "Bearer " + auth.token, |
130 | }, |
131 | body: JSON.stringify(body), |
132 | }); |
133 | if (!response.ok) { |
134 | const text = await response.text(); |
135 | throw new Error(`${response.status} ${text}`); |
136 | } |
137 | return await response.json(); |
138 | } |
139 |
|