summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Arsh <69170106+lilnasy@users.noreply.github.com> 2024-03-14 23:58:51 +0530
committerGravatar GitHub <noreply@github.com> 2024-03-14 14:28:51 -0400
commit238f047b9d1ebc407f53d61ee61574b380a76ac9 (patch)
tree6883382d984b84883e56e88ebab32737e608aa34
parentf92ff40fd31066632e575b0bf971a651989b7462 (diff)
downloadastro-238f047b9d1ebc407f53d61ee61574b380a76ac9.tar.gz
astro-238f047b9d1ebc407f53d61ee61574b380a76ac9.tar.zst
astro-238f047b9d1ebc407f53d61ee61574b380a76ac9.zip
fix(db): separate utils for runtime (#10443)
* fix(db): separate utils for runtime * add changeset * use safeFetch from runtime utils
-rw-r--r--.changeset/few-teachers-impress.md5
-rw-r--r--packages/db/src/core/cli/commands/link/index.ts3
-rw-r--r--packages/db/src/core/cli/commands/push/index.ts3
-rw-r--r--packages/db/src/core/cli/migration-queries.ts4
-rw-r--r--packages/db/src/core/tokens.ts3
-rw-r--r--packages/db/src/core/utils.ts19
-rw-r--r--packages/db/src/runtime/db-client.ts2
-rw-r--r--packages/db/src/runtime/utils.ts18
8 files changed, 33 insertions, 24 deletions
diff --git a/.changeset/few-teachers-impress.md b/.changeset/few-teachers-impress.md
new file mode 100644
index 000000000..0fdcec9bf
--- /dev/null
+++ b/.changeset/few-teachers-impress.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/db": patch
+---
+
+Fixes an issue where `astro:db` could not be used in serverless environments.
diff --git a/packages/db/src/core/cli/commands/link/index.ts b/packages/db/src/core/cli/commands/link/index.ts
index c65f6ef65..0dae1c218 100644
--- a/packages/db/src/core/cli/commands/link/index.ts
+++ b/packages/db/src/core/cli/commands/link/index.ts
@@ -7,7 +7,8 @@ import ora from 'ora';
import prompts from 'prompts';
import { MISSING_SESSION_ID_ERROR } from '../../../errors.js';
import { PROJECT_ID_FILE, getSessionIdFromFile } from '../../../tokens.js';
-import { type Result, getAstroStudioUrl, safeFetch } from '../../../utils.js';
+import { type Result, getAstroStudioUrl } from '../../../utils.js';
+import { safeFetch } from '../../../../runtime/utils.js';
export async function cmd() {
const sessionToken = await getSessionIdFromFile();
diff --git a/packages/db/src/core/cli/commands/push/index.ts b/packages/db/src/core/cli/commands/push/index.ts
index 760ec7986..fed1e1a70 100644
--- a/packages/db/src/core/cli/commands/push/index.ts
+++ b/packages/db/src/core/cli/commands/push/index.ts
@@ -3,7 +3,8 @@ import type { Arguments } from 'yargs-parser';
import { MIGRATION_VERSION } from '../../../consts.js';
import { getManagedAppTokenOrExit } from '../../../tokens.js';
import { type DBConfig, type DBSnapshot } from '../../../types.js';
-import { type Result, getRemoteDatabaseUrl, safeFetch } from '../../../utils.js';
+import { type Result, getRemoteDatabaseUrl } from '../../../utils.js';
+import { safeFetch } from '../../../../runtime/utils.js';
import {
createCurrentSnapshot,
createEmptySnapshot,
diff --git a/packages/db/src/core/cli/migration-queries.ts b/packages/db/src/core/cli/migration-queries.ts
index d8b27db0d..0474f9bd1 100644
--- a/packages/db/src/core/cli/migration-queries.ts
+++ b/packages/db/src/core/cli/migration-queries.ts
@@ -32,7 +32,9 @@ import {
type NumberColumn,
type TextColumn,
} from '../types.js';
-import { type Result, getRemoteDatabaseUrl, safeFetch } from '../utils.js';
+import { type Result, getRemoteDatabaseUrl } from '../utils.js';
+import { safeFetch } from '../../runtime/utils.js';
+
const sqlite = new SQLiteAsyncDialect();
const genTempTableName = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10);
diff --git a/packages/db/src/core/tokens.ts b/packages/db/src/core/tokens.ts
index 314296b43..88bb26ec5 100644
--- a/packages/db/src/core/tokens.ts
+++ b/packages/db/src/core/tokens.ts
@@ -5,7 +5,8 @@ import { pathToFileURL } from 'node:url';
import { green } from 'kleur/colors';
import ora from 'ora';
import { MISSING_PROJECT_ID_ERROR, MISSING_SESSION_ID_ERROR } from './errors.js';
-import { getAstroStudioEnv, getAstroStudioUrl, safeFetch } from './utils.js';
+import { getAstroStudioEnv, getAstroStudioUrl } from './utils.js';
+import { safeFetch } from '../runtime/utils.js';
export const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), '.astro', 'session-token'));
export const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), '.astro', 'link'));
diff --git a/packages/db/src/core/utils.ts b/packages/db/src/core/utils.ts
index 549a8c654..784f60aa7 100644
--- a/packages/db/src/core/utils.ts
+++ b/packages/db/src/core/utils.ts
@@ -27,23 +27,4 @@ export function defineDbIntegration(integration: AstroDbIntegration): AstroInteg
return integration;
}
-/**
- * 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.
- */
-export async function safeFetch(
- url: Parameters<typeof fetch>[0],
- options: Parameters<typeof fetch>[1] = {},
- onNotOK: (response: Response) => void | Promise<void> = () => {
- throw new Error(`Request to ${url} returned a non-OK status code.`);
- }
-): Promise<Response> {
- const response = await fetch(url, options);
-
- if (!response.ok) {
- await onNotOK(response);
- }
-
- return response;
-}
-
export type Result<T> = { success: true; data: T } | { success: false; data: unknown };
diff --git a/packages/db/src/runtime/db-client.ts b/packages/db/src/runtime/db-client.ts
index 6695779a1..e3d142dd2 100644
--- a/packages/db/src/runtime/db-client.ts
+++ b/packages/db/src/runtime/db-client.ts
@@ -4,7 +4,7 @@ import type { LibSQLDatabase } from 'drizzle-orm/libsql';
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql';
import { drizzle as drizzleProxy } from 'drizzle-orm/sqlite-proxy';
import { z } from 'zod';
-import { safeFetch } from '../core/utils.js';
+import { safeFetch } from './utils.js';
const isWebContainer = !!process.versions?.webcontainer;
diff --git a/packages/db/src/runtime/utils.ts b/packages/db/src/runtime/utils.ts
new file mode 100644
index 000000000..32bc60a45
--- /dev/null
+++ b/packages/db/src/runtime/utils.ts
@@ -0,0 +1,18 @@
+/**
+ * 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.
+ */
+export async function safeFetch(
+ url: Parameters<typeof fetch>[0],
+ options: Parameters<typeof fetch>[1] = {},
+ onNotOK: (response: Response) => void | Promise<void> = () => {
+ throw new Error(`Request to ${url} returned a non-OK status code.`);
+ }
+): Promise<Response> {
+ const response = await fetch(url, options);
+
+ if (!response.ok) {
+ await onNotOK(response);
+ }
+
+ return response;
+} \ No newline at end of file