diff --git a/.changeset/node-sdk-config-file-env-path.md b/.changeset/node-sdk-config-file-env-path.md new file mode 100644 index 00000000..09269542 --- /dev/null +++ b/.changeset/node-sdk-config-file-env-path.md @@ -0,0 +1,5 @@ +--- +"@reflag/node-sdk": patch +--- + +Fix `REFLAG_CONFIG_FILE` handling so the SDK loads the config file from the path provided by the environment variable. diff --git a/packages/node-sdk/src/client.ts b/packages/node-sdk/src/client.ts index 89735ba2..1bcfbe71 100644 --- a/packages/node-sdk/src/client.ts +++ b/packages/node-sdk/src/client.ts @@ -387,12 +387,18 @@ export class ReflagClient { "flagsPushUrl must be a non-empty string", ); + const envConfigFile = + typeof process.env.REFLAG_CONFIG_FILE === "string" && + process.env.REFLAG_CONFIG_FILE.length > 0 + ? process.env.REFLAG_CONFIG_FILE + : undefined; + if (!options.configFile) { options.configFile = - (process.env.REFLAG_CONFIG_FILE ?? - fs.existsSync(reflagConfigDefaultFile)) + envConfigFile ?? + (fs.existsSync(reflagConfigDefaultFile) ? reflagConfigDefaultFile - : undefined; + : undefined); } const externalConfig = loadConfig(options.configFile); diff --git a/packages/node-sdk/test/client.test.ts b/packages/node-sdk/test/client.test.ts index e2cf65cb..98ab44fc 100644 --- a/packages/node-sdk/test/client.test.ts +++ b/packages/node-sdk/test/client.test.ts @@ -1,3 +1,7 @@ +import { mkdtempSync, rmSync, writeFileSync } from "fs"; +import os from "os"; +import path from "path"; + import flushPromises from "flush-promises"; import { afterEach, @@ -261,6 +265,36 @@ describe("ReflagClient", () => { }); }); + it("should use the REFLAG_CONFIG_FILE path when set", () => { + const tempDir = mkdtempSync(path.join(os.tmpdir(), "reflag-node-sdk-")); + const originalConfigFile = process.env.REFLAG_CONFIG_FILE; + const customConfigFile = path.join(tempDir, "custom.reflag.config.json"); + + writeFileSync( + customConfigFile, + JSON.stringify({ apiBaseUrl: "https://custom-config.example/" }), + ); + + process.env.REFLAG_CONFIG_FILE = customConfigFile; + + try { + const client = new ReflagClient({ + offline: true, + }); + + expect(client["_config"].apiBaseUrl).toBe( + "https://custom-config.example/", + ); + } finally { + if (originalConfigFile === undefined) { + delete process.env.REFLAG_CONFIG_FILE; + } else { + process.env.REFLAG_CONFIG_FILE = originalConfigFile; + } + rmSync(tempDir, { recursive: true, force: true }); + } + }); + it("should create a client instance with valid options", () => { const client = new ReflagClient(validOptions);