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,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,152 @@
|
|
|
1
|
+
import { SegmentSource } from '../models.js';
|
|
2
|
+
import { IDENTITY_OVERRIDE_SEGMENT_NAME } from '../../segments/constants.js';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { uuidToBigInt } from '../../features/util.js';
|
|
5
|
+
export function getEvaluationContext(environment, identity, overrideTraits, isEnvironmentEvaluation = false) {
|
|
6
|
+
const environmentContext = mapEnvironmentModelToEvaluationContext(environment);
|
|
7
|
+
if (isEnvironmentEvaluation) {
|
|
8
|
+
return environmentContext;
|
|
9
|
+
}
|
|
10
|
+
const identityContext = identity
|
|
11
|
+
? mapIdentityModelToIdentityContext(identity, overrideTraits)
|
|
12
|
+
: undefined;
|
|
13
|
+
const context = {
|
|
14
|
+
...environmentContext,
|
|
15
|
+
...(identityContext && { identity: identityContext })
|
|
16
|
+
};
|
|
17
|
+
return context;
|
|
18
|
+
}
|
|
19
|
+
function mapEnvironmentModelToEvaluationContext(environment) {
|
|
20
|
+
const environmentContext = {
|
|
21
|
+
key: environment.apiKey,
|
|
22
|
+
name: environment.name
|
|
23
|
+
};
|
|
24
|
+
const features = {};
|
|
25
|
+
for (const fs of environment.featureStates) {
|
|
26
|
+
const variants = fs.multivariateFeatureStateValues?.length > 0
|
|
27
|
+
? fs.multivariateFeatureStateValues.map(mv => ({
|
|
28
|
+
value: mv.multivariateFeatureOption.value,
|
|
29
|
+
weight: mv.percentageAllocation,
|
|
30
|
+
priority: mv.id ?? uuidToBigInt(mv.mvFsValueUuid)
|
|
31
|
+
}))
|
|
32
|
+
: undefined;
|
|
33
|
+
features[fs.feature.name] = {
|
|
34
|
+
key: fs.djangoID?.toString() || fs.featurestateUUID,
|
|
35
|
+
name: fs.feature.name,
|
|
36
|
+
enabled: fs.enabled,
|
|
37
|
+
value: fs.getValue(),
|
|
38
|
+
variants,
|
|
39
|
+
priority: fs.featureSegment?.priority,
|
|
40
|
+
metadata: {
|
|
41
|
+
id: fs.feature.id
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const segmentOverrides = {};
|
|
46
|
+
for (const segment of environment.project.segments) {
|
|
47
|
+
segmentOverrides[segment.id.toString()] = {
|
|
48
|
+
key: segment.id.toString(),
|
|
49
|
+
name: segment.name,
|
|
50
|
+
rules: segment.rules.map(rule => mapSegmentRuleModelToRule(rule)),
|
|
51
|
+
overrides: segment.featureStates.length > 0
|
|
52
|
+
? segment.featureStates.map(fs => ({
|
|
53
|
+
key: fs.djangoID?.toString() || fs.featurestateUUID,
|
|
54
|
+
name: fs.feature.name,
|
|
55
|
+
enabled: fs.enabled,
|
|
56
|
+
value: fs.getValue(),
|
|
57
|
+
priority: fs.featureSegment?.priority,
|
|
58
|
+
metadata: {
|
|
59
|
+
id: fs.feature.id
|
|
60
|
+
}
|
|
61
|
+
}))
|
|
62
|
+
: [],
|
|
63
|
+
metadata: {
|
|
64
|
+
source: SegmentSource.API,
|
|
65
|
+
id: segment.id
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
let identityOverrideSegments = {};
|
|
70
|
+
if (environment.identityOverrides && environment.identityOverrides.length > 0) {
|
|
71
|
+
identityOverrideSegments = mapIdentityOverridesToSegments(environment.identityOverrides);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
environment: environmentContext,
|
|
75
|
+
features,
|
|
76
|
+
segments: {
|
|
77
|
+
...segmentOverrides,
|
|
78
|
+
...identityOverrideSegments
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function mapIdentityModelToIdentityContext(identity, overrideTraits) {
|
|
83
|
+
const traits = overrideTraits || identity.identityTraits;
|
|
84
|
+
const traitsContext = {};
|
|
85
|
+
for (const trait of traits) {
|
|
86
|
+
traitsContext[trait.traitKey] = trait.traitValue;
|
|
87
|
+
}
|
|
88
|
+
const identityContext = {
|
|
89
|
+
identifier: identity.identifier,
|
|
90
|
+
traits: traitsContext
|
|
91
|
+
};
|
|
92
|
+
return identityContext;
|
|
93
|
+
}
|
|
94
|
+
function mapSegmentRuleModelToRule(rule) {
|
|
95
|
+
return {
|
|
96
|
+
type: rule.type,
|
|
97
|
+
conditions: rule.conditions.map((condition) => ({
|
|
98
|
+
property: condition.property,
|
|
99
|
+
operator: condition.operator,
|
|
100
|
+
value: condition.value
|
|
101
|
+
})),
|
|
102
|
+
rules: rule.rules.map((subRule) => mapSegmentRuleModelToRule(subRule))
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function mapIdentityOverridesToSegments(identityOverrides) {
|
|
106
|
+
const segments = {};
|
|
107
|
+
const featuresToIdentifiers = new Map();
|
|
108
|
+
for (const identity of identityOverrides) {
|
|
109
|
+
if (!identity.identityFeatures || identity.identityFeatures.length === 0) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const sortedFeatures = [...identity.identityFeatures].sort((a, b) => a.feature.name.localeCompare(b.feature.name));
|
|
113
|
+
const overridesKey = sortedFeatures.map(fs => ({
|
|
114
|
+
name: fs.feature.name,
|
|
115
|
+
enabled: fs.enabled,
|
|
116
|
+
value: fs.getValue(),
|
|
117
|
+
priority: -Infinity,
|
|
118
|
+
metadata: {
|
|
119
|
+
id: fs.feature.id
|
|
120
|
+
}
|
|
121
|
+
}));
|
|
122
|
+
const overridesHash = createHash('sha1').update(JSON.stringify(overridesKey)).digest('hex');
|
|
123
|
+
if (!featuresToIdentifiers.has(overridesHash)) {
|
|
124
|
+
featuresToIdentifiers.set(overridesHash, { identifiers: [], overrides: overridesKey });
|
|
125
|
+
}
|
|
126
|
+
featuresToIdentifiers.get(overridesHash).identifiers.push(identity.identifier);
|
|
127
|
+
}
|
|
128
|
+
for (const [overrideHash, { identifiers, overrides }] of featuresToIdentifiers.entries()) {
|
|
129
|
+
const segmentKey = `identity_override_${overrideHash}`;
|
|
130
|
+
segments[segmentKey] = {
|
|
131
|
+
key: segmentKey,
|
|
132
|
+
name: IDENTITY_OVERRIDE_SEGMENT_NAME,
|
|
133
|
+
rules: [
|
|
134
|
+
{
|
|
135
|
+
type: 'ALL',
|
|
136
|
+
conditions: [
|
|
137
|
+
{
|
|
138
|
+
property: '$.identity.identifier',
|
|
139
|
+
operator: 'IN',
|
|
140
|
+
value: identifiers.join(',')
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
],
|
|
145
|
+
metadata: {
|
|
146
|
+
source: SegmentSource.IDENTITY_OVERRIDE
|
|
147
|
+
},
|
|
148
|
+
overrides: overrides
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
return segments;
|
|
152
|
+
}
|
|
@@ -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,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,50 @@
|
|
|
1
|
+
import type { FlagResult, FeatureMetadata, SegmentMetadata } from './evaluationResult/evaluationResult.types.js';
|
|
2
|
+
import type { FeatureContext, EnvironmentContext, IdentityContext, SegmentContext } from './evaluationContext/evaluationContext.types.js';
|
|
3
|
+
export * from './evaluationContext/evaluationContext.types.js';
|
|
4
|
+
export declare enum SegmentSource {
|
|
5
|
+
API = "api",
|
|
6
|
+
IDENTITY_OVERRIDE = "identity_override"
|
|
7
|
+
}
|
|
8
|
+
export interface SDKFeatureMetadata extends FeatureMetadata {
|
|
9
|
+
id: number;
|
|
10
|
+
}
|
|
11
|
+
export interface FeatureContextWithMetadata<T extends FeatureMetadata = FeatureMetadata> extends FeatureContext {
|
|
12
|
+
metadata: T;
|
|
13
|
+
[k: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
export type FeaturesWithMetadata<T extends FeatureMetadata = FeatureMetadata> = {
|
|
16
|
+
[k: string]: FeatureContextWithMetadata<T>;
|
|
17
|
+
};
|
|
18
|
+
export type FlagResultWithMetadata<T extends FeatureMetadata = FeatureMetadata> = FlagResult & {
|
|
19
|
+
metadata: T;
|
|
20
|
+
};
|
|
21
|
+
export type EvaluationResultFlags<T extends FeatureMetadata = FeatureMetadata> = Record<string, FlagResultWithMetadata<T>>;
|
|
22
|
+
export interface SDKSegmentMetadata extends SegmentMetadata {
|
|
23
|
+
id?: number;
|
|
24
|
+
source?: SegmentSource;
|
|
25
|
+
}
|
|
26
|
+
export interface SegmentContextWithMetadata<T extends SegmentMetadata = SegmentMetadata> extends SegmentContext {
|
|
27
|
+
metadata: T;
|
|
28
|
+
overrides?: FeatureContextWithMetadata<FeatureMetadata>[];
|
|
29
|
+
}
|
|
30
|
+
export type SegmentsWithMetadata<T extends SegmentMetadata = SegmentMetadata> = {
|
|
31
|
+
[k: string]: SegmentContextWithMetadata<T>;
|
|
32
|
+
};
|
|
33
|
+
export interface SegmentResultWithMetadata {
|
|
34
|
+
name: string;
|
|
35
|
+
metadata: SDKSegmentMetadata;
|
|
36
|
+
}
|
|
37
|
+
export type EvaluationResultSegments = SegmentResultWithMetadata[];
|
|
38
|
+
export interface GenericEvaluationContext<T extends FeatureMetadata = FeatureMetadata, S extends SegmentMetadata = SegmentMetadata> {
|
|
39
|
+
environment: EnvironmentContext;
|
|
40
|
+
identity?: IdentityContext | null;
|
|
41
|
+
segments?: SegmentsWithMetadata<S>;
|
|
42
|
+
features?: FeaturesWithMetadata<T>;
|
|
43
|
+
[k: string]: unknown;
|
|
44
|
+
}
|
|
45
|
+
export type EvaluationContextWithMetadata = GenericEvaluationContext<SDKFeatureMetadata, SDKSegmentMetadata>;
|
|
46
|
+
export type EvaluationResult<T extends FeatureMetadata = FeatureMetadata> = {
|
|
47
|
+
flags: EvaluationResultFlags<T>;
|
|
48
|
+
segments: EvaluationResultSegments;
|
|
49
|
+
};
|
|
50
|
+
export type EvaluationResultWithMetadata = EvaluationResult<SDKFeatureMetadata>;
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
export * from './evaluationContext/evaluationContext.types.js';
|
|
5
|
+
export var SegmentSource;
|
|
6
|
+
(function (SegmentSource) {
|
|
7
|
+
SegmentSource["API"] = "api";
|
|
8
|
+
SegmentSource["IDENTITY_OVERRIDE"] = "identity_override";
|
|
9
|
+
})(SegmentSource || (SegmentSource = {}));
|
|
@@ -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
|
export class FeatureModel {
|
|
4
4
|
id;
|
|
5
5
|
name;
|
|
@@ -87,7 +87,7 @@ export class FeatureStateModel {
|
|
|
87
87
|
return myValue.multivariateFeatureOption.value;
|
|
88
88
|
default:
|
|
89
89
|
if (percentageValue === undefined) {
|
|
90
|
-
percentageValue =
|
|
90
|
+
percentageValue = getHashedPercentageForObjIds([
|
|
91
91
|
this.djangoID || this.featurestateUUID,
|
|
92
92
|
identityID
|
|
93
93
|
]);
|
|
@@ -2,3 +2,4 @@ import { FeatureModel, FeatureSegment, FeatureStateModel } from './models.js';
|
|
|
2
2
|
export declare function buildFeatureModel(featuresModelJSON: any): FeatureModel;
|
|
3
3
|
export declare function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStateModel;
|
|
4
4
|
export declare function buildFeatureSegment(featureSegmentJSON: any): FeatureSegment;
|
|
5
|
+
export declare function uuidToBigInt(uuid: string): BigInt;
|
|
@@ -19,3 +19,6 @@ export function buildFeatureStateModel(featuresStateModelJSON) {
|
|
|
19
19
|
export function buildFeatureSegment(featureSegmentJSON) {
|
|
20
20
|
return new FeatureSegment(featureSegmentJSON.priority);
|
|
21
21
|
}
|
|
22
|
+
export function uuidToBigInt(uuid) {
|
|
23
|
+
return BigInt('0x' + uuid.replace(/-/g, ''));
|
|
24
|
+
}
|
|
@@ -1,14 +1,66 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { IdentityModel } from './identities/models.js';
|
|
4
|
-
import { TraitModel } from './identities/traits/models.js';
|
|
1
|
+
import { EvaluationContextWithMetadata, EvaluationResultSegments, EvaluationResultWithMetadata, FeatureContextWithMetadata, SDKFeatureMetadata } from './evaluation/models.js';
|
|
2
|
+
import { EvaluationResultFlags } from './evaluation/models.js';
|
|
5
3
|
export { EnvironmentModel } from './environments/models.js';
|
|
6
|
-
export { FeatureModel, FeatureStateModel } from './features/models.js';
|
|
7
4
|
export { IdentityModel } from './identities/models.js';
|
|
8
5
|
export { TraitModel } from './identities/traits/models.js';
|
|
9
6
|
export { SegmentModel } from './segments/models.js';
|
|
7
|
+
export { FeatureModel, FeatureStateModel } from './features/models.js';
|
|
10
8
|
export { OrganisationModel } from './organisations/models.js';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
type SegmentOverride = {
|
|
10
|
+
feature: FeatureContextWithMetadata<SDKFeatureMetadata>;
|
|
11
|
+
segmentName: string;
|
|
12
|
+
};
|
|
13
|
+
export type SegmentOverrides = Record<string, SegmentOverride>;
|
|
14
|
+
/**
|
|
15
|
+
* Evaluates flags and segments for the given context.
|
|
16
|
+
*
|
|
17
|
+
* This is the main entry point for the evaluation engine. It processes segments,
|
|
18
|
+
* applies feature overrides based on segment priority, and returns the final flag states with
|
|
19
|
+
* evaluation reasons.
|
|
20
|
+
*
|
|
21
|
+
* @param context - EvaluationContext containing environment, identity, and segment data
|
|
22
|
+
* @returns EvaluationResult with flags, segments, and original context
|
|
23
|
+
*/
|
|
24
|
+
export declare function getEvaluationResult(context: EvaluationContextWithMetadata): EvaluationResultWithMetadata;
|
|
25
|
+
/**
|
|
26
|
+
* Evaluates which segments the identity belongs to and collects feature overrides.
|
|
27
|
+
*
|
|
28
|
+
* @param context - EvaluationContext containing identity and segment definitions
|
|
29
|
+
* @returns Object containing segments the identity belongs to and any feature overrides
|
|
30
|
+
*/
|
|
31
|
+
export declare function evaluateSegments(context: EvaluationContextWithMetadata): {
|
|
32
|
+
segments: EvaluationResultSegments;
|
|
33
|
+
segmentOverrides: Record<string, SegmentOverride>;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Processes feature overrides from segments, applying priority rules.
|
|
37
|
+
*
|
|
38
|
+
* When multiple segments override the same feature, the segment with
|
|
39
|
+
* higher priority (lower numeric value) takes precedence.
|
|
40
|
+
*
|
|
41
|
+
* @param identitySegments - Segments that the identity belongs to
|
|
42
|
+
* @returns Map of feature keys to their highest-priority segment overrides
|
|
43
|
+
*/
|
|
44
|
+
export declare function processSegmentOverrides(identitySegments: any[]): Record<string, SegmentOverride>;
|
|
45
|
+
/**
|
|
46
|
+
* Evaluates all features in the context, applying segment overrides where applicable.
|
|
47
|
+
* For each feature:
|
|
48
|
+
* - Checks if a segment override exists
|
|
49
|
+
* - Uses override values if present, otherwise evaluates the base feature
|
|
50
|
+
* - Determines appropriate evaluation reason
|
|
51
|
+
* - Handles multivariate evaluation for features without overrides
|
|
52
|
+
*
|
|
53
|
+
* @param context - EvaluationContext containing features and identity
|
|
54
|
+
* @param segmentOverrides - Map of feature keys to their segment overrides
|
|
55
|
+
* @returns EvaluationResultFlags containing evaluated flag results
|
|
56
|
+
*/
|
|
57
|
+
export declare function evaluateFeatures(context: EvaluationContextWithMetadata, segmentOverrides: Record<string, SegmentOverride>): EvaluationResultFlags<SDKFeatureMetadata>;
|
|
58
|
+
export declare function shouldApplyOverride(override: any, existingOverrides: Record<string, SegmentOverride>): boolean;
|
|
59
|
+
export declare function isHigherPriority(priorityA: number | undefined, priorityB: number | undefined): boolean;
|
|
60
|
+
export type TargetingMatchReason = {
|
|
61
|
+
type: 'SEGMENT';
|
|
62
|
+
override: SegmentOverride;
|
|
63
|
+
} | {
|
|
64
|
+
type: 'SPLIT';
|
|
65
|
+
weight: number;
|
|
66
|
+
};
|