@astrojs/db 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
3
+ */
4
+ export declare function safeFetch(url: Parameters<typeof fetch>[0], options?: Parameters<typeof fetch>[1], onNotOK?: (response: Response) => void | Promise<void>): Promise<Response>;
@@ -5,9 +5,10 @@ import { slug } from "github-slugger";
5
5
  import { bgRed, cyan } from "kleur/colors";
6
6
  import ora from "ora";
7
7
  import prompts from "prompts";
8
+ import { safeFetch } from "../../../../runtime/utils.js";
8
9
  import { MISSING_SESSION_ID_ERROR } from "../../../errors.js";
9
10
  import { PROJECT_ID_FILE, getSessionIdFromFile } from "../../../tokens.js";
10
- import { getAstroStudioUrl, safeFetch } from "../../../utils.js";
11
+ import { getAstroStudioUrl } from "../../../utils.js";
11
12
  async function cmd() {
12
13
  const sessionToken = await getSessionIdFromFile();
13
14
  if (!sessionToken) {
@@ -225,7 +226,11 @@ async function promptNewProjectRegion() {
225
226
  message: `Where should your new database live?`,
226
227
  choices: [
227
228
  { title: "North America (East)", value: "NorthAmericaEast" },
228
- { title: "North America (West)", value: "NorthAmericaWest" }
229
+ { title: "North America (West)", value: "NorthAmericaWest" },
230
+ { title: "Europe (Amsterdam)", value: "EuropeCentral" },
231
+ { title: "South America (Brazil)", value: "SouthAmericaEast" },
232
+ { title: "Asia (India)", value: "AsiaSouth" },
233
+ { title: "Asia (Japan)", value: "AsiaNorthEast" }
229
234
  ],
230
235
  initial: 0
231
236
  });
@@ -1,7 +1,8 @@
1
+ import { safeFetch } from "../../../../runtime/utils.js";
1
2
  import { MIGRATION_VERSION } from "../../../consts.js";
2
3
  import { getManagedAppTokenOrExit } from "../../../tokens.js";
3
4
  import {} from "../../../types.js";
4
- import { getRemoteDatabaseUrl, safeFetch } from "../../../utils.js";
5
+ import { getRemoteDatabaseUrl } from "../../../utils.js";
5
6
  import {
6
7
  createCurrentSnapshot,
7
8
  createEmptySnapshot,
@@ -14,12 +14,13 @@ import {
14
14
  schemaTypeToSqlType
15
15
  } from "../../runtime/queries.js";
16
16
  import { isSerializedSQL } from "../../runtime/types.js";
17
+ import { safeFetch } from "../../runtime/utils.js";
17
18
  import { MIGRATION_VERSION } from "../consts.js";
18
19
  import { RENAME_COLUMN_ERROR, RENAME_TABLE_ERROR } from "../errors.js";
19
20
  import { columnSchema } from "../schemas.js";
20
21
  import {
21
22
  } from "../types.js";
22
- import { getRemoteDatabaseUrl, safeFetch } from "../utils.js";
23
+ import { getRemoteDatabaseUrl } from "../utils.js";
23
24
  const sqlite = new SQLiteAsyncDialect();
24
25
  const genTempTableName = customAlphabet("abcdefghijklmnopqrstuvwxyz", 10);
25
26
  async function getMigrationQueries({
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.8.2"}`
30
+ `v${"0.8.4"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -2,8 +2,11 @@ import { readFile } from "node:fs/promises";
2
2
  import { homedir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import { pathToFileURL } from "node:url";
5
+ import { green } from "kleur/colors";
6
+ import ora from "ora";
7
+ import { safeFetch } from "../runtime/utils.js";
5
8
  import { MISSING_PROJECT_ID_ERROR, MISSING_SESSION_ID_ERROR } from "./errors.js";
6
- import { getAstroStudioEnv, getAstroStudioUrl, getRemoteDatabaseUrl, safeFetch } from "./utils.js";
9
+ import { getAstroStudioEnv, getAstroStudioUrl } from "./utils.js";
7
10
  const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), ".astro", "session-token"));
8
11
  const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), ".astro", "link"));
9
12
  class ManagedLocalAppToken {
@@ -19,17 +22,11 @@ class ManagedLocalAppToken {
19
22
  class ManagedRemoteAppToken {
20
23
  token;
21
24
  session;
22
- region;
23
25
  projectId;
24
26
  ttl;
25
27
  renewTimer;
26
- static async getRegionCode() {
27
- const pingResponse = await safeFetch(new URL(`${getRemoteDatabaseUrl()}/ping`));
28
- const pingResult = await pingResponse.json();
29
- return pingResult.data.region;
30
- }
31
28
  static async create(sessionToken, projectId) {
32
- const region = await ManagedRemoteAppToken.getRegionCode();
29
+ const spinner = ora("Connecting to remote database...").start();
33
30
  const response = await safeFetch(
34
31
  new URL(`${getAstroStudioUrl()}/auth/cli/token-create`),
35
32
  {
@@ -37,17 +34,18 @@ class ManagedRemoteAppToken {
37
34
  headers: new Headers({
38
35
  Authorization: `Bearer ${sessionToken}`
39
36
  }),
40
- body: JSON.stringify({ projectId, region })
37
+ body: JSON.stringify({ projectId })
41
38
  },
42
39
  (res) => {
43
40
  throw new Error(`Failed to create token: ${res.status} ${res.statusText}`);
44
41
  }
45
42
  );
43
+ await new Promise((resolve) => setTimeout(resolve, 2e3));
44
+ spinner.succeed(green("Connected to remote database."));
46
45
  const { token: shortLivedAppToken, ttl } = await response.json();
47
46
  return new ManagedRemoteAppToken({
48
47
  token: shortLivedAppToken,
49
48
  session: sessionToken,
50
- region,
51
49
  projectId,
52
50
  ttl
53
51
  });
@@ -55,7 +53,6 @@ class ManagedRemoteAppToken {
55
53
  constructor(options) {
56
54
  this.token = options.token;
57
55
  this.session = options.session;
58
- this.region = options.region;
59
56
  this.projectId = options.projectId;
60
57
  this.ttl = options.ttl;
61
58
  this.renewTimer = setTimeout(() => this.renew(), 1e3 * 60 * 5 / 2);
@@ -69,7 +66,7 @@ class ManagedRemoteAppToken {
69
66
  Authorization: `Bearer ${this.session}`,
70
67
  "Content-Type": "application/json"
71
68
  },
72
- body: JSON.stringify({ ...body, region: this.region })
69
+ body: JSON.stringify(body)
73
70
  },
74
71
  () => {
75
72
  throw new Error(`Failed to fetch ${url}.`);
@@ -6,10 +6,6 @@ export declare function getRemoteDatabaseUrl(): string;
6
6
  export declare function getAstroStudioUrl(): string;
7
7
  export declare function getDbDirectoryUrl(root: URL | string): URL;
8
8
  export declare function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration;
9
- /**
10
- * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
11
- */
12
- export declare function safeFetch(url: Parameters<typeof fetch>[0], options?: Parameters<typeof fetch>[1], onNotOK?: (response: Response) => void | Promise<void>): Promise<Response>;
13
9
  export type Result<T> = {
14
10
  success: true;
15
11
  data: T;
@@ -17,20 +17,10 @@ function getDbDirectoryUrl(root) {
17
17
  function defineDbIntegration(integration) {
18
18
  return integration;
19
19
  }
20
- async function safeFetch(url, options = {}, onNotOK = () => {
21
- throw new Error(`Request to ${url} returned a non-OK status code.`);
22
- }) {
23
- const response = await fetch(url, options);
24
- if (!response.ok) {
25
- await onNotOK(response);
26
- }
27
- return response;
28
- }
29
20
  export {
30
21
  defineDbIntegration,
31
22
  getAstroStudioEnv,
32
23
  getAstroStudioUrl,
33
24
  getDbDirectoryUrl,
34
- getRemoteDatabaseUrl,
35
- safeFetch
25
+ getRemoteDatabaseUrl
36
26
  };
@@ -2,7 +2,7 @@ import { createClient } from "@libsql/client";
2
2
  import { drizzle as drizzleLibsql } from "drizzle-orm/libsql";
3
3
  import { drizzle as drizzleProxy } from "drizzle-orm/sqlite-proxy";
4
4
  import { z } from "zod";
5
- import { safeFetch } from "../core/utils.js";
5
+ import { safeFetch } from "./utils.js";
6
6
  const isWebContainer = !!process.versions?.webcontainer;
7
7
  function createLocalDatabaseClient({ dbUrl }) {
8
8
  const url = isWebContainer ? "file:content.db" : dbUrl;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
3
+ */
4
+ export declare function safeFetch(url: Parameters<typeof fetch>[0], options?: Parameters<typeof fetch>[1], onNotOK?: (response: Response) => void | Promise<void>): Promise<Response>;
@@ -0,0 +1,12 @@
1
+ async function safeFetch(url, options = {}, onNotOK = () => {
2
+ throw new Error(`Request to ${url} returned a non-OK status code.`);
3
+ }) {
4
+ const response = await fetch(url, options);
5
+ if (!response.ok) {
6
+ await onNotOK(response);
7
+ }
8
+ return response;
9
+ }
10
+ export {
11
+ safeFetch
12
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -80,7 +80,7 @@
80
80
  "mocha": "^10.2.0",
81
81
  "typescript": "^5.2.2",
82
82
  "vite": "^5.1.4",
83
- "astro": "4.5.3",
83
+ "astro": "4.5.4",
84
84
  "astro-scripts": "0.0.14"
85
85
  },
86
86
  "scripts": {
@@ -1,19 +0,0 @@
1
- import type { AstroConfig, AstroIntegration } from 'astro';
2
- import type { AstroDbIntegration } from './types.js';
3
- export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
4
- export declare function getAstroStudioEnv(envMode?: string): Record<`ASTRO_STUDIO_${string}`, string>;
5
- export declare function getRemoteDatabaseUrl(): string;
6
- export declare function getAstroStudioUrl(): string;
7
- export declare function getDbDirectoryUrl(root: URL | string): URL;
8
- export declare function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration;
9
- /**
10
- * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
11
- */
12
- export declare function safeFetch(url: Parameters<typeof fetch>[0], options?: Parameters<typeof fetch>[1], onNotOK?: (response: Response) => void | Promise<void>): Promise<Response>;
13
- export type Result<T> = {
14
- success: true;
15
- data: T;
16
- } | {
17
- success: false;
18
- data: unknown;
19
- };