Skip to content

Commit 593ccb7

Browse files
committed
optimise
1 parent 8fe3adb commit 593ccb7

3 files changed

Lines changed: 21 additions & 43 deletions

File tree

apps/web/src/components/nosql-explorer/connection-form.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Label } from "@/components/ui/label";
88
import { IconDatabase, IconTrash, IconHistory, IconPencil, IconPlugConnected, IconCheck, IconX, IconBrandMongodb, IconServer, IconCopy } from "@tabler/icons-react";
99
import { SavedConnection } from "./types";
1010
import { getConnections, deleteConnection, saveConnection, updateConnectionDetails } from "./connection-service";
11+
import { backendFetch } from "@/lib/backend-auth";
1112
import useAuth from "@/utils/useAuth";
1213
import { useMasterKeyStore } from "@/store/master-key-store";
1314
import { toast } from "sonner";
@@ -90,7 +91,7 @@ export function ConnectionForm({ onConnect, loading, error }: ConnectionFormProp
9091
if (!connectionString) return;
9192
setIsTesting(true);
9293
try {
93-
const res = await fetch("/api/nosql/connect", {
94+
const res = await backendFetch("/api/nosql/connect", {
9495
method: "POST",
9596
headers: { "Content-Type": "application/json" },
9697
body: JSON.stringify({ connectionString }),

apps/web/src/components/nosql-explorer/connection-service.ts

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { auth } from "@/database/firebase";
22
import { encryptData, decryptData } from "@/lib/encryption";
3+
import { proxyJsonAuthed } from "@/lib/backend-auth";
34
import { toast } from "sonner";
45
import { SavedConnection } from "./types";
56

@@ -8,52 +9,27 @@ const BACKEND_BASE_URL: string =
89
process.env.NEXT_PUBLIC_BACKEND_BASE_URL ||
910
"http://localhost:8000";
1011

11-
// ── proxy helper ──────────────────────────────────────────────────────────────
12-
13-
type ProxyResponse = {
14-
status: number;
15-
statusText: string;
16-
headers: Record<string, string>;
17-
body: string;
18-
isBase64: boolean;
19-
time: number;
20-
size: number;
21-
error?: string;
22-
};
12+
// ── proxy helper (with automatic token refresh on 401) ───────────────────────
2313

2414
const proxyRequest = async <T,>(
2515
method: string,
2616
path: string,
2717
body?: unknown
2818
): Promise<T> => {
29-
const currentUser = auth.currentUser;
30-
if (!currentUser) throw new Error("Not authenticated.");
31-
32-
const url = new URL(path, BACKEND_BASE_URL).toString();
33-
const headersObj: Record<string, string> = {};
34-
const proxyBody = body !== undefined ? JSON.stringify(body) : undefined;
35-
if (proxyBody !== undefined && method !== "GET" && method !== "HEAD") {
36-
headersObj["Content-Type"] = "application/json";
37-
}
19+
if (!auth.currentUser) throw new Error("Not authenticated.");
3820

39-
const proxyRes = await fetch("/api/proxy", {
40-
method: "POST",
41-
credentials: "include",
42-
headers: { "Content-Type": "application/json" },
43-
body: JSON.stringify({ url, method, headers: headersObj, body: proxyBody }),
44-
});
21+
const { status, data } = await proxyJsonAuthed<T>(BACKEND_BASE_URL, method, path, body);
4522

46-
const proxyData = (await proxyRes.json()) as ProxyResponse;
47-
if (proxyData.status < 200 || proxyData.status >= 300) {
48-
throw new Error(proxyData.body || proxyData.statusText || proxyData.error || "Request failed");
23+
if (status < 200 || status >= 300) {
24+
const err = data as Record<string, unknown> | null;
25+
throw new Error(
26+
(typeof err?.detail === "string" ? err.detail : null) ||
27+
(typeof err?.error === "string" ? err.error : null) ||
28+
`Request failed (${status})`
29+
);
4930
}
5031

51-
if (!proxyData.body) return undefined as T;
52-
try {
53-
return JSON.parse(proxyData.body) as T;
54-
} catch {
55-
return proxyData.body as unknown as T;
56-
}
32+
return data as T;
5733
};
5834

5935
// ── raw type returned by backend (no decrypted connectionString) ──────────────

apps/web/src/components/nosql-explorer/explorer-sidebar.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useInfiniteScroll } from "@/hooks/use-infinite-scroll";
1010
import useAuth from "@/utils/useAuth";
1111
import { useMasterKeyStore } from "@/store/master-key-store";
1212
import { getConnections, updateConnectionName, deleteConnection } from "./connection-service";
13+
import { backendFetch } from "@/lib/backend-auth";
1314
import { toast } from "sonner";
1415
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
1516
import {
@@ -138,7 +139,7 @@ export function ExplorerSidebar({
138139

139140
setConnections(prev => prev.map((c, i) => i === index ? { ...c, isLoading: true, error: null } : c));
140141
try {
141-
const res = await fetch("/api/nosql/connect", {
142+
const res = await backendFetch("/api/nosql/connect", {
142143
method: "POST",
143144
headers: { "Content-Type": "application/json" },
144145
body: JSON.stringify({ connectionString: connection.connectionString }),
@@ -198,7 +199,7 @@ export function ExplorerSidebar({
198199
const refreshCollections = async (connIndex: number, dbName: string) => {
199200
const node = connections[connIndex];
200201
try {
201-
const res = await fetch("/api/nosql/collections", {
202+
const res = await backendFetch("/api/nosql/collections", {
202203
method: "POST",
203204
headers: { "Content-Type": "application/json" },
204205
body: JSON.stringify({
@@ -264,7 +265,7 @@ export function ExplorerSidebar({
264265

265266
const node = connections[connIndex];
266267
try {
267-
const res = await fetch("/api/nosql/database/drop", {
268+
const res = await backendFetch("/api/nosql/database/drop", {
268269
method: "POST",
269270
headers: { "Content-Type": "application/json" },
270271
body: JSON.stringify({ connectionString: node.connection.connectionString, dbName }),
@@ -284,7 +285,7 @@ export function ExplorerSidebar({
284285

285286
const node = connections[connIndex];
286287
try {
287-
const res = await fetch("/api/nosql/collection/drop", {
288+
const res = await backendFetch("/api/nosql/collection/drop", {
288289
method: "POST",
289290
headers: { "Content-Type": "application/json" },
290291
body: JSON.stringify({ connectionString: node.connection.connectionString, dbName, collectionName }),
@@ -304,7 +305,7 @@ export function ExplorerSidebar({
304305
if (!connection || !newName) return;
305306

306307
try {
307-
const res = await fetch("/api/nosql/collection/rename", {
308+
const res = await backendFetch("/api/nosql/collection/rename", {
308309
method: "POST",
309310
headers: { "Content-Type": "application/json" },
310311
body: JSON.stringify({
@@ -335,7 +336,7 @@ export function ExplorerSidebar({
335336
if (!connection || !newName) return;
336337

337338
try {
338-
const res = await fetch("/api/nosql/database/rename", {
339+
const res = await backendFetch("/api/nosql/database/rename", {
339340
method: "POST",
340341
headers: { "Content-Type": "application/json" },
341342
body: JSON.stringify({

0 commit comments

Comments
 (0)