Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/node-sdk-config-file-env-path.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 9 additions & 3 deletions packages/node-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions packages/node-sdk/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);

Expand Down
Loading