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-bind-client-preserve-context.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 12 additions & 3 deletions packages/node-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
25 changes: 25 additions & 0 deletions packages/node-sdk/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading