From cbe6075fc885aa049eb1d34a8a9c7c4735cda1a0 Mon Sep 17 00:00:00 2001 From: gilangjavier Date: Wed, 25 Mar 2026 20:16:51 +0700 Subject: [PATCH] fix(lib): normalize backend baseURL to avoid /v3/v3 Prevents invalid API URLs when NEXT_PUBLIC_BACKEND_URL already includes /v3 (fixes dashboard JSON parse errors). --- packages/lib/api.ts | 12 +++++++++++- packages/lib/auth.ts | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/lib/api.ts b/packages/lib/api.ts index 777feb08..89f93bfc 100644 --- a/packages/lib/api.ts +++ b/packages/lib/api.ts @@ -227,8 +227,18 @@ export const apiSchema = createSchema({ }, }) +function normalizeBackendV3BaseURL(rawBaseURL: string): string { + // Allow passing either "https://api.supermemory.ai" or "https://api.supermemory.ai/v3" + // (and tolerate trailing slashes) without accidentally producing "/v3/v3". + const trimmed = rawBaseURL.replace(/\/+$/, "") + return trimmed.endsWith("/v3") ? trimmed : `${trimmed}/v3` +} + +const backendBaseURL = + process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai" + export const $fetch = createFetch({ - baseURL: `${process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai"}/v3`, + baseURL: normalizeBackendV3BaseURL(backendBaseURL), credentials: "include", retry: { attempts: 3, diff --git a/packages/lib/auth.ts b/packages/lib/auth.ts index 4369bef1..738be426 100644 --- a/packages/lib/auth.ts +++ b/packages/lib/auth.ts @@ -9,8 +9,18 @@ import { } from "better-auth/client/plugins" import { createAuthClient } from "better-auth/react" +function normalizeBackendBaseURL(rawBaseURL: string): string { + // Accept either "https://api.supermemory.ai" or "https://api.supermemory.ai/v3". + // Auth endpoints are mounted at the root base (not under /v3). + const trimmed = rawBaseURL.replace(/\/+$/, "") + return trimmed.endsWith("/v3") ? trimmed.slice(0, -3) : trimmed +} + +const backendBaseURL = + process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai" + export const authClient = createAuthClient({ - baseURL: process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai", + baseURL: normalizeBackendBaseURL(backendBaseURL), fetchOptions: { credentials: "include", throw: true,