@featurevisor/sdk 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/feature.ts CHANGED
@@ -19,6 +19,26 @@ export function getMatchedAllocation(
19
19
  return undefined;
20
20
  }
21
21
 
22
+ export function getMatchedTraffic(
23
+ traffic: Traffic[],
24
+ context: Context,
25
+ datafileReader: DatafileReader,
26
+ ): Traffic | undefined {
27
+ return traffic.find((t) => {
28
+ if (
29
+ !allGroupSegmentsAreMatched(
30
+ typeof t.segments === "string" && t.segments !== "*" ? JSON.parse(t.segments) : t.segments,
31
+ context,
32
+ datafileReader,
33
+ )
34
+ ) {
35
+ return false;
36
+ }
37
+
38
+ return true;
39
+ });
40
+ }
41
+
22
42
  export interface MatchedTrafficAndAllocation {
23
43
  matchedTraffic: Traffic | undefined;
24
44
  matchedAllocation: Allocation | undefined;
@@ -54,17 +54,16 @@ describe("sdk: instance", function () {
54
54
  features: [
55
55
  {
56
56
  key: "test",
57
- defaultVariation: false,
58
57
  bucketBy: "userId",
59
- variations: [{ value: true }, { value: false }],
58
+ variations: [{ value: "control" }, { value: "treatment" }],
60
59
  traffic: [
61
60
  {
62
61
  key: "1",
63
62
  segments: "*",
64
63
  percentage: 100000,
65
64
  allocation: [
66
- { variation: true, range: [0, 100000] },
67
- { variation: false, range: [0, 0] },
65
+ { variation: "control", range: [0, 100000] },
66
+ { variation: "treatment", range: [0, 0] },
68
67
  ],
69
68
  },
70
69
  ],
@@ -80,11 +79,13 @@ describe("sdk: instance", function () {
80
79
  },
81
80
  });
82
81
 
83
- const variation = sdk.getVariation("test", {
82
+ const featureKey = "test";
83
+ const context = {
84
84
  userId: "123",
85
- });
85
+ };
86
86
 
87
- expect(variation).toEqual(true);
87
+ expect(sdk.isEnabled(featureKey, context)).toEqual(true);
88
+ expect(sdk.getVariation(featureKey, context)).toEqual("control");
88
89
  expect(capturedBucketKey).toEqual("123.test");
89
90
  });
90
91
 
@@ -98,17 +99,16 @@ describe("sdk: instance", function () {
98
99
  features: [
99
100
  {
100
101
  key: "test",
101
- defaultVariation: false,
102
102
  bucketBy: ["userId", "organizationId"],
103
- variations: [{ value: true }, { value: false }],
103
+ variations: [{ value: "control" }, { value: "treatment" }],
104
104
  traffic: [
105
105
  {
106
106
  key: "1",
107
107
  segments: "*",
108
108
  percentage: 100000,
109
109
  allocation: [
110
- { variation: true, range: [0, 100000] },
111
- { variation: false, range: [0, 0] },
110
+ { variation: "control", range: [0, 100000] },
111
+ { variation: "treatment", range: [0, 0] },
112
112
  ],
113
113
  },
114
114
  ],
@@ -124,12 +124,13 @@ describe("sdk: instance", function () {
124
124
  },
125
125
  });
126
126
 
127
- const variation = sdk.getVariation("test", {
127
+ const featureKey = "test";
128
+ const context = {
128
129
  userId: "123",
129
130
  organizationId: "456",
130
- });
131
+ };
131
132
 
132
- expect(variation).toEqual(true);
133
+ expect(sdk.getVariation(featureKey, context)).toEqual("control");
133
134
  expect(capturedBucketKey).toEqual("123.456.test");
134
135
  });
135
136
 
@@ -143,17 +144,16 @@ describe("sdk: instance", function () {
143
144
  features: [
144
145
  {
145
146
  key: "test",
146
- defaultVariation: false,
147
147
  bucketBy: { or: ["userId", "deviceId"] },
148
- variations: [{ value: true }, { value: false }],
148
+ variations: [{ value: "control" }, { value: "treatment" }],
149
149
  traffic: [
150
150
  {
151
151
  key: "1",
152
152
  segments: "*",
153
153
  percentage: 100000,
154
154
  allocation: [
155
- { variation: true, range: [0, 100000] },
156
- { variation: false, range: [0, 0] },
155
+ { variation: "control", range: [0, 100000] },
156
+ { variation: "treatment", range: [0, 0] },
157
157
  ],
158
158
  },
159
159
  ],
@@ -170,18 +170,24 @@ describe("sdk: instance", function () {
170
170
  });
171
171
 
172
172
  expect(
173
- sdk.getVariation("test", {
173
+ sdk.isEnabled("test", {
174
174
  userId: "123",
175
175
  deviceId: "456",
176
176
  }),
177
177
  ).toEqual(true);
178
+ expect(
179
+ sdk.getVariation("test", {
180
+ userId: "123",
181
+ deviceId: "456",
182
+ }),
183
+ ).toEqual("control");
178
184
  expect(capturedBucketKey).toEqual("123.test");
179
185
 
180
186
  expect(
181
187
  sdk.getVariation("test", {
182
188
  deviceId: "456",
183
189
  }),
184
- ).toEqual(true);
190
+ ).toEqual("control");
185
191
  expect(capturedBucketKey).toEqual("456.test");
186
192
  });
187
193
 
@@ -195,17 +201,16 @@ describe("sdk: instance", function () {
195
201
  features: [
196
202
  {
197
203
  key: "test",
198
- defaultVariation: false,
199
204
  bucketBy: "userId",
200
- variations: [{ value: true }, { value: false }],
205
+ variations: [{ value: "control" }, { value: "treatment" }],
201
206
  traffic: [
202
207
  {
203
208
  key: "1",
204
209
  segments: "*",
205
210
  percentage: 100000,
206
211
  allocation: [
207
- { variation: true, range: [0, 100000] },
208
- { variation: false, range: [0, 0] },
212
+ { variation: "control", range: [0, 100000] },
213
+ { variation: "treatment", range: [0, 0] },
209
214
  ],
210
215
  },
211
216
  ],
@@ -227,7 +232,7 @@ describe("sdk: instance", function () {
227
232
  userId: "123",
228
233
  });
229
234
 
230
- expect(variation).toEqual(true);
235
+ expect(variation).toEqual("control");
231
236
  expect(intercepted).toEqual(true);
232
237
  });
233
238
 
@@ -241,17 +246,16 @@ describe("sdk: instance", function () {
241
246
  features: [
242
247
  {
243
248
  key: "test",
244
- defaultVariation: false,
245
249
  bucketBy: "userId",
246
- variations: [{ value: true }, { value: false }],
250
+ variations: [{ value: "control" }, { value: "treatment" }],
247
251
  traffic: [
248
252
  {
249
253
  key: "1",
250
254
  segments: "*",
251
255
  percentage: 100000,
252
256
  allocation: [
253
- { variation: true, range: [0, 100000] },
254
- { variation: false, range: [0, 0] },
257
+ { variation: "control", range: [0, 100000] },
258
+ { variation: "treatment", range: [0, 0] },
255
259
  ],
256
260
  },
257
261
  ],
@@ -270,14 +274,14 @@ describe("sdk: instance", function () {
270
274
  });
271
275
 
272
276
  expect(activated).toEqual(false);
273
- expect(variation).toEqual(true);
277
+ expect(variation).toEqual("control");
274
278
 
275
279
  const activatedVariation = sdk.activate("test", {
276
280
  userId: "123",
277
281
  });
278
282
 
279
283
  expect(activated).toEqual(true);
280
- expect(activatedVariation).toEqual(true);
284
+ expect(activatedVariation).toEqual("control");
281
285
  });
282
286
 
283
287
  it("should refresh datafile", function (done) {
@@ -292,17 +296,16 @@ describe("sdk: instance", function () {
292
296
  features: [
293
297
  {
294
298
  key: "test",
295
- defaultVariation: false,
296
299
  bucketBy: "userId",
297
- variations: [{ value: true }, { value: false }],
300
+ variations: [{ value: "control" }, { value: "treatment" }],
298
301
  traffic: [
299
302
  {
300
303
  key: "1",
301
304
  segments: "*",
302
305
  percentage: 100000,
303
306
  allocation: [
304
- { variation: true, range: [0, 100000] },
305
- { variation: false, range: [0, 0] },
307
+ { variation: "control", range: [0, 100000] },
308
+ { variation: "treatment", range: [0, 0] },
306
309
  ],
307
310
  },
308
311
  ],
@@ -351,7 +354,8 @@ describe("sdk: instance", function () {
351
354
  const sdk = createInstance({
352
355
  stickyFeatures: {
353
356
  test: {
354
- variation: false,
357
+ enabled: true,
358
+ variation: "control",
355
359
  },
356
360
  },
357
361
  datafileUrl: "http://localhost:3000/datafile.json",
@@ -362,17 +366,16 @@ describe("sdk: instance", function () {
362
366
  features: [
363
367
  {
364
368
  key: "test",
365
- defaultVariation: false,
366
369
  bucketBy: "userId",
367
- variations: [{ value: true }, { value: false }],
370
+ variations: [{ value: "control" }, { value: "treatment" }],
368
371
  traffic: [
369
372
  {
370
373
  key: "1",
371
374
  segments: "*",
372
375
  percentage: 100000,
373
376
  allocation: [
374
- { variation: true, range: [0, 100000] },
375
- { variation: false, range: [0, 0] },
377
+ { variation: "control", range: [0, 0] },
378
+ { variation: "treatment", range: [0, 100000] },
376
379
  ],
377
380
  },
378
381
  ],
@@ -390,28 +393,28 @@ describe("sdk: instance", function () {
390
393
  },
391
394
  });
392
395
 
393
- // initially false
396
+ // initially control
394
397
  expect(
395
398
  sdk.getVariation("test", {
396
399
  userId: "123",
397
400
  }),
398
- ).toEqual(false);
401
+ ).toEqual("control");
399
402
 
400
403
  setTimeout(function () {
401
- // still false after fetching datafile
404
+ // still control after fetching datafile
402
405
  expect(
403
406
  sdk.getVariation("test", {
404
407
  userId: "123",
405
408
  }),
406
- ).toEqual(false);
409
+ ).toEqual("control");
407
410
 
408
- // unsetting sticky features will make it true
411
+ // unsetting sticky features will make it treatment
409
412
  sdk.setStickyFeatures({});
410
413
  expect(
411
414
  sdk.getVariation("test", {
412
415
  userId: "123",
413
416
  }),
414
- ).toEqual(true);
417
+ ).toEqual("treatment");
415
418
 
416
419
  done();
417
420
  }, 75);
@@ -421,7 +424,8 @@ describe("sdk: instance", function () {
421
424
  const sdk = createInstance({
422
425
  initialFeatures: {
423
426
  test: {
424
- variation: false,
427
+ enabled: true,
428
+ variation: "control",
425
429
  },
426
430
  },
427
431
  datafileUrl: "http://localhost:3000/datafile.json",
@@ -432,17 +436,16 @@ describe("sdk: instance", function () {
432
436
  features: [
433
437
  {
434
438
  key: "test",
435
- defaultVariation: false,
436
439
  bucketBy: "userId",
437
- variations: [{ value: true }, { value: false }],
440
+ variations: [{ value: "control" }, { value: "treatment" }],
438
441
  traffic: [
439
442
  {
440
443
  key: "1",
441
444
  segments: "*",
442
445
  percentage: 100000,
443
446
  allocation: [
444
- { variation: true, range: [0, 100000] },
445
- { variation: false, range: [0, 0] },
447
+ { variation: "control", range: [0, 0] },
448
+ { variation: "treatment", range: [0, 100000] },
446
449
  ],
447
450
  },
448
451
  ],
@@ -460,20 +463,20 @@ describe("sdk: instance", function () {
460
463
  },
461
464
  });
462
465
 
463
- // initially false
466
+ // initially control
464
467
  expect(
465
468
  sdk.getVariation("test", {
466
469
  userId: "123",
467
470
  }),
468
- ).toEqual(false);
471
+ ).toEqual("control");
469
472
 
470
473
  setTimeout(function () {
471
- // true after fetching datafile
474
+ // treatment after fetching datafile
472
475
  expect(
473
476
  sdk.getVariation("test", {
474
477
  userId: "123",
475
478
  }),
476
- ).toEqual(true);
479
+ ).toEqual("treatment");
477
480
 
478
481
  done();
479
482
  }, 75);