diff --git a/src/index.ts b/src/index.ts index d186e5a..53463a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,9 +19,13 @@ import * as fs from "fs"; import * as path from "path"; // Configuration - can be overridden via environment variables -// API_BASE_URL should point to the base (e.g., http://192.168.1.16:8001) -// The /api/v1 prefix is added by apiRequest -const API_BASE_URL = (process.env.SECOND_BRAIN_API_URL || "http://192.168.1.16:8001").replace(/\/+$/, ""); +// API_BASE_URL can be: +// - http://192.168.1.16:8001 (internal, /api/v1 prefix added) +// - https://2brain.coer.nl (external, /api/v1 prefix added) +// - https://2brain.coer.nl/api (external with /api, only /v1 added) +const RAW_API_URL = (process.env.SECOND_BRAIN_API_URL || "http://192.168.1.16:8001").replace(/\/+$/, ""); +const API_BASE_URL = RAW_API_URL.endsWith("/api") ? RAW_API_URL : RAW_API_URL; +const API_HAS_PREFIX = RAW_API_URL.endsWith("/api"); const API_USERNAME = process.env.SECOND_BRAIN_USERNAME || ""; const API_PASSWORD = process.env.SECOND_BRAIN_PASSWORD || ""; @@ -42,10 +46,19 @@ async function apiRequest( endpoint: string, options: RequestInit = {} ): Promise { - // Ensure endpoint starts with /api/v1 - const normalizedEndpoint = endpoint.startsWith("/api/v1") - ? endpoint - : `/api${endpoint.startsWith("/v1") ? endpoint : `/v1${endpoint}`}`; + // Normalize endpoint to /v1/... format + let normalizedEndpoint = endpoint; + if (endpoint.startsWith("/api/v1")) { + normalizedEndpoint = endpoint.substring(4); // Remove /api prefix + } else if (!endpoint.startsWith("/v1")) { + normalizedEndpoint = `/v1${endpoint}`; + } + + // Add /api prefix only if base URL doesn't already have it + if (!API_HAS_PREFIX) { + normalizedEndpoint = `/api${normalizedEndpoint}`; + } + const url = `${API_BASE_URL}${normalizedEndpoint}`; const headers = { ...getAuthHeader(),