@ -19,9 +19,13 @@ import * as fs from "fs";
import * as path from "path" ;
import * as path from "path" ;
// Configuration - can be overridden via environment variables
// Configuration - can be overridden via environment variables
// API_BASE_URL should point to the base (e.g., http://192.168.1.16:8001)
// API_BASE_URL can be:
// The /api/v1 prefix is added by apiRequest
// - http://192.168.1.16:8001 (internal, /api/v1 prefix added)
const API_BASE_URL = ( process . env . SECOND_BRAIN_API_URL || "http://192.168.1.16:8001" ) . replace ( /\/+$/ , "" ) ;
// - 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_USERNAME = process . env . SECOND_BRAIN_USERNAME || "" ;
const API_PASSWORD = process . env . SECOND_BRAIN_PASSWORD || "" ;
const API_PASSWORD = process . env . SECOND_BRAIN_PASSWORD || "" ;
@ -42,10 +46,19 @@ async function apiRequest(
endpoint : string ,
endpoint : string ,
options : RequestInit = { }
options : RequestInit = { }
) : Promise < Response > {
) : Promise < Response > {
// Ensure endpoint starts with /api/v1
// Normalize endpoint to /v1/... format
const normalizedEndpoint = endpoint . startsWith ( "/api/v1" )
let normalizedEndpoint = endpoint ;
? endpoint
if ( endpoint . startsWith ( "/api/v1" ) ) {
: ` /api ${ endpoint . startsWith ( "/v1" ) ? endpoint : ` /v1 ${ endpoint } ` } ` ;
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 url = ` ${ API_BASE_URL } ${ normalizedEndpoint } ` ;
const headers = {
const headers = {
. . . getAuthHeader ( ) ,
. . . getAuthHeader ( ) ,