From 8ac6707a83d1d2975bdb9a5dcaec0e526d3b0853 Mon Sep 17 00:00:00 2001 From: kwasniew Date: Wed, 18 Oct 2023 15:29:44 +0200 Subject: [PATCH] feat: default session id --- src/client.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/client.ts b/src/client.ts index 1ee15bc..c71c5f4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -100,9 +100,13 @@ class Client extends EventEmitter implements IClient { const definitions = this.unleash.getFeatureToggleDefinitions() || []; return definitions.map((d) => { - const enabled = this.unleash.isEnabled(d.name, context); + const sessionId = context.sessionId || String(Math.random()); + const enabled = this.unleash.isEnabled(d.name, { + ...context, + sessionId, + }); const variant = enabled - ? this.unleash.forceGetVariant(d.name, context) + ? this.unleash.getVariant(d.name, { ...context, sessionId }) : getDefaultVariant(); return { @@ -121,13 +125,19 @@ class Client extends EventEmitter implements IClient { ); const context = this.fixContext(inContext); + const sessionId = context.sessionId || String(Math.random()); const definitions = this.unleash.getFeatureToggleDefinitions() || []; return definitions - .filter((d) => this.unleash.isEnabled(d.name, context)) + .filter((d) => + this.unleash.isEnabled(d.name, { ...context, sessionId }), + ) .map((d) => ({ name: d.name, enabled: true, - variant: this.unleash.forceGetVariant(d.name, context), + variant: this.unleash.getVariant(d.name, { + ...context, + sessionId, + }), impressionData: d.impressionData, })); }