flagsmith-nodejs 6.2.0 → 7.0.2
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/.github/workflows/publish.yml +8 -5
- package/.github/workflows/pull_request.yaml +3 -0
- package/.gitmodules +1 -1
- package/.husky/pre-commit +1 -0
- package/.nvmrc +1 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +40 -0
- package/build/cjs/flagsmith-engine/environments/models.d.ts +2 -1
- package/build/cjs/flagsmith-engine/environments/models.js +3 -1
- package/build/cjs/flagsmith-engine/environments/util.js +1 -1
- package/build/cjs/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.d.ts +230 -0
- package/build/cjs/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.js +8 -0
- package/build/cjs/flagsmith-engine/evaluation/evaluationContext/mappers.d.ts +5 -0
- package/build/cjs/flagsmith-engine/evaluation/evaluationContext/mappers.js +156 -0
- package/build/cjs/flagsmith-engine/evaluation/evaluationContext/types.d.ts +216 -0
- package/build/cjs/flagsmith-engine/evaluation/evaluationContext/types.js +8 -0
- package/build/cjs/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.d.ts +68 -0
- package/build/cjs/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.js +8 -0
- package/build/cjs/flagsmith-engine/evaluation/models.d.ts +50 -0
- package/build/cjs/flagsmith-engine/evaluation/models.js +26 -0
- package/build/cjs/flagsmith-engine/features/models.js +1 -1
- package/build/cjs/flagsmith-engine/features/types.d.ts +5 -0
- package/build/cjs/flagsmith-engine/features/types.js +9 -0
- package/build/cjs/flagsmith-engine/features/util.d.ts +1 -0
- package/build/cjs/flagsmith-engine/features/util.js +5 -1
- package/build/cjs/flagsmith-engine/index.d.ts +61 -9
- package/build/cjs/flagsmith-engine/index.js +176 -56
- package/build/cjs/flagsmith-engine/segments/constants.d.ts +1 -0
- package/build/cjs/flagsmith-engine/segments/constants.js +2 -1
- package/build/cjs/flagsmith-engine/segments/evaluators.d.ts +41 -7
- package/build/cjs/flagsmith-engine/segments/evaluators.js +136 -24
- package/build/cjs/flagsmith-engine/segments/models.d.ts +9 -4
- package/build/cjs/flagsmith-engine/segments/models.js +115 -13
- package/build/cjs/flagsmith-engine/utils/hashing/index.d.ts +1 -1
- package/build/cjs/flagsmith-engine/utils/hashing/index.js +4 -4
- package/build/cjs/sdk/index.d.ts +1 -3
- package/build/cjs/sdk/index.js +22 -19
- package/build/cjs/sdk/models.d.ts +8 -1
- package/build/cjs/sdk/models.js +29 -1
- package/build/esm/flagsmith-engine/environments/models.d.ts +2 -1
- package/build/esm/flagsmith-engine/environments/models.js +3 -1
- package/build/esm/flagsmith-engine/environments/util.js +1 -1
- package/build/esm/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.d.ts +230 -0
- package/build/esm/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.js +7 -0
- package/build/esm/flagsmith-engine/evaluation/evaluationContext/mappers.d.ts +5 -0
- package/build/esm/flagsmith-engine/evaluation/evaluationContext/mappers.js +152 -0
- package/build/esm/flagsmith-engine/evaluation/evaluationContext/types.d.ts +216 -0
- package/build/esm/flagsmith-engine/evaluation/evaluationContext/types.js +7 -0
- package/build/esm/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.d.ts +68 -0
- package/build/esm/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.js +7 -0
- package/build/esm/flagsmith-engine/evaluation/models.d.ts +50 -0
- package/build/esm/flagsmith-engine/evaluation/models.js +9 -0
- package/build/esm/flagsmith-engine/features/models.js +2 -2
- package/build/esm/flagsmith-engine/features/types.d.ts +5 -0
- package/build/esm/flagsmith-engine/features/types.js +6 -0
- package/build/esm/flagsmith-engine/features/util.d.ts +1 -0
- package/build/esm/flagsmith-engine/features/util.js +3 -0
- package/build/esm/flagsmith-engine/index.d.ts +61 -9
- package/build/esm/flagsmith-engine/index.js +161 -43
- package/build/esm/flagsmith-engine/segments/constants.d.ts +1 -0
- package/build/esm/flagsmith-engine/segments/constants.js +1 -0
- package/build/esm/flagsmith-engine/segments/evaluators.d.ts +41 -7
- package/build/esm/flagsmith-engine/segments/evaluators.js +137 -25
- package/build/esm/flagsmith-engine/segments/models.d.ts +9 -4
- package/build/esm/flagsmith-engine/segments/models.js +115 -13
- package/build/esm/flagsmith-engine/utils/hashing/index.d.ts +1 -1
- package/build/esm/flagsmith-engine/utils/hashing/index.js +2 -2
- package/build/esm/sdk/index.d.ts +1 -3
- package/build/esm/sdk/index.js +21 -18
- package/build/esm/sdk/models.d.ts +8 -1
- package/build/esm/sdk/models.js +29 -1
- package/flagsmith-engine/environments/models.ts +3 -1
- package/flagsmith-engine/environments/util.ts +2 -1
- package/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.ts +247 -0
- package/flagsmith-engine/evaluation/evaluationContext/mappers.ts +204 -0
- package/flagsmith-engine/evaluation/evaluationContext/types.ts +233 -0
- package/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.ts +71 -0
- package/flagsmith-engine/evaluation/models.ts +96 -0
- package/flagsmith-engine/features/models.ts +3 -2
- package/flagsmith-engine/features/types.ts +5 -0
- package/flagsmith-engine/features/util.ts +4 -0
- package/flagsmith-engine/index.ts +229 -72
- package/flagsmith-engine/segments/constants.ts +1 -0
- package/flagsmith-engine/segments/evaluators.ts +178 -62
- package/flagsmith-engine/segments/models.ts +171 -23
- package/flagsmith-engine/utils/hashing/index.ts +2 -2
- package/package.json +13 -2
- package/sdk/index.ts +36 -23
- package/sdk/models.ts +44 -2
- package/tests/engine/e2e/engine.test.ts +43 -38
- package/tests/engine/unit/engine.test.ts +306 -59
- package/tests/engine/unit/mappers.test.ts +353 -0
- package/tests/engine/unit/segments/segment_evaluators.test.ts +391 -49
- package/tests/engine/unit/segments/segments_model.test.ts +85 -0
- package/tests/engine/unit/utils/utils.test.ts +7 -7
- package/tests/engine/unit/utils.ts +1 -1
- package/tests/sdk/data/environment.json +1 -0
- package/tests/sdk/flagsmith.test.ts +29 -3
- package/tests/sdk/offline-handlers.test.ts +3 -1
- package/vitest.config.esm.ts +34 -0
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SegmentModel = exports.SegmentRuleModel = exports.SegmentConditionModel = exports.getMatchingFunctions = exports.semverMatchingFunction = exports.matchingFunctions = exports.any = exports.all = void 0;
|
|
4
4
|
const semver = require("semver");
|
|
5
|
+
const models_js_1 = require("../features/models.js");
|
|
5
6
|
const index_js_1 = require("../utils/index.js");
|
|
6
7
|
const constants_js_1 = require("./constants.js");
|
|
7
8
|
const util_js_1 = require("./util.js");
|
|
9
|
+
const constants_js_2 = require("../features/constants.js");
|
|
10
|
+
const models_js_2 = require("../evaluation/models.js");
|
|
8
11
|
const all = (iterable) => iterable.filter(e => !!e).length === iterable.length;
|
|
9
12
|
exports.all = all;
|
|
10
13
|
const any = (iterable) => iterable.filter(e => !!e).length > 0;
|
|
@@ -16,15 +19,33 @@ exports.matchingFunctions = {
|
|
|
16
19
|
[constants_js_1.CONDITION_OPERATORS.LESS_THAN]: (thisValue, otherValue) => thisValue > otherValue,
|
|
17
20
|
[constants_js_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue, otherValue) => thisValue >= otherValue,
|
|
18
21
|
[constants_js_1.CONDITION_OPERATORS.NOT_EQUAL]: (thisValue, otherValue) => thisValue != otherValue,
|
|
19
|
-
[constants_js_1.CONDITION_OPERATORS.CONTAINS]: (thisValue, otherValue) =>
|
|
22
|
+
[constants_js_1.CONDITION_OPERATORS.CONTAINS]: (thisValue, otherValue) => {
|
|
23
|
+
try {
|
|
24
|
+
return !!otherValue && otherValue.includes(thisValue);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
// Semver library throws an error if the version is invalid, in this case, we want to catch and return false
|
|
32
|
+
const safeSemverCompare = (semverMatchingFunction) => {
|
|
33
|
+
return (conditionValue, traitValue) => {
|
|
34
|
+
try {
|
|
35
|
+
return semverMatchingFunction(conditionValue, traitValue);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
20
41
|
};
|
|
21
42
|
exports.semverMatchingFunction = {
|
|
22
43
|
...exports.matchingFunctions,
|
|
23
|
-
[constants_js_1.CONDITION_OPERATORS.EQUAL]: (
|
|
24
|
-
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN]: (
|
|
25
|
-
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (
|
|
26
|
-
[constants_js_1.CONDITION_OPERATORS.LESS_THAN]: (
|
|
27
|
-
[constants_js_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (
|
|
44
|
+
[constants_js_1.CONDITION_OPERATORS.EQUAL]: safeSemverCompare((conditionValue, traitValue) => semver.eq(traitValue, conditionValue)),
|
|
45
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN]: safeSemverCompare((conditionValue, traitValue) => semver.gt(traitValue, conditionValue)),
|
|
46
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: safeSemverCompare((conditionValue, traitValue) => semver.gte(traitValue, conditionValue)),
|
|
47
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN]: safeSemverCompare((conditionValue, traitValue) => semver.lt(traitValue, conditionValue)),
|
|
48
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: safeSemverCompare((conditionValue, traitValue) => semver.lte(traitValue, conditionValue))
|
|
28
49
|
};
|
|
29
50
|
const getMatchingFunctions = (semver) => semver ? exports.semverMatchingFunction : exports.matchingFunctions;
|
|
30
51
|
exports.getMatchingFunctions = getMatchingFunctions;
|
|
@@ -37,11 +58,11 @@ class SegmentConditionModel {
|
|
|
37
58
|
};
|
|
38
59
|
operator;
|
|
39
60
|
value;
|
|
40
|
-
|
|
61
|
+
property;
|
|
41
62
|
constructor(operator, value, property) {
|
|
42
63
|
this.operator = operator;
|
|
43
64
|
this.value = value;
|
|
44
|
-
this.
|
|
65
|
+
this.property = property;
|
|
45
66
|
}
|
|
46
67
|
matchesTraitValue(traitValue) {
|
|
47
68
|
const evaluators = {
|
|
@@ -51,17 +72,49 @@ class SegmentConditionModel {
|
|
|
51
72
|
!traitValue.includes(this.value?.toString()));
|
|
52
73
|
},
|
|
53
74
|
evaluateRegex: (traitValue) => {
|
|
54
|
-
|
|
75
|
+
try {
|
|
76
|
+
if (!this.value) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const regex = new RegExp(this.value?.toString());
|
|
80
|
+
return !!traitValue?.toString().match(regex);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
55
85
|
},
|
|
56
86
|
evaluateModulo: (traitValue) => {
|
|
57
|
-
|
|
87
|
+
const parsedTraitValue = parseFloat(traitValue);
|
|
88
|
+
if (isNaN(parsedTraitValue) || !this.value) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
const parts = this.value.toString().split('|');
|
|
92
|
+
if (parts.length !== 2) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
const divisor = parseFloat(parts[0]);
|
|
96
|
+
const remainder = parseFloat(parts[1]);
|
|
97
|
+
if (isNaN(divisor) || isNaN(remainder) || divisor === 0) {
|
|
58
98
|
return false;
|
|
59
99
|
}
|
|
60
|
-
|
|
61
|
-
const [divisor, reminder] = [parseFloat(parts[0]), parseFloat(parts[1])];
|
|
62
|
-
return traitValue % divisor === reminder;
|
|
100
|
+
return parsedTraitValue % divisor === remainder;
|
|
63
101
|
},
|
|
64
102
|
evaluateIn: (traitValue) => {
|
|
103
|
+
if (!traitValue || typeof traitValue === 'boolean') {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
if (Array.isArray(this.value)) {
|
|
107
|
+
return this.value.includes(traitValue.toString());
|
|
108
|
+
}
|
|
109
|
+
if (typeof this.value === 'string') {
|
|
110
|
+
try {
|
|
111
|
+
const parsed = JSON.parse(this.value);
|
|
112
|
+
if (Array.isArray(parsed)) {
|
|
113
|
+
return parsed.includes(traitValue.toString());
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch { }
|
|
117
|
+
}
|
|
65
118
|
return this.value?.split(',').includes(traitValue.toString());
|
|
66
119
|
}
|
|
67
120
|
};
|
|
@@ -107,5 +160,54 @@ class SegmentModel {
|
|
|
107
160
|
this.id = id;
|
|
108
161
|
this.name = name;
|
|
109
162
|
}
|
|
163
|
+
static fromSegmentResult(segmentResults, evaluationContext) {
|
|
164
|
+
const segmentModels = [];
|
|
165
|
+
if (!evaluationContext.segments) {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
for (const segmentResult of segmentResults) {
|
|
169
|
+
if (segmentResult.metadata?.source === models_js_2.SegmentSource.IDENTITY_OVERRIDE) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const segmentMetadataId = segmentResult.metadata?.id;
|
|
173
|
+
if (!segmentMetadataId) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const segmentContext = evaluationContext.segments[segmentMetadataId.toString()];
|
|
177
|
+
if (segmentContext) {
|
|
178
|
+
const segment = new SegmentModel(segmentMetadataId, segmentContext.name);
|
|
179
|
+
segment.rules = segmentContext.rules.map(rule => new SegmentRuleModel(rule.type));
|
|
180
|
+
segment.featureStates = SegmentModel.createFeatureStatesFromOverrides(segmentContext.overrides || []);
|
|
181
|
+
segmentModels.push(segment);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return segmentModels;
|
|
185
|
+
}
|
|
186
|
+
static createFeatureStatesFromOverrides(overrides) {
|
|
187
|
+
if (!overrides)
|
|
188
|
+
return [];
|
|
189
|
+
return overrides
|
|
190
|
+
.filter(override => {
|
|
191
|
+
const overrideMetadataId = override?.metadata?.id;
|
|
192
|
+
return typeof overrideMetadataId === 'number';
|
|
193
|
+
})
|
|
194
|
+
.map(override => {
|
|
195
|
+
const overrideMetadataId = override.metadata.id;
|
|
196
|
+
const feature = new models_js_1.FeatureModel(overrideMetadataId, override.name, override.variants?.length && override.variants.length > 0
|
|
197
|
+
? constants_js_2.CONSTANTS.MULTIVARIATE
|
|
198
|
+
: constants_js_2.CONSTANTS.STANDARD);
|
|
199
|
+
const featureState = new models_js_1.FeatureStateModel(feature, override.enabled, override.priority || 0);
|
|
200
|
+
if (override.value !== undefined) {
|
|
201
|
+
featureState.setValue(override.value);
|
|
202
|
+
}
|
|
203
|
+
if (override.variants && override.variants.length > 0) {
|
|
204
|
+
featureState.multivariateFeatureStateValues = this.createMultivariateValues(override.variants);
|
|
205
|
+
}
|
|
206
|
+
return featureState;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
static createMultivariateValues(variants) {
|
|
210
|
+
return variants.map(variant => new models_js_1.MultivariateFeatureStateValueModel(new models_js_1.MultivariateFeatureOptionModel(variant.value, variant.id), variant.weight, variant.id));
|
|
211
|
+
}
|
|
110
212
|
}
|
|
111
213
|
exports.SegmentModel = SegmentModel;
|
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
* @param {} iterations=1 num times to include each id in the generated string to hash
|
|
7
7
|
* @returns number number between 0 (inclusive) and 100 (exclusive)
|
|
8
8
|
*/
|
|
9
|
-
export declare function
|
|
9
|
+
export declare function getHashedPercentageForObjIds(objectIds: Array<any>, iterations?: number): number;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getHashedPercentageForObjIds = void 0;
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
const md5 = (data) => (0, node_crypto_1.createHash)('md5').update(data).digest('hex');
|
|
6
6
|
const makeRepeated = (arr, repeats) => Array.from({ length: repeats }, () => arr).flat();
|
|
@@ -13,7 +13,7 @@ const makeRepeated = (arr, repeats) => Array.from({ length: repeats }, () => arr
|
|
|
13
13
|
* @param {} iterations=1 num times to include each id in the generated string to hash
|
|
14
14
|
* @returns number number between 0 (inclusive) and 100 (exclusive)
|
|
15
15
|
*/
|
|
16
|
-
function
|
|
16
|
+
function getHashedPercentageForObjIds(objectIds, iterations = 1) {
|
|
17
17
|
let toHash = makeRepeated(objectIds, iterations).join(',');
|
|
18
18
|
const hashedValue = md5(toHash);
|
|
19
19
|
const hashedInt = BigInt('0x' + hashedValue);
|
|
@@ -22,8 +22,8 @@ function getHashedPercentateForObjIds(objectIds, iterations = 1) {
|
|
|
22
22
|
/* istanbul ignore next */
|
|
23
23
|
if (value === 100) {
|
|
24
24
|
/* istanbul ignore next */
|
|
25
|
-
return
|
|
25
|
+
return getHashedPercentageForObjIds(objectIds, iterations + 1);
|
|
26
26
|
}
|
|
27
27
|
return value;
|
|
28
28
|
}
|
|
29
|
-
exports.
|
|
29
|
+
exports.getHashedPercentageForObjIds = getHashedPercentageForObjIds;
|
package/build/cjs/sdk/index.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { Dispatcher } from 'undici-types';
|
|
2
|
-
import { EnvironmentModel } from '../flagsmith-engine/index.js';
|
|
3
|
-
import { IdentityModel } from '../flagsmith-engine/index.js';
|
|
4
2
|
import { BaseOfflineHandler } from './offline_handlers.js';
|
|
5
3
|
import { DefaultFlag, Flags } from './models.js';
|
|
6
4
|
import { EnvironmentDataPollingManager } from './polling_manager.js';
|
|
7
|
-
import { SegmentModel } from '../flagsmith-engine/index.js';
|
|
5
|
+
import { SegmentModel, EnvironmentModel, IdentityModel } from '../flagsmith-engine/index.js';
|
|
8
6
|
import { FlagsmithConfig, FlagsmithTraitValue, TraitConfig } from './types.js';
|
|
9
7
|
export { AnalyticsProcessor, AnalyticsProcessorOptions } from './analytics.js';
|
|
10
8
|
export { FlagsmithAPIError, FlagsmithClientError } from './errors.js';
|
package/build/cjs/sdk/index.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Flagsmith = exports.EnvironmentDataPollingManager = exports.Flags = exports.DefaultFlag = exports.BaseFlag = exports.FlagsmithClientError = exports.FlagsmithAPIError = exports.AnalyticsProcessor = void 0;
|
|
4
|
-
const index_js_1 = require("../flagsmith-engine/index.js");
|
|
5
4
|
const util_js_1 = require("../flagsmith-engine/environments/util.js");
|
|
6
|
-
const index_js_2 = require("../flagsmith-engine/index.js");
|
|
7
|
-
const index_js_3 = require("../flagsmith-engine/index.js");
|
|
8
5
|
const analytics_js_1 = require("./analytics.js");
|
|
9
6
|
const errors_js_1 = require("./errors.js");
|
|
10
7
|
const models_js_1 = require("./models.js");
|
|
11
8
|
const polling_manager_js_1 = require("./polling_manager.js");
|
|
12
9
|
const utils_js_1 = require("./utils.js");
|
|
13
|
-
const
|
|
10
|
+
const index_js_1 = require("../flagsmith-engine/index.js");
|
|
14
11
|
const pino_1 = require("pino");
|
|
12
|
+
const mappers_js_1 = require("../flagsmith-engine/evaluation/evaluationContext/mappers.js");
|
|
15
13
|
var analytics_js_2 = require("./analytics.js");
|
|
16
14
|
Object.defineProperty(exports, "AnalyticsProcessor", { enumerable: true, get: function () { return analytics_js_2.AnalyticsProcessor; } });
|
|
17
15
|
var errors_js_2 = require("./errors.js");
|
|
@@ -230,7 +228,12 @@ class Flagsmith {
|
|
|
230
228
|
key,
|
|
231
229
|
value: traits?.[key]
|
|
232
230
|
})));
|
|
233
|
-
|
|
231
|
+
const context = (0, mappers_js_1.getEvaluationContext)(environment, identityModel);
|
|
232
|
+
if (!context) {
|
|
233
|
+
throw new errors_js_1.FlagsmithClientError('Local evaluation required to obtain identity segments');
|
|
234
|
+
}
|
|
235
|
+
const evaluationResult = (0, index_js_1.getEvaluationResult)(context);
|
|
236
|
+
return index_js_1.SegmentModel.fromSegmentResult(evaluationResult.segments, context);
|
|
234
237
|
}
|
|
235
238
|
async fetchEnvironment() {
|
|
236
239
|
const deferred = new utils_js_1.Deferred();
|
|
@@ -361,11 +364,12 @@ class Flagsmith {
|
|
|
361
364
|
}
|
|
362
365
|
async getEnvironmentFlagsFromDocument() {
|
|
363
366
|
const environment = await this.getEnvironment();
|
|
364
|
-
const
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
367
|
+
const context = (0, mappers_js_1.getEvaluationContext)(environment, undefined, undefined, true);
|
|
368
|
+
if (!context) {
|
|
369
|
+
throw new errors_js_1.FlagsmithClientError('Unable to get flags. No environment present.');
|
|
370
|
+
}
|
|
371
|
+
const evaluationResult = (0, index_js_1.getEvaluationResult)(context);
|
|
372
|
+
const flags = models_js_1.Flags.fromEvaluationResult(evaluationResult);
|
|
369
373
|
if (!!this.cache) {
|
|
370
374
|
await this.cache.set('flags', flags);
|
|
371
375
|
}
|
|
@@ -377,13 +381,12 @@ class Flagsmith {
|
|
|
377
381
|
key,
|
|
378
382
|
value: traits[key]
|
|
379
383
|
})));
|
|
380
|
-
const
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
});
|
|
384
|
+
const context = (0, mappers_js_1.getEvaluationContext)(environment, identityModel);
|
|
385
|
+
if (!context) {
|
|
386
|
+
throw new errors_js_1.FlagsmithClientError('Unable to get flags. No environment present.');
|
|
387
|
+
}
|
|
388
|
+
const evaluationResult = (0, index_js_1.getEvaluationResult)(context);
|
|
389
|
+
const flags = models_js_1.Flags.fromEvaluationResult(evaluationResult, this.defaultFlagHandler, this.analyticsProcessor);
|
|
387
390
|
if (!!this.cache) {
|
|
388
391
|
await this.cache.set(`flags-${identifier}`, flags);
|
|
389
392
|
}
|
|
@@ -421,13 +424,13 @@ class Flagsmith {
|
|
|
421
424
|
return flags;
|
|
422
425
|
}
|
|
423
426
|
getIdentityModel(environment, identifier, traits) {
|
|
424
|
-
const traitModels = traits.map(trait => new
|
|
427
|
+
const traitModels = traits.map(trait => new index_js_1.TraitModel(trait.key, trait.value));
|
|
425
428
|
let identityWithOverrides = this.identitiesWithOverridesByIdentifier?.get(identifier);
|
|
426
429
|
if (identityWithOverrides) {
|
|
427
430
|
identityWithOverrides.updateTraits(traitModels);
|
|
428
431
|
return identityWithOverrides;
|
|
429
432
|
}
|
|
430
|
-
return new
|
|
433
|
+
return new index_js_1.IdentityModel('0', traitModels, [], environment.apiKey, identifier);
|
|
431
434
|
}
|
|
432
435
|
}
|
|
433
436
|
exports.Flagsmith = Flagsmith;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { EvaluationResultWithMetadata } from '../flagsmith-engine/evaluation/models.js';
|
|
1
2
|
import { FeatureStateModel } from '../flagsmith-engine/features/models.js';
|
|
2
3
|
import { AnalyticsProcessor } from './analytics.js';
|
|
3
|
-
type FlagValue = string | number | boolean | undefined;
|
|
4
|
+
type FlagValue = string | number | boolean | undefined | null;
|
|
4
5
|
/**
|
|
5
6
|
* A Flagsmith feature. It has an enabled/disabled state, and an optional {@link FlagValue}.
|
|
6
7
|
*/
|
|
@@ -38,12 +39,17 @@ export declare class Flag extends BaseFlag {
|
|
|
38
39
|
* The programmatic name for this feature, unique per Flagsmith project.
|
|
39
40
|
*/
|
|
40
41
|
featureName: string;
|
|
42
|
+
/**
|
|
43
|
+
* The reason for this feature, unique per Flagsmith project.
|
|
44
|
+
*/
|
|
45
|
+
reason?: string;
|
|
41
46
|
constructor(params: {
|
|
42
47
|
value: FlagValue;
|
|
43
48
|
enabled: boolean;
|
|
44
49
|
isDefault?: boolean;
|
|
45
50
|
featureId: number;
|
|
46
51
|
featureName: string;
|
|
52
|
+
reason?: string;
|
|
47
53
|
});
|
|
48
54
|
static fromFeatureStateModel(fsm: FeatureStateModel, identityId: number | string | undefined): Flag;
|
|
49
55
|
static fromAPIFlag(flagData: any): Flag;
|
|
@@ -61,6 +67,7 @@ export declare class Flags {
|
|
|
61
67
|
defaultFlagHandler?: (v: string) => DefaultFlag;
|
|
62
68
|
analyticsProcessor?: AnalyticsProcessor;
|
|
63
69
|
});
|
|
70
|
+
static fromEvaluationResult(evaluationResult: EvaluationResultWithMetadata, defaultFlagHandler?: (v: string) => DefaultFlag, analyticsProcessor?: AnalyticsProcessor): Flags;
|
|
64
71
|
static fromFeatureStateModels(data: {
|
|
65
72
|
featureStates: FeatureStateModel[];
|
|
66
73
|
analyticsProcessor?: AnalyticsProcessor;
|
package/build/cjs/sdk/models.js
CHANGED
|
@@ -46,10 +46,15 @@ class Flag extends BaseFlag {
|
|
|
46
46
|
* The programmatic name for this feature, unique per Flagsmith project.
|
|
47
47
|
*/
|
|
48
48
|
featureName;
|
|
49
|
+
/**
|
|
50
|
+
* The reason for this feature, unique per Flagsmith project.
|
|
51
|
+
*/
|
|
52
|
+
reason;
|
|
49
53
|
constructor(params) {
|
|
50
54
|
super(params.value, params.enabled, !!params.isDefault);
|
|
51
55
|
this.featureId = params.featureId;
|
|
52
56
|
this.featureName = params.featureName;
|
|
57
|
+
this.reason = params.reason;
|
|
53
58
|
}
|
|
54
59
|
static fromFeatureStateModel(fsm, identityId) {
|
|
55
60
|
return new Flag({
|
|
@@ -64,7 +69,8 @@ class Flag extends BaseFlag {
|
|
|
64
69
|
enabled: flagData['enabled'],
|
|
65
70
|
value: flagData['feature_state_value'] ?? flagData['value'],
|
|
66
71
|
featureId: flagData['feature']['id'],
|
|
67
|
-
featureName: flagData['feature']['name']
|
|
72
|
+
featureName: flagData['feature']['name'],
|
|
73
|
+
reason: flagData['feature']['reason']
|
|
68
74
|
});
|
|
69
75
|
}
|
|
70
76
|
}
|
|
@@ -78,6 +84,28 @@ class Flags {
|
|
|
78
84
|
this.defaultFlagHandler = data.defaultFlagHandler;
|
|
79
85
|
this.analyticsProcessor = data.analyticsProcessor;
|
|
80
86
|
}
|
|
87
|
+
static fromEvaluationResult(evaluationResult, defaultFlagHandler, analyticsProcessor) {
|
|
88
|
+
const flags = {};
|
|
89
|
+
for (const flag of Object.values(evaluationResult.flags)) {
|
|
90
|
+
const flagMetadataId = flag.metadata?.id;
|
|
91
|
+
if (!flagMetadataId) {
|
|
92
|
+
throw new Error(`FlagResult metadata.id is missing for feature "${flag.name}". ` +
|
|
93
|
+
`This indicates a bug in the SDK, please report it.`);
|
|
94
|
+
}
|
|
95
|
+
flags[flag.name] = new Flag({
|
|
96
|
+
enabled: flag.enabled,
|
|
97
|
+
value: flag.value ?? null,
|
|
98
|
+
featureId: flagMetadataId,
|
|
99
|
+
featureName: flag.name,
|
|
100
|
+
reason: flag.reason
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return new Flags({
|
|
104
|
+
flags: flags,
|
|
105
|
+
defaultFlagHandler: defaultFlagHandler,
|
|
106
|
+
analyticsProcessor: analyticsProcessor
|
|
107
|
+
});
|
|
108
|
+
}
|
|
81
109
|
static fromFeatureStateModels(data) {
|
|
82
110
|
const flags = {};
|
|
83
111
|
for (const fs of data.featureStates) {
|
|
@@ -18,5 +18,6 @@ export declare class EnvironmentModel {
|
|
|
18
18
|
project: ProjectModel;
|
|
19
19
|
featureStates: FeatureStateModel[];
|
|
20
20
|
identityOverrides: IdentityModel[];
|
|
21
|
-
|
|
21
|
+
name: string;
|
|
22
|
+
constructor(id: number, apiKey: string, project: ProjectModel, name: string);
|
|
22
23
|
}
|
|
@@ -24,9 +24,11 @@ export class EnvironmentModel {
|
|
|
24
24
|
project;
|
|
25
25
|
featureStates = [];
|
|
26
26
|
identityOverrides = [];
|
|
27
|
-
|
|
27
|
+
name;
|
|
28
|
+
constructor(id, apiKey, project, name) {
|
|
28
29
|
this.id = id;
|
|
29
30
|
this.apiKey = apiKey;
|
|
30
31
|
this.project = project;
|
|
32
|
+
this.name = name;
|
|
31
33
|
}
|
|
32
34
|
}
|
|
@@ -5,7 +5,7 @@ import { EnvironmentAPIKeyModel, EnvironmentModel } from './models.js';
|
|
|
5
5
|
export function buildEnvironmentModel(environmentJSON) {
|
|
6
6
|
const project = buildProjectModel(environmentJSON.project);
|
|
7
7
|
const featureStates = environmentJSON.feature_states.map((fs) => buildFeatureStateModel(fs));
|
|
8
|
-
const environmentModel = new EnvironmentModel(environmentJSON.id, environmentJSON.api_key, project);
|
|
8
|
+
const environmentModel = new EnvironmentModel(environmentJSON.id, environmentJSON.api_key, project, environmentJSON.name);
|
|
9
9
|
environmentModel.featureStates = featureStates;
|
|
10
10
|
if (!!environmentJSON.identity_overrides) {
|
|
11
11
|
environmentModel.identityOverrides = environmentJSON.identity_overrides.map((identityData) => buildIdentityModel(identityData));
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file was automatically generated by json-schema-to-typescript.
|
|
3
|
+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
|
|
4
|
+
* and run json-schema-to-typescript to regenerate this file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Unique environment key. May be used for selecting a value for a multivariate feature, or for % split segmentation.
|
|
8
|
+
*/
|
|
9
|
+
export type Key = string;
|
|
10
|
+
/**
|
|
11
|
+
* An environment's human-readable name.
|
|
12
|
+
*/
|
|
13
|
+
export type Name = string;
|
|
14
|
+
/**
|
|
15
|
+
* A unique identifier for an identity as displayed in the Flagsmith UI.
|
|
16
|
+
*/
|
|
17
|
+
export type Identifier = string;
|
|
18
|
+
/**
|
|
19
|
+
* Key used when selecting a value for a multivariate feature, or for % split segmentation. Set to an internal identifier or a composite value based on the environment key and identifier, depending on Flagsmith implementation.
|
|
20
|
+
*/
|
|
21
|
+
export type Key1 = string;
|
|
22
|
+
/**
|
|
23
|
+
* Unique segment key used for % split segmentation.
|
|
24
|
+
*/
|
|
25
|
+
export type Key2 = string;
|
|
26
|
+
/**
|
|
27
|
+
* The name of the segment.
|
|
28
|
+
*/
|
|
29
|
+
export type Name1 = string;
|
|
30
|
+
/**
|
|
31
|
+
* Segment rule type. Represents a logical quantifier for the conditions and sub-rules.
|
|
32
|
+
*/
|
|
33
|
+
export type Type = 'ALL' | 'ANY' | 'NONE';
|
|
34
|
+
export type SegmentCondition = SegmentCondition1 | InSegmentCondition;
|
|
35
|
+
/**
|
|
36
|
+
* A reference to the identity trait or value in the evaluation context.
|
|
37
|
+
*/
|
|
38
|
+
export type Property = string;
|
|
39
|
+
/**
|
|
40
|
+
* The operator to use for evaluating the condition.
|
|
41
|
+
*/
|
|
42
|
+
export type Operator = 'EQUAL' | 'GREATER_THAN' | 'LESS_THAN' | 'LESS_THAN_INCLUSIVE' | 'CONTAINS' | 'GREATER_THAN_INCLUSIVE' | 'NOT_CONTAINS' | 'NOT_EQUAL' | 'REGEX' | 'PERCENTAGE_SPLIT' | 'MODULO' | 'IS_SET' | 'IS_NOT_SET' | 'IN';
|
|
43
|
+
/**
|
|
44
|
+
* The value to compare against the trait or context value.
|
|
45
|
+
*/
|
|
46
|
+
export type Value = string;
|
|
47
|
+
/**
|
|
48
|
+
* A reference to the identity trait or value in the evaluation context.
|
|
49
|
+
*/
|
|
50
|
+
export type Property1 = string;
|
|
51
|
+
/**
|
|
52
|
+
* The operator to use for evaluating the condition.
|
|
53
|
+
*/
|
|
54
|
+
export type Operator1 = 'IN';
|
|
55
|
+
/**
|
|
56
|
+
* The values to compare against the trait or context value.
|
|
57
|
+
*/
|
|
58
|
+
export type Value1 = string[];
|
|
59
|
+
/**
|
|
60
|
+
* Conditions that must be met for the rule to apply.
|
|
61
|
+
*/
|
|
62
|
+
export type Conditions = SegmentCondition[];
|
|
63
|
+
/**
|
|
64
|
+
* Sub-rules nested within the segment rule.
|
|
65
|
+
*/
|
|
66
|
+
export type SubRules = SegmentRule[];
|
|
67
|
+
/**
|
|
68
|
+
* Rules that define the segment.
|
|
69
|
+
*/
|
|
70
|
+
export type Rules = SegmentRule[];
|
|
71
|
+
/**
|
|
72
|
+
* Unique feature key used when selecting a variant if the feature is multivariate. Set to an internal identifier or a UUID, depending on Flagsmith implementation.
|
|
73
|
+
*/
|
|
74
|
+
export type Key3 = string;
|
|
75
|
+
/**
|
|
76
|
+
* Feature name.
|
|
77
|
+
*/
|
|
78
|
+
export type Name2 = string;
|
|
79
|
+
/**
|
|
80
|
+
* Indicates whether the feature is enabled in the environment.
|
|
81
|
+
*/
|
|
82
|
+
export type Enabled = boolean;
|
|
83
|
+
/**
|
|
84
|
+
* A default environment value for the feature. If the feature is multivariate, this will be the control value.
|
|
85
|
+
*/
|
|
86
|
+
export type Value2 = string | number | boolean | null;
|
|
87
|
+
/**
|
|
88
|
+
* The value of the feature.
|
|
89
|
+
*/
|
|
90
|
+
export type Value3 = string | number | boolean | null;
|
|
91
|
+
/**
|
|
92
|
+
* The weight of the feature value variant, as a percentage number (i.e. 100.0).
|
|
93
|
+
*/
|
|
94
|
+
export type Weight = number;
|
|
95
|
+
/**
|
|
96
|
+
* Priority of the feature flag variant. Lower values indicate a higher priority when multiple variants apply to the same context key.
|
|
97
|
+
*/
|
|
98
|
+
export type VariantPriority = number;
|
|
99
|
+
/**
|
|
100
|
+
* An array of environment default values associated with the feature. Empty for standard features, or contains multiple values for multivariate features.
|
|
101
|
+
*/
|
|
102
|
+
export type Variants = FeatureValue[];
|
|
103
|
+
/**
|
|
104
|
+
* Priority of the feature context. Lower values indicate a higher priority when multiple contexts apply to the same feature.
|
|
105
|
+
*/
|
|
106
|
+
export type FeaturePriority = number;
|
|
107
|
+
/**
|
|
108
|
+
* Feature overrides for the segment.
|
|
109
|
+
*/
|
|
110
|
+
export type Overrides = FeatureContext[];
|
|
111
|
+
/**
|
|
112
|
+
* A context object containing the necessary information to evaluate Flagsmith feature flags.
|
|
113
|
+
*/
|
|
114
|
+
export interface EvaluationContext {
|
|
115
|
+
environment: EnvironmentContext;
|
|
116
|
+
/**
|
|
117
|
+
* Identity context used for identity-based evaluation.
|
|
118
|
+
*/
|
|
119
|
+
identity?: IdentityContext | null;
|
|
120
|
+
segments?: Segments;
|
|
121
|
+
features?: Features;
|
|
122
|
+
[k: string]: unknown;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Environment context required for evaluation.
|
|
126
|
+
*/
|
|
127
|
+
export interface EnvironmentContext {
|
|
128
|
+
key: Key;
|
|
129
|
+
name: Name;
|
|
130
|
+
[k: string]: unknown;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Represents an identity context for feature flag evaluation.
|
|
134
|
+
*/
|
|
135
|
+
export interface IdentityContext {
|
|
136
|
+
identifier: Identifier;
|
|
137
|
+
key?: Key1;
|
|
138
|
+
traits?: Traits;
|
|
139
|
+
[k: string]: unknown;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* A map of traits associated with the identity, where the key is the trait name and the value is the trait value.
|
|
143
|
+
*/
|
|
144
|
+
export interface Traits {
|
|
145
|
+
[k: string]: string | number | boolean | null;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Segments applicable to the evaluation context.
|
|
149
|
+
*/
|
|
150
|
+
export interface Segments {
|
|
151
|
+
[k: string]: SegmentContext;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Represents a segment context for feature flag evaluation.
|
|
155
|
+
*/
|
|
156
|
+
export interface SegmentContext {
|
|
157
|
+
key: Key2;
|
|
158
|
+
name: Name1;
|
|
159
|
+
rules: Rules;
|
|
160
|
+
overrides?: Overrides;
|
|
161
|
+
metadata?: SegmentMetadata;
|
|
162
|
+
[k: string]: unknown;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Represents a rule within a segment for feature flag evaluation.
|
|
166
|
+
*/
|
|
167
|
+
export interface SegmentRule {
|
|
168
|
+
type: Type;
|
|
169
|
+
conditions?: Conditions;
|
|
170
|
+
rules?: SubRules;
|
|
171
|
+
[k: string]: unknown;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Represents a condition within a segment rule for feature flag evaluation.
|
|
175
|
+
*/
|
|
176
|
+
export interface SegmentCondition1 {
|
|
177
|
+
property: Property;
|
|
178
|
+
operator: Operator;
|
|
179
|
+
value: Value;
|
|
180
|
+
[k: string]: unknown;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Represents an IN condition within a segment rule for feature flag evaluation.
|
|
184
|
+
*/
|
|
185
|
+
export interface InSegmentCondition {
|
|
186
|
+
property: Property1;
|
|
187
|
+
operator: Operator1;
|
|
188
|
+
value: Value1;
|
|
189
|
+
[k: string]: unknown;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Represents a feature context for feature flag evaluation.
|
|
193
|
+
*/
|
|
194
|
+
export interface FeatureContext {
|
|
195
|
+
key: Key3;
|
|
196
|
+
name: Name2;
|
|
197
|
+
enabled: Enabled;
|
|
198
|
+
value: Value2;
|
|
199
|
+
variants?: Variants;
|
|
200
|
+
priority?: FeaturePriority;
|
|
201
|
+
metadata?: FeatureMetadata;
|
|
202
|
+
[k: string]: unknown;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Represents a multivariate value for a feature flag.
|
|
206
|
+
*/
|
|
207
|
+
export interface FeatureValue {
|
|
208
|
+
value: Value3;
|
|
209
|
+
weight: Weight;
|
|
210
|
+
priority: VariantPriority;
|
|
211
|
+
[k: string]: unknown;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Additional metadata associated with the feature.
|
|
215
|
+
*/
|
|
216
|
+
export interface FeatureMetadata {
|
|
217
|
+
[k: string]: unknown;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Additional metadata associated with the segment.
|
|
221
|
+
*/
|
|
222
|
+
export interface SegmentMetadata {
|
|
223
|
+
[k: string]: unknown;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Features to be evaluated in the context.
|
|
227
|
+
*/
|
|
228
|
+
export interface Features {
|
|
229
|
+
[k: string]: FeatureContext;
|
|
230
|
+
}
|