Update Hyperdrive

Updates and returns the specified Hyperdrive configuration.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Update Hyperdrive
8
 * Updates and returns the specified Hyperdrive configuration.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  hyperdrive_identifier: string,
14
  body: {
15
    caching?: {
16
      disabled?: boolean;
17
      max_age?: number;
18
      stale_while_revalidate?: number;
19
      [k: string]: unknown;
20
    };
21
    name: string;
22
    origin: {
23
      database?: string;
24
      host?: string;
25
      port?: number;
26
      scheme?: "postgres" | "postgresql";
27
      user?: string;
28
      [k: string]: unknown;
29
    } & { [k: string]: unknown };
30
    [k: string]: unknown;
31
  } & { password: string; [k: string]: unknown }
32
) {
33
  const url = new URL(
34
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/hyperdrive/configs/${hyperdrive_identifier}`
35
  );
36

37
  const response = await fetch(url, {
38
    method: "PUT",
39
    headers: {
40
      "X-AUTH-EMAIL": auth.email,
41
      "X-AUTH-KEY": auth.key,
42
      "Content-Type": "application/json",
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: JSON.stringify(body),
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53