diff --git a/.changeset/few-teachers-impress.md b/.changeset/few-teachers-impress.md new file mode 100644 index 0000000000..0fdcec9bf1 --- /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 c65f6ef657..0dae1c2189 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 760ec7986e..fed1e1a70c 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 d8b27db0dc..0474f9bd1c 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 314296b43e..88bb26ec5d 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 549a8c6540..784f60aa7c 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[0], - options: Parameters[1] = {}, - onNotOK: (response: Response) => void | Promise = () => { - throw new Error(`Request to ${url} returned a non-OK status code.`); - } -): Promise { - const response = await fetch(url, options); - - if (!response.ok) { - await onNotOK(response); - } - - return response; -} - export type Result = { 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 6695779a17..e3d142dd2f 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 0000000000..32bc60a45a --- /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[0], + options: Parameters[1] = {}, + onNotOK: (response: Response) => void | Promise = () => { + throw new Error(`Request to ${url} returned a non-OK status code.`); + } +): Promise { + const response = await fetch(url, options); + + if (!response.ok) { + await onNotOK(response); + } + + return response; +} \ No newline at end of file