flagsmith-nodejs 6.1.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/conventional-commit.yml +29 -0
- package/.github/workflows/publish.yml +20 -17
- package/.github/workflows/pull_request.yaml +36 -33
- package/.github/workflows/release-please.yml +18 -0
- package/.gitmodules +1 -0
- package/.husky/pre-commit +1 -0
- package/.nvmrc +1 -0
- package/.prettierrc.cjs +9 -1
- package/.release-please-manifest.json +1 -0
- package/CHANGELOG.md +592 -0
- package/CODEOWNERS +1 -0
- package/README.md +0 -2
- 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/analytics.js +3 -1
- package/build/cjs/sdk/index.d.ts +1 -3
- package/build/cjs/sdk/index.js +63 -24
- package/build/cjs/sdk/models.d.ts +8 -1
- package/build/cjs/sdk/models.js +29 -1
- package/build/cjs/sdk/utils.d.ts +1 -0
- package/build/cjs/sdk/utils.js +14 -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/analytics.js +3 -1
- package/build/esm/sdk/index.d.ts +1 -3
- package/build/esm/sdk/index.js +63 -24
- package/build/esm/sdk/models.d.ts +8 -1
- package/build/esm/sdk/models.js +29 -1
- package/build/esm/sdk/utils.d.ts +1 -0
- package/build/esm/sdk/utils.js +12 -0
- 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/release-please-config.json +62 -0
- package/sdk/analytics.ts +3 -1
- package/sdk/index.ts +89 -30
- package/sdk/models.ts +44 -2
- package/sdk/utils.ts +13 -0
- package/tests/engine/e2e/engine.test.ts +43 -38
- package/tests/engine/unit/engine.test.ts +306 -60
- 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/analytics.test.ts +6 -1
- package/tests/sdk/data/environment.json +1 -0
- package/tests/sdk/flagsmith-environment-flags.test.ts +28 -0
- package/tests/sdk/flagsmith-identity-flags.test.ts +11 -2
- package/tests/sdk/flagsmith.test.ts +190 -3
- package/tests/sdk/offline-handlers.test.ts +3 -1
- package/vitest.config.esm.ts +34 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* This file was automatically generated by json-schema-to-typescript.
|
|
5
|
+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
|
|
6
|
+
* and run json-schema-to-typescript to regenerate this file.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { GenericEvaluationContext } from '../models.js';
|
|
2
|
+
import { EnvironmentModel } from '../../environments/models.js';
|
|
3
|
+
import { IdentityModel } from '../../identities/models.js';
|
|
4
|
+
import { TraitModel } from '../../identities/traits/models.js';
|
|
5
|
+
export declare function getEvaluationContext(environment: EnvironmentModel, identity?: IdentityModel, overrideTraits?: TraitModel[], isEnvironmentEvaluation?: boolean): GenericEvaluationContext;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEvaluationContext = void 0;
|
|
4
|
+
const models_js_1 = require("../models.js");
|
|
5
|
+
const constants_js_1 = require("../../segments/constants.js");
|
|
6
|
+
const node_crypto_1 = require("node:crypto");
|
|
7
|
+
const util_js_1 = require("../../features/util.js");
|
|
8
|
+
function getEvaluationContext(environment, identity, overrideTraits, isEnvironmentEvaluation = false) {
|
|
9
|
+
const environmentContext = mapEnvironmentModelToEvaluationContext(environment);
|
|
10
|
+
if (isEnvironmentEvaluation) {
|
|
11
|
+
return environmentContext;
|
|
12
|
+
}
|
|
13
|
+
const identityContext = identity
|
|
14
|
+
? mapIdentityModelToIdentityContext(identity, overrideTraits)
|
|
15
|
+
: undefined;
|
|
16
|
+
const context = {
|
|
17
|
+
...environmentContext,
|
|
18
|
+
...(identityContext && { identity: identityContext })
|
|
19
|
+
};
|
|
20
|
+
return context;
|
|
21
|
+
}
|
|
22
|
+
exports.getEvaluationContext = getEvaluationContext;
|
|
23
|
+
function mapEnvironmentModelToEvaluationContext(environment) {
|
|
24
|
+
const environmentContext = {
|
|
25
|
+
key: environment.apiKey,
|
|
26
|
+
name: environment.name
|
|
27
|
+
};
|
|
28
|
+
const features = {};
|
|
29
|
+
for (const fs of environment.featureStates) {
|
|
30
|
+
const variants = fs.multivariateFeatureStateValues?.length > 0
|
|
31
|
+
? fs.multivariateFeatureStateValues.map(mv => ({
|
|
32
|
+
value: mv.multivariateFeatureOption.value,
|
|
33
|
+
weight: mv.percentageAllocation,
|
|
34
|
+
priority: mv.id ?? (0, util_js_1.uuidToBigInt)(mv.mvFsValueUuid)
|
|
35
|
+
}))
|
|
36
|
+
: undefined;
|
|
37
|
+
features[fs.feature.name] = {
|
|
38
|
+
key: fs.djangoID?.toString() || fs.featurestateUUID,
|
|
39
|
+
name: fs.feature.name,
|
|
40
|
+
enabled: fs.enabled,
|
|
41
|
+
value: fs.getValue(),
|
|
42
|
+
variants,
|
|
43
|
+
priority: fs.featureSegment?.priority,
|
|
44
|
+
metadata: {
|
|
45
|
+
id: fs.feature.id
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const segmentOverrides = {};
|
|
50
|
+
for (const segment of environment.project.segments) {
|
|
51
|
+
segmentOverrides[segment.id.toString()] = {
|
|
52
|
+
key: segment.id.toString(),
|
|
53
|
+
name: segment.name,
|
|
54
|
+
rules: segment.rules.map(rule => mapSegmentRuleModelToRule(rule)),
|
|
55
|
+
overrides: segment.featureStates.length > 0
|
|
56
|
+
? segment.featureStates.map(fs => ({
|
|
57
|
+
key: fs.djangoID?.toString() || fs.featurestateUUID,
|
|
58
|
+
name: fs.feature.name,
|
|
59
|
+
enabled: fs.enabled,
|
|
60
|
+
value: fs.getValue(),
|
|
61
|
+
priority: fs.featureSegment?.priority,
|
|
62
|
+
metadata: {
|
|
63
|
+
id: fs.feature.id
|
|
64
|
+
}
|
|
65
|
+
}))
|
|
66
|
+
: [],
|
|
67
|
+
metadata: {
|
|
68
|
+
source: models_js_1.SegmentSource.API,
|
|
69
|
+
id: segment.id
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
let identityOverrideSegments = {};
|
|
74
|
+
if (environment.identityOverrides && environment.identityOverrides.length > 0) {
|
|
75
|
+
identityOverrideSegments = mapIdentityOverridesToSegments(environment.identityOverrides);
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
environment: environmentContext,
|
|
79
|
+
features,
|
|
80
|
+
segments: {
|
|
81
|
+
...segmentOverrides,
|
|
82
|
+
...identityOverrideSegments
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function mapIdentityModelToIdentityContext(identity, overrideTraits) {
|
|
87
|
+
const traits = overrideTraits || identity.identityTraits;
|
|
88
|
+
const traitsContext = {};
|
|
89
|
+
for (const trait of traits) {
|
|
90
|
+
traitsContext[trait.traitKey] = trait.traitValue;
|
|
91
|
+
}
|
|
92
|
+
const identityContext = {
|
|
93
|
+
identifier: identity.identifier,
|
|
94
|
+
traits: traitsContext
|
|
95
|
+
};
|
|
96
|
+
return identityContext;
|
|
97
|
+
}
|
|
98
|
+
function mapSegmentRuleModelToRule(rule) {
|
|
99
|
+
return {
|
|
100
|
+
type: rule.type,
|
|
101
|
+
conditions: rule.conditions.map((condition) => ({
|
|
102
|
+
property: condition.property,
|
|
103
|
+
operator: condition.operator,
|
|
104
|
+
value: condition.value
|
|
105
|
+
})),
|
|
106
|
+
rules: rule.rules.map((subRule) => mapSegmentRuleModelToRule(subRule))
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function mapIdentityOverridesToSegments(identityOverrides) {
|
|
110
|
+
const segments = {};
|
|
111
|
+
const featuresToIdentifiers = new Map();
|
|
112
|
+
for (const identity of identityOverrides) {
|
|
113
|
+
if (!identity.identityFeatures || identity.identityFeatures.length === 0) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const sortedFeatures = [...identity.identityFeatures].sort((a, b) => a.feature.name.localeCompare(b.feature.name));
|
|
117
|
+
const overridesKey = sortedFeatures.map(fs => ({
|
|
118
|
+
name: fs.feature.name,
|
|
119
|
+
enabled: fs.enabled,
|
|
120
|
+
value: fs.getValue(),
|
|
121
|
+
priority: -Infinity,
|
|
122
|
+
metadata: {
|
|
123
|
+
id: fs.feature.id
|
|
124
|
+
}
|
|
125
|
+
}));
|
|
126
|
+
const overridesHash = (0, node_crypto_1.createHash)('sha1').update(JSON.stringify(overridesKey)).digest('hex');
|
|
127
|
+
if (!featuresToIdentifiers.has(overridesHash)) {
|
|
128
|
+
featuresToIdentifiers.set(overridesHash, { identifiers: [], overrides: overridesKey });
|
|
129
|
+
}
|
|
130
|
+
featuresToIdentifiers.get(overridesHash).identifiers.push(identity.identifier);
|
|
131
|
+
}
|
|
132
|
+
for (const [overrideHash, { identifiers, overrides }] of featuresToIdentifiers.entries()) {
|
|
133
|
+
const segmentKey = `identity_override_${overrideHash}`;
|
|
134
|
+
segments[segmentKey] = {
|
|
135
|
+
key: segmentKey,
|
|
136
|
+
name: constants_js_1.IDENTITY_OVERRIDE_SEGMENT_NAME,
|
|
137
|
+
rules: [
|
|
138
|
+
{
|
|
139
|
+
type: 'ALL',
|
|
140
|
+
conditions: [
|
|
141
|
+
{
|
|
142
|
+
property: '$.identity.identifier',
|
|
143
|
+
operator: 'IN',
|
|
144
|
+
value: identifiers.join(',')
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
}
|
|
148
|
+
],
|
|
149
|
+
metadata: {
|
|
150
|
+
source: models_js_1.SegmentSource.IDENTITY_OVERRIDE
|
|
151
|
+
},
|
|
152
|
+
overrides: overrides
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return segments;
|
|
156
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
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
|
+
* An environment's unique identifier.
|
|
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, used for segment and multivariate feature flag targeting, and 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
|
+
* 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
|
+
* Key used when selecting a value for a multivariate feature. Set to an internal identifier or a UUID, depending on Flagsmith implementation.
|
|
73
|
+
*/
|
|
74
|
+
export type Key3 = string;
|
|
75
|
+
/**
|
|
76
|
+
* Unique feature identifier.
|
|
77
|
+
*/
|
|
78
|
+
export type FeatureKey = string;
|
|
79
|
+
/**
|
|
80
|
+
* Feature name.
|
|
81
|
+
*/
|
|
82
|
+
export type Name2 = string;
|
|
83
|
+
/**
|
|
84
|
+
* Indicates whether the feature is enabled in the environment.
|
|
85
|
+
*/
|
|
86
|
+
export type Enabled = boolean;
|
|
87
|
+
/**
|
|
88
|
+
* A default environment value for the feature. If the feature is multivariate, this will be the control value.
|
|
89
|
+
*/
|
|
90
|
+
export type Value2 = string;
|
|
91
|
+
/**
|
|
92
|
+
* The value of the feature.
|
|
93
|
+
*/
|
|
94
|
+
export type Value3 = string;
|
|
95
|
+
/**
|
|
96
|
+
* The weight of the feature value variant, as a percentage number (i.e. 100.0).
|
|
97
|
+
*/
|
|
98
|
+
export type Weight = number;
|
|
99
|
+
/**
|
|
100
|
+
* An array of environment default values associated with the feature. Contains a single value for standard features, or 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 Priority = 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
|
+
[k: string]: unknown;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Represents a rule within a segment for feature flag evaluation.
|
|
165
|
+
*/
|
|
166
|
+
export interface SegmentRule {
|
|
167
|
+
type: Type;
|
|
168
|
+
conditions?: Conditions;
|
|
169
|
+
rules?: SubRules;
|
|
170
|
+
[k: string]: unknown;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Represents a condition within a segment rule for feature flag evaluation.
|
|
174
|
+
*/
|
|
175
|
+
export interface SegmentCondition1 {
|
|
176
|
+
property: Property;
|
|
177
|
+
operator: Operator;
|
|
178
|
+
value: Value;
|
|
179
|
+
[k: string]: unknown;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Represents an IN condition within a segment rule for feature flag evaluation.
|
|
183
|
+
*/
|
|
184
|
+
export interface InSegmentCondition {
|
|
185
|
+
property: Property1;
|
|
186
|
+
operator: Operator1;
|
|
187
|
+
value: Value1;
|
|
188
|
+
[k: string]: unknown;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Represents a feature context for feature flag evaluation.
|
|
192
|
+
*/
|
|
193
|
+
export interface FeatureContext {
|
|
194
|
+
key: Key3;
|
|
195
|
+
feature_key: FeatureKey;
|
|
196
|
+
name: Name2;
|
|
197
|
+
enabled: Enabled;
|
|
198
|
+
value: Value2;
|
|
199
|
+
variants?: Variants;
|
|
200
|
+
priority?: Priority;
|
|
201
|
+
[k: string]: unknown;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Represents a multivariate value for a feature flag.
|
|
205
|
+
*/
|
|
206
|
+
export interface FeatureValue {
|
|
207
|
+
value: Value3;
|
|
208
|
+
weight: Weight;
|
|
209
|
+
[k: string]: unknown;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Features to be evaluated in the context.
|
|
213
|
+
*/
|
|
214
|
+
export interface Features {
|
|
215
|
+
[k: string]: FeatureContext;
|
|
216
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* This file was automatically generated by json-schema-to-typescript.
|
|
5
|
+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
|
|
6
|
+
* and run json-schema-to-typescript to regenerate this file.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
* Feature name.
|
|
8
|
+
*/
|
|
9
|
+
export type Name = string;
|
|
10
|
+
/**
|
|
11
|
+
* Indicates if the feature flag is enabled.
|
|
12
|
+
*/
|
|
13
|
+
export type Enabled = boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Feature flag value.
|
|
16
|
+
*/
|
|
17
|
+
export type Value = string | number | boolean | null;
|
|
18
|
+
/**
|
|
19
|
+
* Reason for the feature flag evaluation.
|
|
20
|
+
*/
|
|
21
|
+
export type Reason = string;
|
|
22
|
+
/**
|
|
23
|
+
* Segment name.
|
|
24
|
+
*/
|
|
25
|
+
export type Name1 = string;
|
|
26
|
+
/**
|
|
27
|
+
* List of segments which the provided context belongs to.
|
|
28
|
+
*/
|
|
29
|
+
export type Segments = SegmentResult[];
|
|
30
|
+
/**
|
|
31
|
+
* Evaluation result object containing the used context, flag evaluation results, and segments used in the evaluation.
|
|
32
|
+
*/
|
|
33
|
+
export interface EvaluationResult {
|
|
34
|
+
flags: Flags;
|
|
35
|
+
segments: Segments;
|
|
36
|
+
[k: string]: unknown;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Feature flags evaluated for the context, mapped by feature names.
|
|
40
|
+
*/
|
|
41
|
+
export interface Flags {
|
|
42
|
+
[k: string]: FlagResult;
|
|
43
|
+
}
|
|
44
|
+
export interface FlagResult {
|
|
45
|
+
name: Name;
|
|
46
|
+
enabled: Enabled;
|
|
47
|
+
value: Value;
|
|
48
|
+
reason: Reason;
|
|
49
|
+
metadata?: FeatureMetadata;
|
|
50
|
+
[k: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Additional metadata associated with the feature.
|
|
54
|
+
*/
|
|
55
|
+
export interface FeatureMetadata {
|
|
56
|
+
[k: string]: unknown;
|
|
57
|
+
}
|
|
58
|
+
export interface SegmentResult {
|
|
59
|
+
name: Name1;
|
|
60
|
+
metadata?: SegmentMetadata;
|
|
61
|
+
[k: string]: unknown;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Additional metadata associated with the segment.
|
|
65
|
+
*/
|
|
66
|
+
export interface SegmentMetadata {
|
|
67
|
+
[k: string]: unknown;
|
|
68
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* This file was automatically generated by json-schema-to-typescript.
|
|
5
|
+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
|
|
6
|
+
* and run json-schema-to-typescript to regenerate this file.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|