summaryrefslogtreecommitdiff
path: root/packages/db/src/runtime/db-client.ts
diff options
context:
space:
mode:
authorGravatar Erika <3019731+Princesseuh@users.noreply.github.com> 2024-03-13 17:12:52 +0100
committerGravatar GitHub <noreply@github.com> 2024-03-13 17:12:52 +0100
commit2db25c05a467f2ffd6ebff5eb82076449fa9d72f (patch)
treea352c1c6e6a7448d4d42e8f4fd85d6caad52edaf /packages/db/src/runtime/db-client.ts
parent001f7374d83cf2930f1e4bfbc5d3bb0a75b176ba (diff)
downloadastro-2db25c05a467f2ffd6ebff5eb82076449fa9d72f.tar.gz
astro-2db25c05a467f2ffd6ebff5eb82076449fa9d72f.tar.zst
astro-2db25c05a467f2ffd6ebff5eb82076449fa9d72f.zip
fix(db): Add a safe db fetch wrapper (#10420)
* fix(db): Add a safe db fetch wrapper * chore: changeset
Diffstat (limited to 'packages/db/src/runtime/db-client.ts')
-rw-r--r--packages/db/src/runtime/db-client.ts55
1 files changed, 31 insertions, 24 deletions
diff --git a/packages/db/src/runtime/db-client.ts b/packages/db/src/runtime/db-client.ts
index db8535e15..6695779a1 100644
--- a/packages/db/src/runtime/db-client.ts
+++ b/packages/db/src/runtime/db-client.ts
@@ -4,6 +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';
const isWebContainer = !!process.versions?.webcontainer;
@@ -29,19 +30,22 @@ export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string
const db = drizzleProxy(
async (sql, parameters, method) => {
const requestBody: InStatement = { sql, args: parameters };
- const res = await fetch(url, {
- method: 'POST',
- headers: {
- Authorization: `Bearer ${appToken}`,
- 'Content-Type': 'application/json',
+ const res = await safeFetch(
+ url,
+ {
+ method: 'POST',
+ headers: {
+ Authorization: `Bearer ${appToken}`,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(requestBody),
},
- body: JSON.stringify(requestBody),
- });
- if (!res.ok) {
- throw new Error(
- `Failed to execute query.\nQuery: ${sql}\nFull error: ${res.status} ${await res.text()}}`
- );
- }
+ (response) => {
+ throw new Error(
+ `Failed to execute query.\nQuery: ${sql}\nFull error: ${response.status} ${response.statusText}`
+ );
+ }
+ );
let remoteResult: z.infer<typeof remoteResultSchema>;
try {
@@ -74,19 +78,22 @@ export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string
},
async (queries) => {
const stmts: InStatement[] = queries.map(({ sql, params }) => ({ sql, args: params }));
- const res = await fetch(url, {
- method: 'POST',
- headers: {
- Authorization: `Bearer ${appToken}`,
- 'Content-Type': 'application/json',
+ const res = await safeFetch(
+ url,
+ {
+ method: 'POST',
+ headers: {
+ Authorization: `Bearer ${appToken}`,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(stmts),
},
- body: JSON.stringify(stmts),
- });
- if (!res.ok) {
- throw new Error(
- `Failed to execute batch queries.\nFull error: ${res.status} ${await res.text()}}`
- );
- }
+ (response) => {
+ throw new Error(
+ `Failed to execute batch queries.\nFull error: ${response.status} ${response.statusText}}`
+ );
+ }
+ );
let remoteResults: z.infer<typeof remoteResultSchema>[];
try {