flagsmith-nodejs 6.2.0 → 7.0.3
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 +49 -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
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FeaturesWithMetadata,
|
|
3
|
+
Traits,
|
|
4
|
+
GenericEvaluationContext,
|
|
5
|
+
EnvironmentContext,
|
|
6
|
+
IdentityContext,
|
|
7
|
+
SegmentSource,
|
|
8
|
+
SDKFeatureMetadata,
|
|
9
|
+
SegmentsWithMetadata,
|
|
10
|
+
SDKSegmentMetadata
|
|
11
|
+
} from '../models.js';
|
|
12
|
+
import { EnvironmentModel } from '../../environments/models.js';
|
|
13
|
+
import { IdentityModel } from '../../identities/models.js';
|
|
14
|
+
import { TraitModel } from '../../identities/traits/models.js';
|
|
15
|
+
import { IDENTITY_OVERRIDE_SEGMENT_NAME } from '../../segments/constants.js';
|
|
16
|
+
import { createHash } from 'node:crypto';
|
|
17
|
+
import { uuidToBigInt } from '../../features/util.js';
|
|
18
|
+
|
|
19
|
+
export function getEvaluationContext(
|
|
20
|
+
environment: EnvironmentModel,
|
|
21
|
+
identity?: IdentityModel,
|
|
22
|
+
overrideTraits?: TraitModel[],
|
|
23
|
+
isEnvironmentEvaluation: boolean = false
|
|
24
|
+
): GenericEvaluationContext {
|
|
25
|
+
const environmentContext = mapEnvironmentModelToEvaluationContext(environment);
|
|
26
|
+
if (isEnvironmentEvaluation) {
|
|
27
|
+
return environmentContext;
|
|
28
|
+
}
|
|
29
|
+
const identityContext = identity
|
|
30
|
+
? mapIdentityModelToIdentityContext(identity, overrideTraits)
|
|
31
|
+
: undefined;
|
|
32
|
+
|
|
33
|
+
const context = {
|
|
34
|
+
...environmentContext,
|
|
35
|
+
...(identityContext && { identity: identityContext })
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
return context;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function mapEnvironmentModelToEvaluationContext(
|
|
42
|
+
environment: EnvironmentModel
|
|
43
|
+
): GenericEvaluationContext {
|
|
44
|
+
const environmentContext: EnvironmentContext = {
|
|
45
|
+
key: environment.apiKey,
|
|
46
|
+
name: environment.name
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const features: FeaturesWithMetadata<SDKFeatureMetadata> = {};
|
|
50
|
+
for (const fs of environment.featureStates) {
|
|
51
|
+
const variants =
|
|
52
|
+
fs.multivariateFeatureStateValues?.length > 0
|
|
53
|
+
? fs.multivariateFeatureStateValues.map(mv => ({
|
|
54
|
+
value: mv.multivariateFeatureOption.value,
|
|
55
|
+
weight: mv.percentageAllocation,
|
|
56
|
+
priority: mv.id ?? uuidToBigInt(mv.mvFsValueUuid)
|
|
57
|
+
}))
|
|
58
|
+
: undefined;
|
|
59
|
+
|
|
60
|
+
features[fs.feature.name] = {
|
|
61
|
+
key: fs.djangoID?.toString() || fs.featurestateUUID,
|
|
62
|
+
name: fs.feature.name,
|
|
63
|
+
enabled: fs.enabled,
|
|
64
|
+
value: fs.getValue(),
|
|
65
|
+
variants,
|
|
66
|
+
priority: fs.featureSegment?.priority,
|
|
67
|
+
metadata: {
|
|
68
|
+
id: fs.feature.id
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const segmentOverrides: SegmentsWithMetadata<SDKSegmentMetadata> = {};
|
|
74
|
+
for (const segment of environment.project.segments) {
|
|
75
|
+
segmentOverrides[segment.id.toString()] = {
|
|
76
|
+
key: segment.id.toString(),
|
|
77
|
+
name: segment.name,
|
|
78
|
+
rules: segment.rules.map(rule => mapSegmentRuleModelToRule(rule)),
|
|
79
|
+
overrides:
|
|
80
|
+
segment.featureStates.length > 0
|
|
81
|
+
? segment.featureStates.map(fs => ({
|
|
82
|
+
key: fs.djangoID?.toString() || fs.featurestateUUID,
|
|
83
|
+
name: fs.feature.name,
|
|
84
|
+
enabled: fs.enabled,
|
|
85
|
+
value: fs.getValue(),
|
|
86
|
+
priority: fs.featureSegment?.priority,
|
|
87
|
+
metadata: {
|
|
88
|
+
id: fs.feature.id
|
|
89
|
+
}
|
|
90
|
+
}))
|
|
91
|
+
: [],
|
|
92
|
+
metadata: {
|
|
93
|
+
source: SegmentSource.API,
|
|
94
|
+
id: segment.id
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let identityOverrideSegments: SegmentsWithMetadata<SDKSegmentMetadata> = {};
|
|
100
|
+
if (environment.identityOverrides && environment.identityOverrides.length > 0) {
|
|
101
|
+
identityOverrideSegments = mapIdentityOverridesToSegments(environment.identityOverrides);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
environment: environmentContext,
|
|
106
|
+
features,
|
|
107
|
+
segments: {
|
|
108
|
+
...segmentOverrides,
|
|
109
|
+
...identityOverrideSegments
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function mapIdentityModelToIdentityContext(
|
|
115
|
+
identity: IdentityModel,
|
|
116
|
+
overrideTraits?: TraitModel[]
|
|
117
|
+
): IdentityContext {
|
|
118
|
+
const traits = overrideTraits || identity.identityTraits;
|
|
119
|
+
const traitsContext: Traits = {};
|
|
120
|
+
|
|
121
|
+
for (const trait of traits) {
|
|
122
|
+
traitsContext[trait.traitKey] = trait.traitValue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const identityContext: IdentityContext = {
|
|
126
|
+
identifier: identity.identifier,
|
|
127
|
+
traits: traitsContext
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
return identityContext;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function mapSegmentRuleModelToRule(rule: any): any {
|
|
134
|
+
return {
|
|
135
|
+
type: rule.type,
|
|
136
|
+
conditions: rule.conditions.map((condition: any) => ({
|
|
137
|
+
property: condition.property,
|
|
138
|
+
operator: condition.operator,
|
|
139
|
+
value: condition.value
|
|
140
|
+
})),
|
|
141
|
+
rules: rule.rules.map((subRule: any) => mapSegmentRuleModelToRule(subRule))
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function mapIdentityOverridesToSegments(
|
|
146
|
+
identityOverrides: IdentityModel[]
|
|
147
|
+
): SegmentsWithMetadata<SDKSegmentMetadata> {
|
|
148
|
+
const segments: SegmentsWithMetadata<SDKSegmentMetadata> = {};
|
|
149
|
+
const featuresToIdentifiers = new Map<string, { identifiers: string[]; overrides: any[] }>();
|
|
150
|
+
|
|
151
|
+
for (const identity of identityOverrides) {
|
|
152
|
+
if (!identity.identityFeatures || identity.identityFeatures.length === 0) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const sortedFeatures = [...identity.identityFeatures].sort((a, b) =>
|
|
157
|
+
a.feature.name.localeCompare(b.feature.name)
|
|
158
|
+
);
|
|
159
|
+
const overridesKey = sortedFeatures.map(fs => ({
|
|
160
|
+
name: fs.feature.name,
|
|
161
|
+
enabled: fs.enabled,
|
|
162
|
+
value: fs.getValue(),
|
|
163
|
+
priority: -Infinity,
|
|
164
|
+
metadata: {
|
|
165
|
+
id: fs.feature.id
|
|
166
|
+
}
|
|
167
|
+
}));
|
|
168
|
+
|
|
169
|
+
const overridesHash = createHash('sha1').update(JSON.stringify(overridesKey)).digest('hex');
|
|
170
|
+
|
|
171
|
+
if (!featuresToIdentifiers.has(overridesHash)) {
|
|
172
|
+
featuresToIdentifiers.set(overridesHash, { identifiers: [], overrides: overridesKey });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
featuresToIdentifiers.get(overridesHash)!.identifiers.push(identity.identifier);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
for (const [overrideHash, { identifiers, overrides }] of featuresToIdentifiers.entries()) {
|
|
179
|
+
const segmentKey = `identity_override_${overrideHash}`;
|
|
180
|
+
|
|
181
|
+
segments[segmentKey] = {
|
|
182
|
+
key: segmentKey,
|
|
183
|
+
name: IDENTITY_OVERRIDE_SEGMENT_NAME,
|
|
184
|
+
rules: [
|
|
185
|
+
{
|
|
186
|
+
type: 'ALL',
|
|
187
|
+
conditions: [
|
|
188
|
+
{
|
|
189
|
+
property: '$.identity.identifier',
|
|
190
|
+
operator: 'IN',
|
|
191
|
+
value: identifiers.join(',')
|
|
192
|
+
}
|
|
193
|
+
]
|
|
194
|
+
}
|
|
195
|
+
],
|
|
196
|
+
metadata: {
|
|
197
|
+
source: SegmentSource.IDENTITY_OVERRIDE
|
|
198
|
+
},
|
|
199
|
+
overrides: overrides
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return segments;
|
|
204
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* This file was automatically generated by json-schema-to-typescript.
|
|
4
|
+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
|
|
5
|
+
* and run json-schema-to-typescript to regenerate this file.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* An environment's unique identifier.
|
|
10
|
+
*/
|
|
11
|
+
export type Key = string;
|
|
12
|
+
/**
|
|
13
|
+
* An environment's human-readable name.
|
|
14
|
+
*/
|
|
15
|
+
export type Name = string;
|
|
16
|
+
/**
|
|
17
|
+
* A unique identifier for an identity, used for segment and multivariate feature flag targeting, and displayed in the Flagsmith UI.
|
|
18
|
+
*/
|
|
19
|
+
export type Identifier = string;
|
|
20
|
+
/**
|
|
21
|
+
* 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.
|
|
22
|
+
*/
|
|
23
|
+
export type Key1 = string;
|
|
24
|
+
/**
|
|
25
|
+
* Key used for % split segmentation.
|
|
26
|
+
*/
|
|
27
|
+
export type Key2 = string;
|
|
28
|
+
/**
|
|
29
|
+
* The name of the segment.
|
|
30
|
+
*/
|
|
31
|
+
export type Name1 = string;
|
|
32
|
+
/**
|
|
33
|
+
* Segment rule type. Represents a logical quantifier for the conditions and sub-rules.
|
|
34
|
+
*/
|
|
35
|
+
export type Type = 'ALL' | 'ANY' | 'NONE';
|
|
36
|
+
export type SegmentCondition = SegmentCondition1 | InSegmentCondition;
|
|
37
|
+
/**
|
|
38
|
+
* A reference to the identity trait or value in the evaluation context.
|
|
39
|
+
*/
|
|
40
|
+
export type Property = string;
|
|
41
|
+
/**
|
|
42
|
+
* The operator to use for evaluating the condition.
|
|
43
|
+
*/
|
|
44
|
+
export type Operator =
|
|
45
|
+
| 'EQUAL'
|
|
46
|
+
| 'GREATER_THAN'
|
|
47
|
+
| 'LESS_THAN'
|
|
48
|
+
| 'LESS_THAN_INCLUSIVE'
|
|
49
|
+
| 'CONTAINS'
|
|
50
|
+
| 'GREATER_THAN_INCLUSIVE'
|
|
51
|
+
| 'NOT_CONTAINS'
|
|
52
|
+
| 'NOT_EQUAL'
|
|
53
|
+
| 'REGEX'
|
|
54
|
+
| 'PERCENTAGE_SPLIT'
|
|
55
|
+
| 'MODULO'
|
|
56
|
+
| 'IS_SET'
|
|
57
|
+
| 'IS_NOT_SET'
|
|
58
|
+
| 'IN';
|
|
59
|
+
/**
|
|
60
|
+
* The value to compare against the trait or context value.
|
|
61
|
+
*/
|
|
62
|
+
export type Value = string;
|
|
63
|
+
/**
|
|
64
|
+
* A reference to the identity trait or value in the evaluation context.
|
|
65
|
+
*/
|
|
66
|
+
export type Property1 = string;
|
|
67
|
+
/**
|
|
68
|
+
* The operator to use for evaluating the condition.
|
|
69
|
+
*/
|
|
70
|
+
export type Operator1 = 'IN';
|
|
71
|
+
/**
|
|
72
|
+
* The values to compare against the trait or context value.
|
|
73
|
+
*/
|
|
74
|
+
export type Value1 = string[];
|
|
75
|
+
/**
|
|
76
|
+
* Conditions that must be met for the rule to apply.
|
|
77
|
+
*/
|
|
78
|
+
export type Conditions = SegmentCondition[];
|
|
79
|
+
/**
|
|
80
|
+
* Sub-rules nested within the segment rule.
|
|
81
|
+
*/
|
|
82
|
+
export type SubRules = SegmentRule[];
|
|
83
|
+
/**
|
|
84
|
+
* Rules that define the segment.
|
|
85
|
+
*/
|
|
86
|
+
export type Rules = SegmentRule[];
|
|
87
|
+
/**
|
|
88
|
+
* Key used when selecting a value for a multivariate feature. Set to an internal identifier or a UUID, depending on Flagsmith implementation.
|
|
89
|
+
*/
|
|
90
|
+
export type Key3 = string;
|
|
91
|
+
/**
|
|
92
|
+
* Unique feature identifier.
|
|
93
|
+
*/
|
|
94
|
+
export type FeatureKey = string;
|
|
95
|
+
/**
|
|
96
|
+
* Feature name.
|
|
97
|
+
*/
|
|
98
|
+
export type Name2 = string;
|
|
99
|
+
/**
|
|
100
|
+
* Indicates whether the feature is enabled in the environment.
|
|
101
|
+
*/
|
|
102
|
+
export type Enabled = boolean;
|
|
103
|
+
/**
|
|
104
|
+
* A default environment value for the feature. If the feature is multivariate, this will be the control value.
|
|
105
|
+
*/
|
|
106
|
+
export type Value2 = string;
|
|
107
|
+
/**
|
|
108
|
+
* The value of the feature.
|
|
109
|
+
*/
|
|
110
|
+
export type Value3 = string;
|
|
111
|
+
/**
|
|
112
|
+
* The weight of the feature value variant, as a percentage number (i.e. 100.0).
|
|
113
|
+
*/
|
|
114
|
+
export type Weight = number;
|
|
115
|
+
/**
|
|
116
|
+
* An array of environment default values associated with the feature. Contains a single value for standard features, or multiple values for multivariate features.
|
|
117
|
+
*/
|
|
118
|
+
export type Variants = FeatureValue[];
|
|
119
|
+
/**
|
|
120
|
+
* Priority of the feature context. Lower values indicate a higher priority when multiple contexts apply to the same feature.
|
|
121
|
+
*/
|
|
122
|
+
export type Priority = number;
|
|
123
|
+
/**
|
|
124
|
+
* Feature overrides for the segment.
|
|
125
|
+
*/
|
|
126
|
+
export type Overrides = FeatureContext[];
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* A context object containing the necessary information to evaluate Flagsmith feature flags.
|
|
130
|
+
*/
|
|
131
|
+
export interface EvaluationContext {
|
|
132
|
+
environment: EnvironmentContext;
|
|
133
|
+
/**
|
|
134
|
+
* Identity context used for identity-based evaluation.
|
|
135
|
+
*/
|
|
136
|
+
identity?: IdentityContext | null;
|
|
137
|
+
segments?: Segments;
|
|
138
|
+
features?: Features;
|
|
139
|
+
[k: string]: unknown;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Environment context required for evaluation.
|
|
143
|
+
*/
|
|
144
|
+
export interface EnvironmentContext {
|
|
145
|
+
key: Key;
|
|
146
|
+
name: Name;
|
|
147
|
+
[k: string]: unknown;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Represents an identity context for feature flag evaluation.
|
|
151
|
+
*/
|
|
152
|
+
export interface IdentityContext {
|
|
153
|
+
identifier: Identifier;
|
|
154
|
+
key: Key1;
|
|
155
|
+
traits?: Traits;
|
|
156
|
+
[k: string]: unknown;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* A map of traits associated with the identity, where the key is the trait name and the value is the trait value.
|
|
160
|
+
*/
|
|
161
|
+
export interface Traits {
|
|
162
|
+
[k: string]: string | number | boolean | null;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Segments applicable to the evaluation context.
|
|
166
|
+
*/
|
|
167
|
+
export interface Segments {
|
|
168
|
+
[k: string]: SegmentContext;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Represents a segment context for feature flag evaluation.
|
|
172
|
+
*/
|
|
173
|
+
export interface SegmentContext {
|
|
174
|
+
key: Key2;
|
|
175
|
+
name: Name1;
|
|
176
|
+
rules: Rules;
|
|
177
|
+
overrides?: Overrides;
|
|
178
|
+
[k: string]: unknown;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Represents a rule within a segment for feature flag evaluation.
|
|
182
|
+
*/
|
|
183
|
+
export interface SegmentRule {
|
|
184
|
+
type: Type;
|
|
185
|
+
conditions?: Conditions;
|
|
186
|
+
rules?: SubRules;
|
|
187
|
+
[k: string]: unknown;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Represents a condition within a segment rule for feature flag evaluation.
|
|
191
|
+
*/
|
|
192
|
+
export interface SegmentCondition1 {
|
|
193
|
+
property: Property;
|
|
194
|
+
operator: Operator;
|
|
195
|
+
value: Value;
|
|
196
|
+
[k: string]: unknown;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Represents an IN condition within a segment rule for feature flag evaluation.
|
|
200
|
+
*/
|
|
201
|
+
export interface InSegmentCondition {
|
|
202
|
+
property: Property1;
|
|
203
|
+
operator: Operator1;
|
|
204
|
+
value: Value1;
|
|
205
|
+
[k: string]: unknown;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Represents a feature context for feature flag evaluation.
|
|
209
|
+
*/
|
|
210
|
+
export interface FeatureContext {
|
|
211
|
+
key: Key3;
|
|
212
|
+
feature_key: FeatureKey;
|
|
213
|
+
name: Name2;
|
|
214
|
+
enabled: Enabled;
|
|
215
|
+
value: Value2;
|
|
216
|
+
variants?: Variants;
|
|
217
|
+
priority?: Priority;
|
|
218
|
+
[k: string]: unknown;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Represents a multivariate value for a feature flag.
|
|
222
|
+
*/
|
|
223
|
+
export interface FeatureValue {
|
|
224
|
+
value: Value3;
|
|
225
|
+
weight: Weight;
|
|
226
|
+
[k: string]: unknown;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Features to be evaluated in the context.
|
|
230
|
+
*/
|
|
231
|
+
export interface Features {
|
|
232
|
+
[k: string]: FeatureContext;
|
|
233
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* This file was automatically generated by json-schema-to-typescript.
|
|
4
|
+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
|
|
5
|
+
* and run json-schema-to-typescript to regenerate this file.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Feature name.
|
|
10
|
+
*/
|
|
11
|
+
export type Name = string;
|
|
12
|
+
/**
|
|
13
|
+
* Indicates if the feature flag is enabled.
|
|
14
|
+
*/
|
|
15
|
+
export type Enabled = boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Feature flag value.
|
|
18
|
+
*/
|
|
19
|
+
export type Value = string | number | boolean | null;
|
|
20
|
+
/**
|
|
21
|
+
* Reason for the feature flag evaluation.
|
|
22
|
+
*/
|
|
23
|
+
export type Reason = string;
|
|
24
|
+
/**
|
|
25
|
+
* Segment name.
|
|
26
|
+
*/
|
|
27
|
+
export type Name1 = string;
|
|
28
|
+
/**
|
|
29
|
+
* List of segments which the provided context belongs to.
|
|
30
|
+
*/
|
|
31
|
+
export type Segments = SegmentResult[];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Evaluation result object containing the used context, flag evaluation results, and segments used in the evaluation.
|
|
35
|
+
*/
|
|
36
|
+
export interface EvaluationResult {
|
|
37
|
+
flags: Flags;
|
|
38
|
+
segments: Segments;
|
|
39
|
+
[k: string]: unknown;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Feature flags evaluated for the context, mapped by feature names.
|
|
43
|
+
*/
|
|
44
|
+
export interface Flags {
|
|
45
|
+
[k: string]: FlagResult;
|
|
46
|
+
}
|
|
47
|
+
export interface FlagResult {
|
|
48
|
+
name: Name;
|
|
49
|
+
enabled: Enabled;
|
|
50
|
+
value: Value;
|
|
51
|
+
reason: Reason;
|
|
52
|
+
metadata?: FeatureMetadata;
|
|
53
|
+
[k: string]: unknown;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Additional metadata associated with the feature.
|
|
57
|
+
*/
|
|
58
|
+
export interface FeatureMetadata {
|
|
59
|
+
[k: string]: unknown;
|
|
60
|
+
}
|
|
61
|
+
export interface SegmentResult {
|
|
62
|
+
name: Name1;
|
|
63
|
+
metadata?: SegmentMetadata;
|
|
64
|
+
[k: string]: unknown;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Additional metadata associated with the segment.
|
|
68
|
+
*/
|
|
69
|
+
export interface SegmentMetadata {
|
|
70
|
+
[k: string]: unknown;
|
|
71
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// This file is the entry point for the evaluation module types
|
|
2
|
+
// All types from evaluations should be at least imported here and re-exported
|
|
3
|
+
// Do not use types directly from generated files
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
EvaluationResult as EvaluationContextResult,
|
|
7
|
+
FlagResult,
|
|
8
|
+
FeatureMetadata,
|
|
9
|
+
SegmentMetadata
|
|
10
|
+
} from './evaluationResult/evaluationResult.types.js';
|
|
11
|
+
|
|
12
|
+
import type {
|
|
13
|
+
FeatureContext,
|
|
14
|
+
EnvironmentContext,
|
|
15
|
+
IdentityContext,
|
|
16
|
+
SegmentContext
|
|
17
|
+
} from './evaluationContext/evaluationContext.types.js';
|
|
18
|
+
|
|
19
|
+
export * from './evaluationContext/evaluationContext.types.js';
|
|
20
|
+
|
|
21
|
+
export enum SegmentSource {
|
|
22
|
+
API = 'api',
|
|
23
|
+
IDENTITY_OVERRIDE = 'identity_override'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Feature types
|
|
27
|
+
export interface SDKFeatureMetadata extends FeatureMetadata {
|
|
28
|
+
id: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface FeatureContextWithMetadata<T extends FeatureMetadata = FeatureMetadata>
|
|
32
|
+
extends FeatureContext {
|
|
33
|
+
metadata: T;
|
|
34
|
+
[k: string]: unknown;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type FeaturesWithMetadata<T extends FeatureMetadata = FeatureMetadata> = {
|
|
38
|
+
[k: string]: FeatureContextWithMetadata<T>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type FlagResultWithMetadata<T extends FeatureMetadata = FeatureMetadata> = FlagResult & {
|
|
42
|
+
metadata: T;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type EvaluationResultFlags<T extends FeatureMetadata = FeatureMetadata> = Record<
|
|
46
|
+
string,
|
|
47
|
+
FlagResultWithMetadata<T>
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
// Segment types
|
|
51
|
+
export interface SDKSegmentMetadata extends SegmentMetadata {
|
|
52
|
+
id?: number;
|
|
53
|
+
source?: SegmentSource;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SegmentContextWithMetadata<T extends SegmentMetadata = SegmentMetadata>
|
|
57
|
+
extends SegmentContext {
|
|
58
|
+
metadata: T;
|
|
59
|
+
overrides?: FeatureContextWithMetadata<FeatureMetadata>[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type SegmentsWithMetadata<T extends SegmentMetadata = SegmentMetadata> = {
|
|
63
|
+
[k: string]: SegmentContextWithMetadata<T>;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export interface SegmentResultWithMetadata {
|
|
67
|
+
name: string;
|
|
68
|
+
metadata: SDKSegmentMetadata;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type EvaluationResultSegments = SegmentResultWithMetadata[];
|
|
72
|
+
|
|
73
|
+
// Evaluation context types
|
|
74
|
+
export interface GenericEvaluationContext<
|
|
75
|
+
T extends FeatureMetadata = FeatureMetadata,
|
|
76
|
+
S extends SegmentMetadata = SegmentMetadata
|
|
77
|
+
> {
|
|
78
|
+
environment: EnvironmentContext;
|
|
79
|
+
identity?: IdentityContext | null;
|
|
80
|
+
segments?: SegmentsWithMetadata<S>;
|
|
81
|
+
features?: FeaturesWithMetadata<T>;
|
|
82
|
+
[k: string]: unknown;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type EvaluationContextWithMetadata = GenericEvaluationContext<
|
|
86
|
+
SDKFeatureMetadata,
|
|
87
|
+
SDKSegmentMetadata
|
|
88
|
+
>;
|
|
89
|
+
|
|
90
|
+
// Evaluation result types
|
|
91
|
+
export type EvaluationResult<T extends FeatureMetadata = FeatureMetadata> = {
|
|
92
|
+
flags: EvaluationResultFlags<T>;
|
|
93
|
+
segments: EvaluationResultSegments;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export type EvaluationResultWithMetadata = EvaluationResult<SDKFeatureMetadata>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomUUID as uuidv4 } from 'node:crypto';
|
|
2
|
-
import {
|
|
2
|
+
import { getHashedPercentageForObjIds } from '../utils/hashing/index.js';
|
|
3
3
|
|
|
4
4
|
export class FeatureModel {
|
|
5
5
|
id: number;
|
|
@@ -103,6 +103,7 @@ export class FeatureStateModel {
|
|
|
103
103
|
const sortedF = this.multivariateFeatureStateValues.sort((a, b) => {
|
|
104
104
|
return a.id - b.id;
|
|
105
105
|
});
|
|
106
|
+
|
|
106
107
|
for (const myValue of sortedF) {
|
|
107
108
|
switch (myValue.percentageAllocation) {
|
|
108
109
|
case 0:
|
|
@@ -111,7 +112,7 @@ export class FeatureStateModel {
|
|
|
111
112
|
return myValue.multivariateFeatureOption.value;
|
|
112
113
|
default:
|
|
113
114
|
if (percentageValue === undefined) {
|
|
114
|
-
percentageValue =
|
|
115
|
+
percentageValue = getHashedPercentageForObjIds([
|
|
115
116
|
this.djangoID || this.featurestateUUID,
|
|
116
117
|
identityID
|
|
117
118
|
]);
|
|
@@ -46,3 +46,7 @@ export function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStat
|
|
|
46
46
|
export function buildFeatureSegment(featureSegmentJSON: any): FeatureSegment {
|
|
47
47
|
return new FeatureSegment(featureSegmentJSON.priority);
|
|
48
48
|
}
|
|
49
|
+
|
|
50
|
+
export function uuidToBigInt(uuid: string): BigInt {
|
|
51
|
+
return BigInt('0x' + uuid.replace(/-/g, ''));
|
|
52
|
+
}
|