@featurevisor/sdk 1.35.2 → 2.0.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/CHANGELOG.md +8 -0
- package/README.md +2 -381
- package/coverage/clover.xml +707 -643
- package/coverage/coverage-final.json +11 -9
- package/coverage/lcov-report/{segments.ts.html → bucketer.ts.html} +155 -77
- package/coverage/lcov-report/child.ts.html +940 -0
- package/coverage/lcov-report/conditions.ts.html +107 -158
- package/coverage/lcov-report/datafileReader.ts.html +763 -103
- package/coverage/lcov-report/emitter.ts.html +77 -59
- package/coverage/lcov-report/evaluate.ts.html +689 -416
- package/coverage/lcov-report/events.ts.html +334 -0
- package/coverage/lcov-report/helpers.ts.html +184 -0
- package/coverage/lcov-report/{feature.ts.html → hooks.ts.html} +90 -237
- package/coverage/lcov-report/index.html +119 -89
- package/coverage/lcov-report/instance.ts.html +341 -773
- package/coverage/lcov-report/logger.ts.html +64 -64
- package/coverage/lcov.info +1433 -1223
- package/dist/bucketer.d.ts +11 -0
- package/dist/child.d.ts +26 -0
- package/dist/compareVersions.d.ts +4 -0
- package/dist/conditions.d.ts +4 -4
- package/dist/datafileReader.d.ts +26 -6
- package/dist/emitter.d.ts +8 -9
- package/dist/evaluate.d.ts +31 -29
- package/dist/events.d.ts +5 -0
- package/dist/helpers.d.ts +5 -0
- package/dist/hooks.d.ts +45 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.gz +0 -0
- package/dist/index.mjs.map +1 -1
- package/dist/instance.d.ts +40 -72
- package/dist/logger.d.ts +6 -5
- package/dist/murmurhash.d.ts +1 -0
- package/jest.config.js +2 -0
- package/lib/bucketer.d.ts +11 -0
- package/lib/child.d.ts +26 -0
- package/lib/compareVersions.d.ts +4 -0
- package/lib/conditions.d.ts +4 -4
- package/lib/datafileReader.d.ts +26 -6
- package/lib/emitter.d.ts +8 -9
- package/lib/evaluate.d.ts +31 -29
- package/lib/events.d.ts +5 -0
- package/lib/helpers.d.ts +5 -0
- package/lib/hooks.d.ts +45 -0
- package/lib/index.d.ts +3 -2
- package/lib/instance.d.ts +40 -72
- package/lib/logger.d.ts +6 -5
- package/lib/murmurhash.d.ts +1 -0
- package/package.json +3 -5
- package/src/bucketer.spec.ts +165 -0
- package/src/bucketer.ts +84 -0
- package/src/child.spec.ts +267 -0
- package/src/child.ts +285 -0
- package/src/compareVersions.ts +93 -0
- package/src/conditions.spec.ts +563 -353
- package/src/conditions.ts +46 -63
- package/src/datafileReader.spec.ts +396 -84
- package/src/datafileReader.ts +280 -60
- package/src/emitter.spec.ts +27 -86
- package/src/emitter.ts +38 -32
- package/src/evaluate.ts +349 -258
- package/src/events.spec.ts +154 -0
- package/src/events.ts +83 -0
- package/src/helpers.ts +33 -0
- package/src/hooks.ts +88 -0
- package/src/index.ts +3 -2
- package/src/instance.spec.ts +305 -489
- package/src/instance.ts +247 -391
- package/src/logger.spec.ts +212 -134
- package/src/logger.ts +36 -36
- package/src/murmurhash.ts +71 -0
- package/coverage/lcov-report/bucket.ts.html +0 -502
- package/dist/bucket.d.ts +0 -30
- package/dist/feature.d.ts +0 -16
- package/dist/segments.d.ts +0 -5
- package/lib/bucket.d.ts +0 -30
- package/lib/feature.d.ts +0 -16
- package/lib/segments.d.ts +0 -5
- package/src/bucket.spec.ts +0 -37
- package/src/bucket.ts +0 -139
- package/src/feature.ts +0 -137
- package/src/segments.spec.ts +0 -468
- package/src/segments.ts +0 -58
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.35.3](https://github.com/featurevisor/featurevisor/compare/v1.35.2...v1.35.3) (2025-04-12)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @featurevisor/sdk
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
## [1.35.2](https://github.com/featurevisor/featurevisor/compare/v1.35.1...v1.35.2) (2025-03-31)
|
|
7
15
|
|
|
8
16
|
|
package/README.md
CHANGED
|
@@ -1,387 +1,8 @@
|
|
|
1
1
|
# @featurevisor/sdk <!-- omit in toc -->
|
|
2
2
|
|
|
3
|
-
Universal JavaScript SDK for both Node.js and the browser.
|
|
3
|
+
Universal Featurevisor JavaScript SDK for both Node.js and the browser.
|
|
4
4
|
|
|
5
|
-
Visit [https://featurevisor.com/docs/sdks/](https://featurevisor.com/docs/sdks/) for more information.
|
|
6
|
-
|
|
7
|
-
- [Installation](#installation)
|
|
8
|
-
- [Usage](#usage)
|
|
9
|
-
- [Options](#options)
|
|
10
|
-
- [`bucketKeySeparator`](#bucketkeyseparator)
|
|
11
|
-
- [`configureBucketKey`](#configurebucketkey)
|
|
12
|
-
- [`configureBucketValue`](#configurebucketvalue)
|
|
13
|
-
- [`datafile`](#datafile)
|
|
14
|
-
- [`datafileUrl`](#datafileurl)
|
|
15
|
-
- [`handleDatafileFetch`](#handledatafilefetch)
|
|
16
|
-
- [`initialFeatures`](#initialfeatures)
|
|
17
|
-
- [`interceptContext`](#interceptcontext)
|
|
18
|
-
- [`logger`](#logger)
|
|
19
|
-
- [`onActivation`](#onactivation)
|
|
20
|
-
- [`onReady`](#onready)
|
|
21
|
-
- [`onRefresh`](#onrefresh)
|
|
22
|
-
- [`onUpdate`](#onupdate)
|
|
23
|
-
- [`refreshInterval`](#refreshinterval)
|
|
24
|
-
- [`stickyFeatures`](#stickyfeatures)
|
|
25
|
-
- [API](#api)
|
|
26
|
-
- [`isEnabled`](#isenabled)
|
|
27
|
-
- [`getVariation`](#getvariation)
|
|
28
|
-
- [`getVariable`](#getvariable)
|
|
29
|
-
- [`activate`](#activate)
|
|
30
|
-
- [`isReady`](#isready)
|
|
31
|
-
- [`refresh`](#refresh)
|
|
32
|
-
- [`setLogLevels`](#setloglevels)
|
|
33
|
-
- [`on`](#on)
|
|
34
|
-
- [`addListener`](#addlistener)
|
|
35
|
-
- [`off`](#off)
|
|
36
|
-
- [`removeListener`](#removelistener)
|
|
37
|
-
- [`removeAllListeners`](#removealllisteners)
|
|
38
|
-
- [`onReady`](#onready-1)
|
|
39
|
-
|
|
40
|
-
## Installation
|
|
41
|
-
|
|
42
|
-
```
|
|
43
|
-
$ npm install --save @featurevisor/sdk
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## Usage
|
|
47
|
-
|
|
48
|
-
Initialize the SDK:
|
|
49
|
-
|
|
50
|
-
```js
|
|
51
|
-
import { createInstance } from "@featurevisor/sdk";
|
|
52
|
-
|
|
53
|
-
const sdk = createInstance(options);
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Options
|
|
57
|
-
|
|
58
|
-
Options you can pass when creating Featurevisor SDK instance:
|
|
59
|
-
|
|
60
|
-
### `bucketKeySeparator`
|
|
61
|
-
|
|
62
|
-
- Type: `string`
|
|
63
|
-
- Required: no
|
|
64
|
-
- Defaults to: `.`
|
|
65
|
-
|
|
66
|
-
### `configureBucketKey`
|
|
67
|
-
|
|
68
|
-
- Type: `function`
|
|
69
|
-
- Required: no
|
|
70
|
-
|
|
71
|
-
Use it to take over bucketing key generation process.
|
|
72
|
-
|
|
73
|
-
```js
|
|
74
|
-
const sdk = createInstance({
|
|
75
|
-
configureBucketKey: (feature, context, bucketKey) => {
|
|
76
|
-
return bucketKey;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### `configureBucketValue`
|
|
82
|
-
|
|
83
|
-
- Type: `function`
|
|
84
|
-
- Required: no
|
|
85
|
-
|
|
86
|
-
Use it to take over bucketing process.
|
|
87
|
-
|
|
88
|
-
```js
|
|
89
|
-
const sdk = createInstance({
|
|
90
|
-
configureBucketValue: (feature, context, bucketValue) => {
|
|
91
|
-
return bucketValue; // 0 to 100,000
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
### `datafile`
|
|
97
|
-
|
|
98
|
-
- Type: `object`
|
|
99
|
-
- Required: either `datafile` or `datafileUrl` is required
|
|
100
|
-
|
|
101
|
-
Use it to pass the datafile object directly.
|
|
102
|
-
|
|
103
|
-
### `datafileUrl`
|
|
104
|
-
|
|
105
|
-
- Type: `string`
|
|
106
|
-
- Required: either `datafile` or `datafileUrl` is required
|
|
107
|
-
|
|
108
|
-
Use it to pass the URL to fetch the datafile from.
|
|
109
|
-
|
|
110
|
-
### `handleDatafileFetch`
|
|
111
|
-
|
|
112
|
-
- Type: `function`
|
|
113
|
-
- Required: no
|
|
114
|
-
|
|
115
|
-
Pass this function if you need to take over the datafile fetching and parsing process:
|
|
116
|
-
|
|
117
|
-
```js
|
|
118
|
-
const sdk = createInstance({
|
|
119
|
-
handleDatafileFetch: async (datafileUrl) => {
|
|
120
|
-
const response = await fetch(datafileUrl);
|
|
121
|
-
const datafile = await response.json();
|
|
122
|
-
|
|
123
|
-
return datafile;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
### `initialFeatures`
|
|
129
|
-
|
|
130
|
-
- Type: `object`
|
|
131
|
-
- Required: no
|
|
132
|
-
|
|
133
|
-
Pass set of initial features with their variation and (optional) variables that you want the SDK to return until the datafile is fetched and parsed:
|
|
134
|
-
|
|
135
|
-
```js
|
|
136
|
-
const sdk = createInstance({
|
|
137
|
-
initialFeatures: {
|
|
138
|
-
myFeatureKey: {
|
|
139
|
-
enabled: true,
|
|
140
|
-
|
|
141
|
-
// optional
|
|
142
|
-
variation: "treatment",
|
|
143
|
-
variables: {
|
|
144
|
-
myVariableKey: "my-variable-value"
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### `interceptContext`
|
|
152
|
-
|
|
153
|
-
- Type: `function`
|
|
154
|
-
- Required: no
|
|
155
|
-
|
|
156
|
-
Intercept given context before they are used to bucket the user:
|
|
157
|
-
|
|
158
|
-
```js
|
|
159
|
-
const defaultContext = {
|
|
160
|
-
platform: "web",
|
|
161
|
-
locale: "en-US",
|
|
162
|
-
country: "US",
|
|
163
|
-
timezone: "America/New_York",
|
|
164
|
-
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko)",
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
const sdk = createInstance({
|
|
168
|
-
interceptContext: (context) => {
|
|
169
|
-
return {
|
|
170
|
-
...defaultContext,
|
|
171
|
-
...context,
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### `logger`
|
|
178
|
-
|
|
179
|
-
- Type: `object`
|
|
180
|
-
- Required: no
|
|
181
|
-
|
|
182
|
-
Pass a custom logger with custom levels, otherwise it the SDK will print logs to the console for `error` and `warn` levels.
|
|
183
|
-
|
|
184
|
-
```js
|
|
185
|
-
import { createInstance, createLogger } from "@featurevisor/sdk";
|
|
186
|
-
|
|
187
|
-
const sdk = createInstance({
|
|
188
|
-
logger: createLogger({
|
|
189
|
-
levels: ["debug", "info", "warn", "error"],
|
|
190
|
-
handler: (level, message, details) => {
|
|
191
|
-
// do something with the log
|
|
192
|
-
},
|
|
193
|
-
}),
|
|
194
|
-
});
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
### `onActivation`
|
|
198
|
-
|
|
199
|
-
- Type: `function`
|
|
200
|
-
- Required: no
|
|
201
|
-
|
|
202
|
-
Capture activated features along with their evaluated variation:
|
|
203
|
-
|
|
204
|
-
```js
|
|
205
|
-
const sdk = createInstance({
|
|
206
|
-
onActivation: (featureKey, variation, context, captureContext) => {
|
|
207
|
-
// do something with the activated feature
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
`captureContext` will only contain attributes that are marked as `capture: true` in the Attributes' YAML files.
|
|
213
|
-
|
|
214
|
-
### `onReady`
|
|
215
|
-
|
|
216
|
-
- Type: `function`
|
|
217
|
-
- Required: no
|
|
218
|
-
|
|
219
|
-
Triggered maximum once when the SDK is ready to be used.
|
|
220
|
-
|
|
221
|
-
```js
|
|
222
|
-
const sdk = createInstance({
|
|
223
|
-
onReady: () => {
|
|
224
|
-
// do something when the SDK is ready
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
### `onRefresh`
|
|
230
|
-
|
|
231
|
-
- Type: `function`
|
|
232
|
-
- Required: no
|
|
233
|
-
|
|
234
|
-
Triggered every time the datafile is refreshed.
|
|
235
|
-
|
|
236
|
-
Works only when `datafileUrl` and `refreshInterval` are set.
|
|
237
|
-
|
|
238
|
-
```js
|
|
239
|
-
const sdk = createInstance({
|
|
240
|
-
onRefresh: () => {
|
|
241
|
-
// do something when the datafile is refreshed
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
### `onUpdate`
|
|
247
|
-
|
|
248
|
-
- Type: `function`
|
|
249
|
-
- Required: no
|
|
250
|
-
|
|
251
|
-
Triggered every time the datafile is refreshed, and the newly fetched datafile is detected to have different content than last fetched one.
|
|
252
|
-
|
|
253
|
-
Works only when `datafileUrl` and `refreshInterval` are set.
|
|
254
|
-
|
|
255
|
-
```js
|
|
256
|
-
const sdk = createInstance({
|
|
257
|
-
onUpdate: () => {
|
|
258
|
-
// do something when the datafile is updated
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
### `refreshInterval`
|
|
264
|
-
|
|
265
|
-
- Type: `number` (in seconds)
|
|
266
|
-
- Required: no
|
|
267
|
-
|
|
268
|
-
Set the interval to refresh the datafile.
|
|
269
|
-
|
|
270
|
-
```js
|
|
271
|
-
const sdk = createInstance({
|
|
272
|
-
refreshInterval: 60 * 5, // every 5 minutes
|
|
273
|
-
});
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
### `stickyFeatures`
|
|
277
|
-
|
|
278
|
-
- Type: `object`
|
|
279
|
-
- Required: no
|
|
280
|
-
|
|
281
|
-
If set, the SDK will skip evaluating the datafile and return variation and variable results from this object instead.
|
|
282
|
-
|
|
283
|
-
If a feature key is not present in this object, the SDK will continue to evaluate the datafile.
|
|
284
|
-
|
|
285
|
-
```js
|
|
286
|
-
const sdk = createInstance({
|
|
287
|
-
stickyFeatures: {
|
|
288
|
-
myFeatureKey: {
|
|
289
|
-
enabled: true,
|
|
290
|
-
|
|
291
|
-
// optional
|
|
292
|
-
variation: "treatment",
|
|
293
|
-
variables: {
|
|
294
|
-
myVariableKey: "my-variable-value"
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
});
|
|
299
|
-
```
|
|
300
|
-
|
|
301
|
-
## API
|
|
302
|
-
|
|
303
|
-
These methods are available once the SDK instance is created:
|
|
304
|
-
|
|
305
|
-
### `isEnabled`
|
|
306
|
-
|
|
307
|
-
> `isEnabled(featureKey: string, context: Context): boolean`
|
|
308
|
-
|
|
309
|
-
### `getVariation`
|
|
310
|
-
|
|
311
|
-
> `getVariation(featureKey: string, context: Context): VariationValue`
|
|
312
|
-
|
|
313
|
-
### `getVariable`
|
|
314
|
-
|
|
315
|
-
> `getVariable(featureKey: string, variableKey: string, context: Context): VariableValue`
|
|
316
|
-
|
|
317
|
-
Also supports additional type specific methods:
|
|
318
|
-
|
|
319
|
-
- `getVariableBoolean`
|
|
320
|
-
- `getVariableString`
|
|
321
|
-
- `getVariableInteger`
|
|
322
|
-
- `getVariableDouble`
|
|
323
|
-
- `getVariableArray`
|
|
324
|
-
- `getVariableObject`
|
|
325
|
-
- `getVariableJSON`
|
|
326
|
-
|
|
327
|
-
### `activate`
|
|
328
|
-
|
|
329
|
-
> `activate(featureKey: string, context: Context): VariationValue`
|
|
330
|
-
|
|
331
|
-
Same as `getVariation`, but also calls the `onActivation` callback.
|
|
332
|
-
|
|
333
|
-
This is a convenience method meant to be called when you know the User has been exposed to your Feature, and you also want to track the activation.
|
|
334
|
-
|
|
335
|
-
### `isReady`
|
|
336
|
-
|
|
337
|
-
> `isReady(): boolean`
|
|
338
|
-
|
|
339
|
-
Synchonously check if the SDK is ready to be used.
|
|
340
|
-
|
|
341
|
-
### `refresh`
|
|
342
|
-
|
|
343
|
-
> `refresh(): void`
|
|
344
|
-
|
|
345
|
-
Manually refresh datafile.
|
|
346
|
-
|
|
347
|
-
### `setLogLevels`
|
|
348
|
-
|
|
349
|
-
> `setLogLevels(levels: LogLevel[]): void`
|
|
350
|
-
|
|
351
|
-
Accepted values for `levels`: `["debug", "info", "warn", "error"]`.
|
|
352
|
-
|
|
353
|
-
### `on`
|
|
354
|
-
|
|
355
|
-
> `on(event: string, callback: function): void`
|
|
356
|
-
|
|
357
|
-
Listen to SDK events, like:
|
|
358
|
-
|
|
359
|
-
- `ready`
|
|
360
|
-
- `activation`
|
|
361
|
-
- `refresh`
|
|
362
|
-
- `update`
|
|
363
|
-
|
|
364
|
-
### `addListener`
|
|
365
|
-
|
|
366
|
-
Alias for `on` method.
|
|
367
|
-
|
|
368
|
-
### `off`
|
|
369
|
-
|
|
370
|
-
> `off(event: string, callback: function): void`
|
|
371
|
-
|
|
372
|
-
### `removeListener`
|
|
373
|
-
|
|
374
|
-
Alias for `off` method.
|
|
375
|
-
|
|
376
|
-
### `removeAllListeners`
|
|
377
|
-
|
|
378
|
-
> `removeAllListeners(event?: string): void`
|
|
379
|
-
|
|
380
|
-
### `onReady`
|
|
381
|
-
|
|
382
|
-
Resolves the SDK instance with a promise for convenience when it's ready:
|
|
383
|
-
|
|
384
|
-
> onReady(): Promise<FeaturevisorInstance>
|
|
5
|
+
Visit [https://featurevisor.com/docs/sdks/javascript/](https://featurevisor.com/docs/sdks/javascript/) for more information.
|
|
385
6
|
|
|
386
7
|
## License <!-- omit in toc -->
|
|
387
8
|
|