Create Hyperdrive

Creates and returns a new 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
 * Create Hyperdrive
8
 * Creates and returns a new Hyperdrive configuration.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body: {
14
    caching?: {
15
      disabled?: boolean;
16
      max_age?: number;
17
      stale_while_revalidate?: number;
18
      [k: string]: unknown;
19
    };
20
    name: string;
21
    origin: {
22
      database?: string;
23
      host?: string;
24
      port?: number;
25
      scheme?: "postgres" | "postgresql";
26
      user?: string;
27
      [k: string]: unknown;
28
    } & { [k: string]: unknown };
29
    [k: string]: unknown;
30
  } & { password: string; [k: string]: unknown }
31
) {
32
  const url = new URL(
33
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/hyperdrive/configs`
34
  );
35

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