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