0
Fork 0
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:
Arsh 2024-03-14 23:58:51 +05:30 committed by GitHub
parent f92ff40fd3
commit 238f047b9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 33 additions and 24 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---
Fixes an issue where `astro:db` could not be used in serverless environments.

View file

@ -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();

View file

@ -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,

View file

@ -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);

View file

@ -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'));

View file

@ -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 };

View file

@ -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;

View 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;
}