src / n8nConfig.ts
import { existsSync, readFileSync } from "fs";
import { join } from "path";
export interface N8nConfig {
api_url: string;
api_key: string;
api_version_path: string;
}
const DEFAULT_CONFIG: N8nConfig = {
api_url: "",
api_key: "",
api_version_path: "/api/v1",
};
let cachedConfig: N8nConfig | null = null;
export function loadN8nConfig(reload = false): N8nConfig {
if (cachedConfig !== null && !reload) {
return cachedConfig;
}
const configPath = join(__dirname, "..", "n8n-config.json");
if (existsSync(configPath)) {
try {
const raw = readFileSync(configPath, "utf-8");
const parsed = JSON.parse(raw) as Partial<N8nConfig>;
cachedConfig = {
api_url: parsed.api_url ?? DEFAULT_CONFIG.api_url,
api_key: parsed.api_key ?? DEFAULT_CONFIG.api_key,
api_version_path: parsed.api_version_path ?? DEFAULT_CONFIG.api_version_path,
};
} catch {
cachedConfig = { ...DEFAULT_CONFIG };
}
} else {
cachedConfig = { ...DEFAULT_CONFIG };
}
return cachedConfig;
}