Skip to content

Commit

Permalink
fix(db): separate utils for runtime (#10443)
Browse files Browse the repository at this point in the history
* fix(db): separate utils for runtime

* add changeset

* use safeFetch from runtime utils
  • Loading branch information
lilnasy authored Mar 14, 2024
1 parent f92ff40 commit 238f047
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-teachers-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Fixes an issue where `astro:db` could not be used in serverless environments.
3 changes: 2 additions & 1 deletion packages/db/src/core/cli/commands/link/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion packages/db/src/core/cli/commands/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion packages/db/src/core/cli/migration-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion packages/db/src/core/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
19 changes: 0 additions & 19 deletions packages/db/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion packages/db/src/runtime/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 18 additions & 0 deletions packages/db/src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 238f047

Please sign in to comment.