flagsmith-nodejs 3.3.2 → 4.0.0-beta.1
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 +2 -2
- package/.github/workflows/pull_request.yaml +3 -4
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/environments/models.d.ts +3 -3
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/environments/models.js +20 -13
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/environments/util.d.ts +1 -1
- package/build/cjs/flagsmith-engine/environments/util.js +23 -0
- package/build/cjs/flagsmith-engine/features/models.js +118 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/features/util.d.ts +1 -1
- package/build/cjs/flagsmith-engine/features/util.js +27 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/identities/models.d.ts +2 -2
- package/build/cjs/flagsmith-engine/identities/models.js +48 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/identities/traits/models.js +5 -4
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/identities/util.d.ts +2 -2
- package/build/cjs/flagsmith-engine/identities/util.js +22 -0
- package/build/cjs/flagsmith-engine/index.d.ts +14 -0
- package/build/cjs/flagsmith-engine/index.js +75 -0
- package/build/cjs/flagsmith-engine/organisations/models.js +21 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/organisations/util.d.ts +1 -1
- package/build/cjs/flagsmith-engine/organisations/util.js +8 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/projects/models.d.ts +2 -2
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/projects/models.js +8 -5
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/projects/util.d.ts +1 -1
- package/build/cjs/flagsmith-engine/projects/util.js +15 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/evaluators.d.ts +4 -4
- package/build/cjs/flagsmith-engine/segments/evaluators.js +37 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/models.d.ts +1 -1
- package/build/cjs/flagsmith-engine/segments/models.js +114 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/util.d.ts +1 -1
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/util.js +9 -11
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/collections.d.ts +1 -1
- package/build/cjs/flagsmith-engine/utils/collections.js +6 -0
- package/build/cjs/flagsmith-engine/utils/errors.js +6 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/hashing/index.js +8 -11
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/index.js +5 -5
- package/build/{index.d.ts → cjs/index.d.ts} +3 -3
- package/build/{index.js → cjs/index.js} +17 -17
- package/build/cjs/package.json +1 -0
- package/build/{sdk → cjs/sdk}/analytics.d.ts +3 -0
- package/build/cjs/sdk/analytics.js +73 -0
- package/build/cjs/sdk/errors.js +9 -0
- package/build/{sdk → cjs/sdk}/index.d.ts +19 -18
- package/build/cjs/sdk/index.js +400 -0
- package/build/{sdk → cjs/sdk}/models.d.ts +2 -2
- package/build/cjs/sdk/models.js +101 -0
- package/build/{sdk → cjs/sdk}/offline_handlers.d.ts +1 -1
- package/build/cjs/sdk/offline_handlers.js +46 -0
- package/build/{sdk → cjs/sdk}/polling_manager.d.ts +1 -1
- package/build/cjs/sdk/polling_manager.js +29 -0
- package/build/{sdk → cjs/sdk}/types.d.ts +15 -7
- package/build/cjs/sdk/utils.d.ts +36 -0
- package/build/cjs/sdk/utils.js +63 -0
- package/build/esm/flagsmith-engine/environments/models.d.ts +22 -0
- package/build/esm/flagsmith-engine/environments/models.js +32 -0
- package/build/esm/flagsmith-engine/environments/util.d.ts +3 -0
- package/build/esm/flagsmith-engine/environments/util.js +18 -0
- package/build/esm/flagsmith-engine/features/constants.d.ts +4 -0
- package/build/esm/flagsmith-engine/features/constants.js +4 -0
- package/build/esm/flagsmith-engine/features/models.d.ts +37 -0
- package/build/esm/flagsmith-engine/features/models.js +110 -0
- package/build/esm/flagsmith-engine/features/util.d.ts +4 -0
- package/build/esm/flagsmith-engine/features/util.js +21 -0
- package/build/esm/flagsmith-engine/identities/models.d.ts +15 -0
- package/build/esm/flagsmith-engine/identities/models.js +44 -0
- package/build/esm/flagsmith-engine/identities/traits/models.d.ts +5 -0
- package/build/esm/flagsmith-engine/identities/traits/models.js +8 -0
- package/build/esm/flagsmith-engine/identities/util.d.ts +4 -0
- package/build/esm/flagsmith-engine/identities/util.js +17 -0
- package/build/esm/flagsmith-engine/index.d.ts +14 -0
- package/build/esm/flagsmith-engine/index.js +62 -0
- package/build/esm/flagsmith-engine/organisations/models.d.ts +9 -0
- package/build/esm/flagsmith-engine/organisations/models.js +17 -0
- package/build/esm/flagsmith-engine/organisations/util.d.ts +2 -0
- package/build/esm/flagsmith-engine/organisations/util.js +4 -0
- package/build/esm/flagsmith-engine/projects/models.d.ts +10 -0
- package/build/esm/flagsmith-engine/projects/models.js +13 -0
- package/build/esm/flagsmith-engine/projects/util.d.ts +2 -0
- package/build/esm/flagsmith-engine/projects/util.js +11 -0
- package/build/esm/flagsmith-engine/segments/constants.d.ts +34 -0
- package/build/esm/flagsmith-engine/segments/constants.js +36 -0
- package/build/esm/flagsmith-engine/segments/evaluators.d.ts +7 -0
- package/build/esm/flagsmith-engine/segments/evaluators.js +31 -0
- package/build/esm/flagsmith-engine/segments/models.d.ts +37 -0
- package/build/esm/flagsmith-engine/segments/models.js +102 -0
- package/build/esm/flagsmith-engine/segments/util.d.ts +6 -0
- package/build/esm/flagsmith-engine/segments/util.js +23 -0
- package/build/esm/flagsmith-engine/utils/collections.d.ts +3 -0
- package/build/esm/flagsmith-engine/utils/collections.js +2 -0
- package/build/esm/flagsmith-engine/utils/errors.d.ts +2 -0
- package/build/esm/flagsmith-engine/utils/errors.js +2 -0
- package/build/esm/flagsmith-engine/utils/hashing/index.d.ts +9 -0
- package/build/esm/flagsmith-engine/utils/hashing/index.js +50 -0
- package/build/esm/flagsmith-engine/utils/index.d.ts +1 -0
- package/build/esm/flagsmith-engine/utils/index.js +13 -0
- package/build/esm/index.d.ts +3 -0
- package/build/esm/index.js +4 -0
- package/build/esm/sdk/analytics.d.ts +35 -0
- package/build/esm/sdk/analytics.js +69 -0
- package/build/esm/sdk/errors.d.ts +4 -0
- package/build/esm/sdk/errors.js +4 -0
- package/build/esm/sdk/index.d.ts +131 -0
- package/build/esm/sdk/index.js +390 -0
- package/build/esm/sdk/models.d.ts +55 -0
- package/build/esm/sdk/models.js +94 -0
- package/build/esm/sdk/offline_handlers.d.ts +9 -0
- package/build/esm/sdk/offline_handlers.js +18 -0
- package/build/esm/sdk/polling_manager.d.ts +9 -0
- package/build/esm/sdk/polling_manager.js +25 -0
- package/build/esm/sdk/types.d.ts +38 -0
- package/build/esm/sdk/types.js +1 -0
- package/build/esm/sdk/utils.d.ts +36 -0
- package/build/esm/sdk/utils.js +56 -0
- package/flagsmith-engine/environments/models.ts +3 -3
- package/flagsmith-engine/environments/util.ts +4 -4
- package/flagsmith-engine/features/models.ts +1 -1
- package/flagsmith-engine/features/util.ts +1 -1
- package/flagsmith-engine/identities/models.ts +3 -4
- package/flagsmith-engine/identities/traits/models.ts +0 -1
- package/flagsmith-engine/identities/util.ts +4 -4
- package/flagsmith-engine/index.ts +13 -13
- package/flagsmith-engine/organisations/util.ts +1 -1
- package/flagsmith-engine/projects/models.ts +2 -2
- package/flagsmith-engine/projects/util.ts +4 -4
- package/flagsmith-engine/segments/evaluators.ts +6 -6
- package/flagsmith-engine/segments/models.ts +4 -4
- package/flagsmith-engine/segments/util.ts +3 -3
- package/flagsmith-engine/utils/collections.ts +1 -1
- package/flagsmith-engine/utils/index.ts +1 -1
- package/index.ts +4 -4
- package/package.json +21 -9
- package/sdk/analytics.ts +7 -5
- package/sdk/index.ts +55 -46
- package/sdk/models.ts +2 -3
- package/sdk/offline_handlers.ts +2 -2
- package/sdk/polling_manager.ts +2 -3
- package/sdk/types.ts +35 -24
- package/sdk/utils.ts +49 -29
- package/tests/engine/e2e/engine.test.ts +5 -5
- package/tests/engine/unit/engine.test.ts +5 -5
- package/tests/engine/unit/segments/segment_evaluators.test.ts +9 -9
- package/tests/engine/unit/utils/utils.test.ts +1 -1
- package/tests/sdk/analytics.test.ts +8 -13
- package/tests/sdk/data/identity-with-transient-traits.json +41 -0
- package/tests/sdk/data/transient-identity.json +29 -0
- package/tests/sdk/flagsmith-cache.test.ts +16 -32
- package/tests/sdk/flagsmith-environment-flags.test.ts +21 -36
- package/tests/sdk/flagsmith-identity-flags.test.ts +83 -32
- package/tests/sdk/flagsmith.test.ts +67 -99
- package/tests/sdk/offline-handlers.test.ts +4 -5
- package/tests/sdk/polling.test.ts +6 -8
- package/tests/sdk/utils.ts +19 -15
- package/tsconfig.cjs.json +7 -0
- package/tsconfig.esm.json +7 -0
- package/tsconfig.json +7 -3
- package/vitest.config.ts +17 -0
- package/build/flagsmith-engine/environments/util.js +0 -27
- package/build/flagsmith-engine/features/models.js +0 -132
- package/build/flagsmith-engine/features/util.js +0 -27
- package/build/flagsmith-engine/identities/models.js +0 -113
- package/build/flagsmith-engine/identities/util.js +0 -46
- package/build/flagsmith-engine/index.d.ts +0 -14
- package/build/flagsmith-engine/index.js +0 -127
- package/build/flagsmith-engine/organisations/models.js +0 -21
- package/build/flagsmith-engine/organisations/util.js +0 -8
- package/build/flagsmith-engine/projects/util.js +0 -15
- package/build/flagsmith-engine/segments/evaluators.js +0 -45
- package/build/flagsmith-engine/segments/models.js +0 -147
- package/build/flagsmith-engine/utils/collections.js +0 -26
- package/build/flagsmith-engine/utils/errors.js +0 -26
- package/build/sdk/analytics.js +0 -120
- package/build/sdk/errors.js +0 -34
- package/build/sdk/index.js +0 -594
- package/build/sdk/models.js +0 -149
- package/build/sdk/offline_handlers.js +0 -66
- package/build/sdk/polling_manager.js +0 -72
- package/build/sdk/utils.d.ts +0 -12
- package/build/sdk/utils.js +0 -100
- package/jest.config.js +0 -5
- package/tests/index.js +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/features/constants.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/features/constants.js +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/features/models.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/identities/traits/models.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/organisations/models.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/constants.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/constants.js +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/errors.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/hashing/index.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/index.d.ts +0 -0
- /package/build/{sdk → cjs/sdk}/errors.d.ts +0 -0
- /package/build/{sdk → cjs/sdk}/types.js +0 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SegmentModel = exports.SegmentRuleModel = exports.SegmentConditionModel = exports.getMatchingFunctions = exports.semverMatchingFunction = exports.matchingFunctions = exports.any = exports.all = void 0;
|
|
7
|
+
const semver_1 = __importDefault(require("semver"));
|
|
8
|
+
const index_js_1 = require("../utils/index.js");
|
|
9
|
+
const constants_js_1 = require("./constants.js");
|
|
10
|
+
const util_js_1 = require("./util.js");
|
|
11
|
+
const all = (iterable) => iterable.filter(e => !!e).length === iterable.length;
|
|
12
|
+
exports.all = all;
|
|
13
|
+
const any = (iterable) => iterable.filter(e => !!e).length > 0;
|
|
14
|
+
exports.any = any;
|
|
15
|
+
exports.matchingFunctions = {
|
|
16
|
+
[constants_js_1.CONDITION_OPERATORS.EQUAL]: (thisValue, otherValue) => thisValue == otherValue,
|
|
17
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN]: (thisValue, otherValue) => otherValue > thisValue,
|
|
18
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue, otherValue) => otherValue >= thisValue,
|
|
19
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN]: (thisValue, otherValue) => thisValue > otherValue,
|
|
20
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue, otherValue) => thisValue >= otherValue,
|
|
21
|
+
[constants_js_1.CONDITION_OPERATORS.NOT_EQUAL]: (thisValue, otherValue) => thisValue != otherValue,
|
|
22
|
+
[constants_js_1.CONDITION_OPERATORS.CONTAINS]: (thisValue, otherValue) => !!otherValue && otherValue.includes(thisValue),
|
|
23
|
+
};
|
|
24
|
+
exports.semverMatchingFunction = {
|
|
25
|
+
...exports.matchingFunctions,
|
|
26
|
+
[constants_js_1.CONDITION_OPERATORS.EQUAL]: (thisValue, otherValue) => semver_1.default.eq(thisValue, otherValue),
|
|
27
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN]: (thisValue, otherValue) => semver_1.default.gt(otherValue, thisValue),
|
|
28
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue, otherValue) => semver_1.default.gte(otherValue, thisValue),
|
|
29
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN]: (thisValue, otherValue) => semver_1.default.gt(thisValue, otherValue),
|
|
30
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue, otherValue) => semver_1.default.gte(thisValue, otherValue),
|
|
31
|
+
};
|
|
32
|
+
const getMatchingFunctions = (semver) => (semver ? exports.semverMatchingFunction : exports.matchingFunctions);
|
|
33
|
+
exports.getMatchingFunctions = getMatchingFunctions;
|
|
34
|
+
class SegmentConditionModel {
|
|
35
|
+
EXCEPTION_OPERATOR_METHODS = {
|
|
36
|
+
[constants_js_1.NOT_CONTAINS]: 'evaluateNotContains',
|
|
37
|
+
[constants_js_1.REGEX]: 'evaluateRegex',
|
|
38
|
+
[constants_js_1.MODULO]: 'evaluateModulo',
|
|
39
|
+
[constants_js_1.IN]: 'evaluateIn'
|
|
40
|
+
};
|
|
41
|
+
operator;
|
|
42
|
+
value;
|
|
43
|
+
property_;
|
|
44
|
+
constructor(operator, value, property) {
|
|
45
|
+
this.operator = operator;
|
|
46
|
+
this.value = value;
|
|
47
|
+
this.property_ = property;
|
|
48
|
+
}
|
|
49
|
+
matchesTraitValue(traitValue) {
|
|
50
|
+
const evaluators = {
|
|
51
|
+
evaluateNotContains: (traitValue) => {
|
|
52
|
+
return typeof traitValue == "string" &&
|
|
53
|
+
!!this.value &&
|
|
54
|
+
!traitValue.includes(this.value?.toString());
|
|
55
|
+
},
|
|
56
|
+
evaluateRegex: (traitValue) => {
|
|
57
|
+
return !!this.value && !!traitValue?.toString().match(new RegExp(this.value));
|
|
58
|
+
},
|
|
59
|
+
evaluateModulo: (traitValue) => {
|
|
60
|
+
if (isNaN(parseFloat(traitValue)) || !this.value) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const parts = (this.value).split("|");
|
|
64
|
+
const [divisor, reminder] = [parseFloat(parts[0]), parseFloat(parts[1])];
|
|
65
|
+
return traitValue % divisor === reminder;
|
|
66
|
+
},
|
|
67
|
+
evaluateIn: (traitValue) => {
|
|
68
|
+
return this.value?.split(',').includes(traitValue.toString());
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
// TODO: move this logic to the evaluator module
|
|
72
|
+
if (this.EXCEPTION_OPERATOR_METHODS[this.operator]) {
|
|
73
|
+
const evaluatorFunction = evaluators[this.EXCEPTION_OPERATOR_METHODS[this.operator]];
|
|
74
|
+
return evaluatorFunction(traitValue);
|
|
75
|
+
}
|
|
76
|
+
const defaultFunction = (x, y) => false;
|
|
77
|
+
const matchingFunctionSet = (0, exports.getMatchingFunctions)((0, util_js_1.isSemver)(this.value));
|
|
78
|
+
const matchingFunction = matchingFunctionSet[this.operator] || defaultFunction;
|
|
79
|
+
const traitType = (0, util_js_1.isSemver)(this.value) ? 'semver' : typeof traitValue;
|
|
80
|
+
const castToTypeOfTraitValue = (0, index_js_1.getCastingFunction)(traitType);
|
|
81
|
+
return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.SegmentConditionModel = SegmentConditionModel;
|
|
85
|
+
class SegmentRuleModel {
|
|
86
|
+
type;
|
|
87
|
+
rules = [];
|
|
88
|
+
conditions = [];
|
|
89
|
+
constructor(type) {
|
|
90
|
+
this.type = type;
|
|
91
|
+
}
|
|
92
|
+
static none(iterable) {
|
|
93
|
+
return iterable.filter(e => !!e).length === 0;
|
|
94
|
+
}
|
|
95
|
+
matchingFunction() {
|
|
96
|
+
return {
|
|
97
|
+
[constants_js_1.ANY_RULE]: exports.any,
|
|
98
|
+
[constants_js_1.ALL_RULE]: exports.all,
|
|
99
|
+
[constants_js_1.NONE_RULE]: SegmentRuleModel.none
|
|
100
|
+
}[this.type];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.SegmentRuleModel = SegmentRuleModel;
|
|
104
|
+
class SegmentModel {
|
|
105
|
+
id;
|
|
106
|
+
name;
|
|
107
|
+
rules = [];
|
|
108
|
+
featureStates = [];
|
|
109
|
+
constructor(id, name) {
|
|
110
|
+
this.id = id;
|
|
111
|
+
this.name = name;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.SegmentModel = SegmentModel;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models';
|
|
1
|
+
import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models.js';
|
|
2
2
|
export declare function buildSegmentConditionModel(segmentConditionJSON: any): SegmentConditionModel;
|
|
3
3
|
export declare function buildSegmentRuleModel(ruleModelJSON: any): SegmentRuleModel;
|
|
4
4
|
export declare function buildSegmentModel(segmentModelJSON: any): SegmentModel;
|
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.removeSemverSuffix = exports.isSemver = exports.buildSegmentModel = exports.buildSegmentRuleModel = exports.buildSegmentConditionModel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const util_js_1 = require("../features/util.js");
|
|
5
|
+
const models_js_1 = require("./models.js");
|
|
6
6
|
function buildSegmentConditionModel(segmentConditionJSON) {
|
|
7
|
-
return new
|
|
7
|
+
return new models_js_1.SegmentConditionModel(segmentConditionJSON.operator, segmentConditionJSON.value, segmentConditionJSON.property_);
|
|
8
8
|
}
|
|
9
9
|
exports.buildSegmentConditionModel = buildSegmentConditionModel;
|
|
10
10
|
function buildSegmentRuleModel(ruleModelJSON) {
|
|
11
|
-
|
|
12
|
-
ruleModel.rules = ruleModelJSON.rules.map(
|
|
13
|
-
ruleModel.conditions = ruleModelJSON.conditions.map(
|
|
11
|
+
const ruleModel = new models_js_1.SegmentRuleModel(ruleModelJSON.type);
|
|
12
|
+
ruleModel.rules = ruleModelJSON.rules.map((r) => buildSegmentRuleModel(r));
|
|
13
|
+
ruleModel.conditions = ruleModelJSON.conditions.map((c) => buildSegmentConditionModel(c));
|
|
14
14
|
return ruleModel;
|
|
15
15
|
}
|
|
16
16
|
exports.buildSegmentRuleModel = buildSegmentRuleModel;
|
|
17
17
|
function buildSegmentModel(segmentModelJSON) {
|
|
18
|
-
|
|
19
|
-
model.featureStates = segmentModelJSON['feature_states'].map(
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
model.rules = segmentModelJSON['rules'].map(function (r) { return buildSegmentRuleModel(r); });
|
|
18
|
+
const model = new models_js_1.SegmentModel(segmentModelJSON.id, segmentModelJSON.name);
|
|
19
|
+
model.featureStates = segmentModelJSON['feature_states'].map((fs) => (0, util_js_1.buildFeatureStateModel)(fs));
|
|
20
|
+
model.rules = segmentModelJSON['rules'].map((r) => buildSegmentRuleModel(r));
|
|
23
21
|
return model;
|
|
24
22
|
}
|
|
25
23
|
exports.buildSegmentModel = buildSegmentModel;
|
|
@@ -4,11 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getHashedPercentateForObjIds = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return Array.from({ length: repeats }, function () { return arr; }).flat();
|
|
11
|
-
};
|
|
7
|
+
const md5_1 = __importDefault(require("md5"));
|
|
8
|
+
const big_integer_1 = __importDefault(require("big-integer"));
|
|
9
|
+
const makeRepeated = (arr, repeats) => Array.from({ length: repeats }, () => arr).flat();
|
|
12
10
|
// https://stackoverflow.com/questions/12532871/how-to-convert-a-very-large-hex-number-to-decimal-in-javascript
|
|
13
11
|
function h2d(s) {
|
|
14
12
|
function add(x, y) {
|
|
@@ -43,12 +41,11 @@ function h2d(s) {
|
|
|
43
41
|
* @param {} iterations=1 num times to include each id in the generated string to hash
|
|
44
42
|
* @returns number number between 0 (inclusive) and 100 (exclusive)
|
|
45
43
|
*/
|
|
46
|
-
function getHashedPercentateForObjIds(objectIds, iterations) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
var value = (hashedInt.mod(9999).toJSNumber() / 9998) * 100;
|
|
44
|
+
function getHashedPercentateForObjIds(objectIds, iterations = 1) {
|
|
45
|
+
let toHash = makeRepeated(objectIds, iterations).join(',');
|
|
46
|
+
const hashedValue = (0, md5_1.default)(toHash);
|
|
47
|
+
const hashedInt = (0, big_integer_1.default)(h2d(hashedValue));
|
|
48
|
+
const value = (hashedInt.mod(9999).toJSNumber() / 9998) * 100;
|
|
52
49
|
// we ignore this for it's nearly impossible use case to catch
|
|
53
50
|
/* istanbul ignore next */
|
|
54
51
|
if (value === 100) {
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getCastingFunction = void 0;
|
|
4
|
-
|
|
4
|
+
const util_js_1 = require("../segments/util.js");
|
|
5
5
|
function getCastingFunction(traitType) {
|
|
6
6
|
switch (traitType) {
|
|
7
7
|
case 'boolean':
|
|
8
|
-
return
|
|
8
|
+
return (x) => !['False', 'false'].includes(x);
|
|
9
9
|
case 'number':
|
|
10
|
-
return
|
|
10
|
+
return (x) => parseFloat(x);
|
|
11
11
|
case 'semver':
|
|
12
|
-
return
|
|
12
|
+
return (x) => (0, util_js_1.removeSemverSuffix)(x);
|
|
13
13
|
default:
|
|
14
|
-
return
|
|
14
|
+
return (x) => String(x);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
exports.getCastingFunction = getCastingFunction;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { AnalyticsProcessor, FlagsmithAPIError, FlagsmithClientError, EnvironmentDataPollingManager, FlagsmithCache, DefaultFlag, Flags, default } from './sdk';
|
|
2
|
-
export { FlagsmithConfig } from './sdk/types';
|
|
3
|
-
export { EnvironmentModel, FeatureStateModel, IdentityModel, TraitModel, SegmentModel, OrganisationModel } from './flagsmith-engine';
|
|
1
|
+
export { AnalyticsProcessor, FlagsmithAPIError, FlagsmithClientError, EnvironmentDataPollingManager, FlagsmithCache, DefaultFlag, Flags, default } from './sdk/index.js';
|
|
2
|
+
export { FlagsmithConfig } from './sdk/types.js';
|
|
3
|
+
export { EnvironmentModel, FeatureStateModel, IdentityModel, TraitModel, SegmentModel, OrganisationModel } from './flagsmith-engine/index.js';
|
|
@@ -4,20 +4,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.OrganisationModel = exports.SegmentModel = exports.TraitModel = exports.IdentityModel = exports.FeatureStateModel = exports.EnvironmentModel = exports.default = exports.Flags = exports.DefaultFlag = exports.EnvironmentDataPollingManager = exports.FlagsmithClientError = exports.FlagsmithAPIError = exports.AnalyticsProcessor = void 0;
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
Object.defineProperty(exports, "AnalyticsProcessor", { enumerable: true, get: function () { return
|
|
10
|
-
Object.defineProperty(exports, "FlagsmithAPIError", { enumerable: true, get: function () { return
|
|
11
|
-
Object.defineProperty(exports, "FlagsmithClientError", { enumerable: true, get: function () { return
|
|
12
|
-
Object.defineProperty(exports, "EnvironmentDataPollingManager", { enumerable: true, get: function () { return
|
|
13
|
-
Object.defineProperty(exports, "DefaultFlag", { enumerable: true, get: function () { return
|
|
14
|
-
Object.defineProperty(exports, "Flags", { enumerable: true, get: function () { return
|
|
15
|
-
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(
|
|
16
|
-
var
|
|
17
|
-
Object.defineProperty(exports, "EnvironmentModel", { enumerable: true, get: function () { return
|
|
18
|
-
Object.defineProperty(exports, "FeatureStateModel", { enumerable: true, get: function () { return
|
|
19
|
-
Object.defineProperty(exports, "IdentityModel", { enumerable: true, get: function () { return
|
|
20
|
-
Object.defineProperty(exports, "TraitModel", { enumerable: true, get: function () { return
|
|
21
|
-
Object.defineProperty(exports, "SegmentModel", { enumerable: true, get: function () { return
|
|
22
|
-
Object.defineProperty(exports, "OrganisationModel", { enumerable: true, get: function () { return
|
|
23
|
-
module.exports =
|
|
7
|
+
const index_js_1 = __importDefault(require("./sdk/index.js"));
|
|
8
|
+
var index_js_2 = require("./sdk/index.js");
|
|
9
|
+
Object.defineProperty(exports, "AnalyticsProcessor", { enumerable: true, get: function () { return index_js_2.AnalyticsProcessor; } });
|
|
10
|
+
Object.defineProperty(exports, "FlagsmithAPIError", { enumerable: true, get: function () { return index_js_2.FlagsmithAPIError; } });
|
|
11
|
+
Object.defineProperty(exports, "FlagsmithClientError", { enumerable: true, get: function () { return index_js_2.FlagsmithClientError; } });
|
|
12
|
+
Object.defineProperty(exports, "EnvironmentDataPollingManager", { enumerable: true, get: function () { return index_js_2.EnvironmentDataPollingManager; } });
|
|
13
|
+
Object.defineProperty(exports, "DefaultFlag", { enumerable: true, get: function () { return index_js_2.DefaultFlag; } });
|
|
14
|
+
Object.defineProperty(exports, "Flags", { enumerable: true, get: function () { return index_js_2.Flags; } });
|
|
15
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(index_js_2).default; } });
|
|
16
|
+
var index_js_3 = require("./flagsmith-engine/index.js");
|
|
17
|
+
Object.defineProperty(exports, "EnvironmentModel", { enumerable: true, get: function () { return index_js_3.EnvironmentModel; } });
|
|
18
|
+
Object.defineProperty(exports, "FeatureStateModel", { enumerable: true, get: function () { return index_js_3.FeatureStateModel; } });
|
|
19
|
+
Object.defineProperty(exports, "IdentityModel", { enumerable: true, get: function () { return index_js_3.IdentityModel; } });
|
|
20
|
+
Object.defineProperty(exports, "TraitModel", { enumerable: true, get: function () { return index_js_3.TraitModel; } });
|
|
21
|
+
Object.defineProperty(exports, "SegmentModel", { enumerable: true, get: function () { return index_js_3.SegmentModel; } });
|
|
22
|
+
Object.defineProperty(exports, "OrganisationModel", { enumerable: true, get: function () { return index_js_3.OrganisationModel; } });
|
|
23
|
+
module.exports = index_js_1.default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Logger } from 'pino';
|
|
2
|
+
import { Fetch } from "./types.js";
|
|
2
3
|
export declare class AnalyticsProcessor {
|
|
3
4
|
private analyticsEndpoint;
|
|
4
5
|
private environmentKey;
|
|
@@ -9,6 +10,7 @@ export declare class AnalyticsProcessor {
|
|
|
9
10
|
private requestTimeoutMs;
|
|
10
11
|
private logger;
|
|
11
12
|
private currentFlush;
|
|
13
|
+
private customFetch;
|
|
12
14
|
/**
|
|
13
15
|
* AnalyticsProcessor is used to track how often individual Flags are evaluated within
|
|
14
16
|
* the Flagsmith SDK. Docs: https://docs.flagsmith.com/advanced-use/flag-analytics.
|
|
@@ -23,6 +25,7 @@ export declare class AnalyticsProcessor {
|
|
|
23
25
|
baseApiUrl: string;
|
|
24
26
|
requestTimeoutMs?: number;
|
|
25
27
|
logger?: Logger;
|
|
28
|
+
fetch?: Fetch;
|
|
26
29
|
});
|
|
27
30
|
/**
|
|
28
31
|
* Sends all the collected data to the api asynchronously and resets the timer
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnalyticsProcessor = void 0;
|
|
4
|
+
const pino_1 = require("pino");
|
|
5
|
+
const ANALYTICS_ENDPOINT = 'analytics/flags/';
|
|
6
|
+
// Used to control how often we send data(in seconds)
|
|
7
|
+
const ANALYTICS_TIMER = 10;
|
|
8
|
+
class AnalyticsProcessor {
|
|
9
|
+
analyticsEndpoint;
|
|
10
|
+
environmentKey;
|
|
11
|
+
lastFlushed;
|
|
12
|
+
analyticsData;
|
|
13
|
+
requestTimeoutMs = 3000;
|
|
14
|
+
logger;
|
|
15
|
+
currentFlush;
|
|
16
|
+
customFetch;
|
|
17
|
+
/**
|
|
18
|
+
* AnalyticsProcessor is used to track how often individual Flags are evaluated within
|
|
19
|
+
* the Flagsmith SDK. Docs: https://docs.flagsmith.com/advanced-use/flag-analytics.
|
|
20
|
+
*
|
|
21
|
+
* @param data.environmentKey environment key obtained from the Flagsmith UI
|
|
22
|
+
* @param data.baseApiUrl base api url to override when using self hosted version
|
|
23
|
+
* @param data.requestTimeoutMs used to tell requests to stop waiting for a response after a
|
|
24
|
+
given number of milliseconds
|
|
25
|
+
*/
|
|
26
|
+
constructor(data) {
|
|
27
|
+
this.analyticsEndpoint = data.baseApiUrl + ANALYTICS_ENDPOINT;
|
|
28
|
+
this.environmentKey = data.environmentKey;
|
|
29
|
+
this.lastFlushed = Date.now();
|
|
30
|
+
this.analyticsData = {};
|
|
31
|
+
this.requestTimeoutMs = data.requestTimeoutMs || this.requestTimeoutMs;
|
|
32
|
+
this.logger = data.logger || (0, pino_1.pino)();
|
|
33
|
+
this.customFetch = data.fetch ?? fetch;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Sends all the collected data to the api asynchronously and resets the timer
|
|
37
|
+
*/
|
|
38
|
+
async flush() {
|
|
39
|
+
if (this.currentFlush || !Object.keys(this.analyticsData).length) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
this.currentFlush = this.customFetch(this.analyticsEndpoint, {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
body: JSON.stringify(this.analyticsData),
|
|
46
|
+
signal: AbortSignal.timeout(this.requestTimeoutMs),
|
|
47
|
+
headers: {
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
'X-Environment-Key': this.environmentKey
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
await this.currentFlush;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
// We don't want failing to write analytics to cause any exceptions in the main
|
|
56
|
+
// thread so we just swallow them here.
|
|
57
|
+
this.logger.warn('Failed to post analytics to Flagsmith API. Not clearing data, will retry.');
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
this.currentFlush = undefined;
|
|
62
|
+
}
|
|
63
|
+
this.analyticsData = {};
|
|
64
|
+
this.lastFlushed = Date.now();
|
|
65
|
+
}
|
|
66
|
+
trackFeature(featureName) {
|
|
67
|
+
this.analyticsData[featureName] = (this.analyticsData[featureName] || 0) + 1;
|
|
68
|
+
if (Date.now() - this.lastFlushed > ANALYTICS_TIMER * 1000) {
|
|
69
|
+
this.flush();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.AnalyticsProcessor = AnalyticsProcessor;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlagsmithAPIError = exports.FlagsmithClientError = void 0;
|
|
4
|
+
class FlagsmithClientError extends Error {
|
|
5
|
+
}
|
|
6
|
+
exports.FlagsmithClientError = FlagsmithClientError;
|
|
7
|
+
class FlagsmithAPIError extends Error {
|
|
8
|
+
}
|
|
9
|
+
exports.FlagsmithAPIError = FlagsmithAPIError;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { EnvironmentModel } from '../flagsmith-engine/
|
|
3
|
-
import { IdentityModel } from '../flagsmith-engine/
|
|
4
|
-
import { BaseOfflineHandler } from './offline_handlers';
|
|
5
|
-
import { DefaultFlag, Flags } from './models';
|
|
6
|
-
import { EnvironmentDataPollingManager } from './polling_manager';
|
|
7
|
-
import { SegmentModel } from '../flagsmith-engine/
|
|
8
|
-
import { FlagsmithConfig } from './types';
|
|
9
|
-
export { AnalyticsProcessor } from './analytics';
|
|
10
|
-
export { FlagsmithAPIError, FlagsmithClientError } from './errors';
|
|
11
|
-
export { DefaultFlag, Flags } from './models';
|
|
12
|
-
export { EnvironmentDataPollingManager } from './polling_manager';
|
|
13
|
-
export { FlagsmithCache, FlagsmithConfig } from './types';
|
|
1
|
+
import { Dispatcher } from 'undici-types';
|
|
2
|
+
import { EnvironmentModel } from '../flagsmith-engine/index.js';
|
|
3
|
+
import { IdentityModel } from '../flagsmith-engine/index.js';
|
|
4
|
+
import { BaseOfflineHandler } from './offline_handlers.js';
|
|
5
|
+
import { DefaultFlag, Flags } from './models.js';
|
|
6
|
+
import { EnvironmentDataPollingManager } from './polling_manager.js';
|
|
7
|
+
import { SegmentModel } from '../flagsmith-engine/index.js';
|
|
8
|
+
import { FlagsmithConfig, FlagsmithTraitValue, ITraitConfig } from './types.js';
|
|
9
|
+
export { AnalyticsProcessor } from './analytics.js';
|
|
10
|
+
export { FlagsmithAPIError, FlagsmithClientError } from './errors.js';
|
|
11
|
+
export { DefaultFlag, Flags } from './models.js';
|
|
12
|
+
export { EnvironmentDataPollingManager } from './polling_manager.js';
|
|
13
|
+
export { FlagsmithCache, FlagsmithConfig } from './types.js';
|
|
14
14
|
export declare class Flagsmith {
|
|
15
15
|
environmentKey?: string;
|
|
16
16
|
apiUrl?: string;
|
|
17
17
|
customHeaders?: {
|
|
18
18
|
[key: string]: any;
|
|
19
19
|
};
|
|
20
|
-
agent
|
|
20
|
+
agent?: Dispatcher;
|
|
21
21
|
requestTimeoutMs?: number;
|
|
22
22
|
enableLocalEvaluation?: boolean;
|
|
23
23
|
environmentRefreshIntervalSeconds: number;
|
|
@@ -36,6 +36,7 @@ export declare class Flagsmith {
|
|
|
36
36
|
private onEnvironmentChange?;
|
|
37
37
|
private analyticsProcessor?;
|
|
38
38
|
private logger;
|
|
39
|
+
private customFetch;
|
|
39
40
|
/**
|
|
40
41
|
* A Flagsmith client.
|
|
41
42
|
*
|
|
@@ -87,13 +88,13 @@ export declare class Flagsmith {
|
|
|
87
88
|
*
|
|
88
89
|
* @param {string} identifier a unique identifier for the identity in the current
|
|
89
90
|
environment, e.g. email address, username, uuid
|
|
90
|
-
* @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
|
|
91
|
-
Flagsmith, e.g. {"num_orders": 10}
|
|
91
|
+
* @param {{[key:string]:any | ITraitConfig}} traits? a dictionary of traits to add / update on the identity in
|
|
92
|
+
Flagsmith, e.g. {"num_orders": 10} or {age: {value: 30, transient: true}}
|
|
92
93
|
* @returns Flags object holding all the flags for the given identity.
|
|
93
94
|
*/
|
|
94
95
|
getIdentityFlags(identifier: string, traits?: {
|
|
95
|
-
[key: string]:
|
|
96
|
-
}): Promise<Flags>;
|
|
96
|
+
[key: string]: FlagsmithTraitValue | ITraitConfig;
|
|
97
|
+
}, transient?: boolean): Promise<Flags>;
|
|
97
98
|
/**
|
|
98
99
|
* Get the segments for the current environment for a given identity. Will also
|
|
99
100
|
upsert all traits to the Flagsmith API for future evaluations. Providing a
|