mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
fix(db): separate utils for runtime (#10443)
* fix(db): separate utils for runtime * add changeset * use safeFetch from runtime utils
This commit is contained in:
parent
f92ff40fd3
commit
238f047b9d
8 changed files with 33 additions and 24 deletions
5
.changeset/few-teachers-impress.md
Normal file
5
.changeset/few-teachers-impress.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/db": patch
|
||||
---
|
||||
|
||||
Fixes an issue where `astro:db` could not be used in serverless environments.
|
|
@ -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();
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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'));
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
18
packages/db/src/runtime/utils.ts
Normal file
18
packages/db/src/runtime/utils.ts
Normal file
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue