diff --git a/.changeset/node-sdk-bind-client-preserve-context.md b/.changeset/node-sdk-bind-client-preserve-context.md new file mode 100644 index 00000000..9d57ee13 --- /dev/null +++ b/.changeset/node-sdk-bind-client-preserve-context.md @@ -0,0 +1,5 @@ +--- +"@reflag/node-sdk": patch +--- + +Fix `BoundReflagClient.bindClient()` so omitted `user`, `company`, and `other` fields preserve the previously bound context instead of being cleared. diff --git a/packages/node-sdk/src/client.ts b/packages/node-sdk/src/client.ts index 89735ba2..1c1ab7fc 100644 --- a/packages/node-sdk/src/client.ts +++ b/packages/node-sdk/src/client.ts @@ -1832,9 +1832,18 @@ export class BoundReflagClient { // merge new context into existing const boundConfig = { ...this._options, - user: user ? { ...this._options.user, ...user } : undefined, - company: company ? { ...this._options.company, ...company } : undefined, - other: { ...this._options.other, ...other }, + user: + user === undefined + ? this._options.user + : { ...this._options.user, ...user }, + company: + company === undefined + ? this._options.company + : { ...this._options.company, ...company }, + other: + other === undefined + ? this._options.other + : { ...this._options.other, ...other }, enableTracking: enableTracking ?? this._options.enableTracking, meta: meta ?? this._options.meta, }; diff --git a/packages/node-sdk/test/client.test.ts b/packages/node-sdk/test/client.test.ts index e2cf65cb..d7819c93 100644 --- a/packages/node-sdk/test/client.test.ts +++ b/packages/node-sdk/test/client.test.ts @@ -3035,6 +3035,31 @@ describe("BoundReflagClient", () => { }); }); + it("should preserve existing user and company when omitted on rebind", () => { + const reboundClient = client + .bindClient({ user, company }) + .bindClient({ other: otherContext }); + + expect(reboundClient["_options"]).toEqual({ + user, + company, + other: otherContext, + enableTracking: true, + }); + }); + + it("should preserve existing other context when omitted on rebind", () => { + const reboundClient = client + .bindClient({ other: otherContext }) + .bindClient({ user }); + + expect(reboundClient["_options"]).toEqual({ + user, + other: otherContext, + enableTracking: true, + }); + }); + it("should allow using expected methods when bound to user", async () => { const boundClient = client.bindClient({ user }); expect(boundClient.user).toEqual(user);