flagsmith-nodejs 6.2.0 → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/.github/workflows/publish.yml +8 -5
  2. package/.github/workflows/pull_request.yaml +3 -0
  3. package/.gitmodules +1 -1
  4. package/.husky/pre-commit +1 -0
  5. package/.nvmrc +1 -0
  6. package/.release-please-manifest.json +1 -1
  7. package/CHANGELOG.md +40 -0
  8. package/build/cjs/flagsmith-engine/environments/models.d.ts +2 -1
  9. package/build/cjs/flagsmith-engine/environments/models.js +3 -1
  10. package/build/cjs/flagsmith-engine/environments/util.js +1 -1
  11. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.d.ts +230 -0
  12. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.js +8 -0
  13. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/mappers.d.ts +5 -0
  14. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/mappers.js +156 -0
  15. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/types.d.ts +216 -0
  16. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/types.js +8 -0
  17. package/build/cjs/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.d.ts +68 -0
  18. package/build/cjs/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.js +8 -0
  19. package/build/cjs/flagsmith-engine/evaluation/models.d.ts +50 -0
  20. package/build/cjs/flagsmith-engine/evaluation/models.js +26 -0
  21. package/build/cjs/flagsmith-engine/features/models.js +1 -1
  22. package/build/cjs/flagsmith-engine/features/types.d.ts +5 -0
  23. package/build/cjs/flagsmith-engine/features/types.js +9 -0
  24. package/build/cjs/flagsmith-engine/features/util.d.ts +1 -0
  25. package/build/cjs/flagsmith-engine/features/util.js +5 -1
  26. package/build/cjs/flagsmith-engine/index.d.ts +61 -9
  27. package/build/cjs/flagsmith-engine/index.js +176 -56
  28. package/build/cjs/flagsmith-engine/segments/constants.d.ts +1 -0
  29. package/build/cjs/flagsmith-engine/segments/constants.js +2 -1
  30. package/build/cjs/flagsmith-engine/segments/evaluators.d.ts +41 -7
  31. package/build/cjs/flagsmith-engine/segments/evaluators.js +136 -24
  32. package/build/cjs/flagsmith-engine/segments/models.d.ts +9 -4
  33. package/build/cjs/flagsmith-engine/segments/models.js +115 -13
  34. package/build/cjs/flagsmith-engine/utils/hashing/index.d.ts +1 -1
  35. package/build/cjs/flagsmith-engine/utils/hashing/index.js +4 -4
  36. package/build/cjs/sdk/index.d.ts +1 -3
  37. package/build/cjs/sdk/index.js +22 -19
  38. package/build/cjs/sdk/models.d.ts +8 -1
  39. package/build/cjs/sdk/models.js +29 -1
  40. package/build/esm/flagsmith-engine/environments/models.d.ts +2 -1
  41. package/build/esm/flagsmith-engine/environments/models.js +3 -1
  42. package/build/esm/flagsmith-engine/environments/util.js +1 -1
  43. package/build/esm/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.d.ts +230 -0
  44. package/build/esm/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.js +7 -0
  45. package/build/esm/flagsmith-engine/evaluation/evaluationContext/mappers.d.ts +5 -0
  46. package/build/esm/flagsmith-engine/evaluation/evaluationContext/mappers.js +152 -0
  47. package/build/esm/flagsmith-engine/evaluation/evaluationContext/types.d.ts +216 -0
  48. package/build/esm/flagsmith-engine/evaluation/evaluationContext/types.js +7 -0
  49. package/build/esm/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.d.ts +68 -0
  50. package/build/esm/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.js +7 -0
  51. package/build/esm/flagsmith-engine/evaluation/models.d.ts +50 -0
  52. package/build/esm/flagsmith-engine/evaluation/models.js +9 -0
  53. package/build/esm/flagsmith-engine/features/models.js +2 -2
  54. package/build/esm/flagsmith-engine/features/types.d.ts +5 -0
  55. package/build/esm/flagsmith-engine/features/types.js +6 -0
  56. package/build/esm/flagsmith-engine/features/util.d.ts +1 -0
  57. package/build/esm/flagsmith-engine/features/util.js +3 -0
  58. package/build/esm/flagsmith-engine/index.d.ts +61 -9
  59. package/build/esm/flagsmith-engine/index.js +161 -43
  60. package/build/esm/flagsmith-engine/segments/constants.d.ts +1 -0
  61. package/build/esm/flagsmith-engine/segments/constants.js +1 -0
  62. package/build/esm/flagsmith-engine/segments/evaluators.d.ts +41 -7
  63. package/build/esm/flagsmith-engine/segments/evaluators.js +137 -25
  64. package/build/esm/flagsmith-engine/segments/models.d.ts +9 -4
  65. package/build/esm/flagsmith-engine/segments/models.js +115 -13
  66. package/build/esm/flagsmith-engine/utils/hashing/index.d.ts +1 -1
  67. package/build/esm/flagsmith-engine/utils/hashing/index.js +2 -2
  68. package/build/esm/sdk/index.d.ts +1 -3
  69. package/build/esm/sdk/index.js +21 -18
  70. package/build/esm/sdk/models.d.ts +8 -1
  71. package/build/esm/sdk/models.js +29 -1
  72. package/flagsmith-engine/environments/models.ts +3 -1
  73. package/flagsmith-engine/environments/util.ts +2 -1
  74. package/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.ts +247 -0
  75. package/flagsmith-engine/evaluation/evaluationContext/mappers.ts +204 -0
  76. package/flagsmith-engine/evaluation/evaluationContext/types.ts +233 -0
  77. package/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.ts +71 -0
  78. package/flagsmith-engine/evaluation/models.ts +96 -0
  79. package/flagsmith-engine/features/models.ts +3 -2
  80. package/flagsmith-engine/features/types.ts +5 -0
  81. package/flagsmith-engine/features/util.ts +4 -0
  82. package/flagsmith-engine/index.ts +229 -72
  83. package/flagsmith-engine/segments/constants.ts +1 -0
  84. package/flagsmith-engine/segments/evaluators.ts +178 -62
  85. package/flagsmith-engine/segments/models.ts +171 -23
  86. package/flagsmith-engine/utils/hashing/index.ts +2 -2
  87. package/package.json +13 -2
  88. package/sdk/index.ts +36 -23
  89. package/sdk/models.ts +44 -2
  90. package/tests/engine/e2e/engine.test.ts +43 -38
  91. package/tests/engine/unit/engine.test.ts +306 -59
  92. package/tests/engine/unit/mappers.test.ts +353 -0
  93. package/tests/engine/unit/segments/segment_evaluators.test.ts +391 -49
  94. package/tests/engine/unit/segments/segments_model.test.ts +85 -0
  95. package/tests/engine/unit/utils/utils.test.ts +7 -7
  96. package/tests/engine/unit/utils.ts +1 -1
  97. package/tests/sdk/data/environment.json +1 -0
  98. package/tests/sdk/flagsmith.test.ts +29 -3
  99. package/tests/sdk/offline-handlers.test.ts +3 -1
  100. package/vitest.config.esm.ts +34 -0
@@ -5,6 +5,10 @@ on:
5
5
  tags:
6
6
  - '*'
7
7
 
8
+ permissions:
9
+ id-token: write # Required for OIDC
10
+ contents: read
11
+
8
12
  jobs:
9
13
  package:
10
14
  runs-on: ubuntu-latest
@@ -12,14 +16,13 @@ jobs:
12
16
 
13
17
  steps:
14
18
  - name: Cloning repo
15
- uses: actions/checkout@v3
19
+ uses: actions/checkout@v5
16
20
 
17
- - uses: actions/setup-node@v4
21
+ - uses: actions/setup-node@v5
18
22
  with:
19
- node-version: '18.x'
23
+ node-version-file: .nvmrc
20
24
  registry-url: 'https://registry.npmjs.org'
21
25
 
26
+ - run: npm install -g npm@latest
22
27
  - run: npm ci
23
28
  - run: npm run deploy
24
- env:
25
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -35,3 +35,6 @@ jobs:
35
35
  - run: npm test
36
36
  env:
37
37
  CI: true
38
+ - run: npm run test:esm-build
39
+ env:
40
+ CI: true
package/.gitmodules CHANGED
@@ -1,4 +1,4 @@
1
1
  [submodule "tests/engine/engine-tests/engine-test-data"]
2
2
  path = tests/engine/engine-tests/engine-test-data
3
3
  url = git@github.com:Flagsmith/engine-test-data.git
4
- branch = v1.0.0
4
+ branch = v3.5.0
package/.husky/pre-commit CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/bin/sh
2
2
  . "$(dirname "$0")/_/husky.sh"
3
3
 
4
+ npm run generate-engine-types
4
5
  npm run lint
5
6
  git add ./flagsmith-engine ./sdk ./tests ./index.ts ./.github
6
7
  npm run test
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 24
@@ -1 +1 @@
1
- {".":"6.2.0"}
1
+ {".":"7.0.2"}
package/CHANGELOG.md CHANGED
@@ -1,5 +1,45 @@
1
1
  # Changelog
2
2
 
3
+ ## [7.0.2](https://github.com/Flagsmith/flagsmith-nodejs-client/compare/v7.0.1...v7.0.2) (2025-12-02)
4
+
5
+
6
+ ### CI
7
+
8
+ * use-nvmrc-for-version ([#236](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/236)) ([a56f073](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/a56f0736f6c31d3c273932ede8204710de7cf853))
9
+
10
+ ## [7.0.1](https://github.com/Flagsmith/flagsmith-nodejs-client/compare/v7.0.0...v7.0.1) (2025-12-02)
11
+
12
+
13
+ ### CI
14
+
15
+ * use-latest-npm ([#234](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/234)) ([6a741f3](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/6a741f3b12af3bea31150561dee1e6f7c7045e56))
16
+
17
+ ## [7.0.0](https://github.com/Flagsmith/flagsmith-nodejs-client/compare/v6.2.0...v7.0.0) (2025-12-02)
18
+
19
+
20
+ ### ⚠ BREAKING CHANGES
21
+
22
+ * implement context values ([#203](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/203))
23
+
24
+ ### Features
25
+
26
+ * implement context values ([#203](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/203)) ([41258f2](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/41258f2e24ef7e89207a0f10116ffbd1229c0a30))
27
+ * removed-feature-key-and-segment-key-from-schema ([#210](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/210)) ([014f38b](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/014f38bf33af77fb706e4e130e8a571914632408))
28
+
29
+
30
+ ### Bug Fixes
31
+
32
+ * exclude-identities-when-traits-is-undefined ([#230](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/230)) ([f7488e1](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/f7488e17fe524111dd18c06a30be1c44ae15ec5d))
33
+ * fix-mv-evaluation ([#222](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/222)) ([ae1fb7e](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/ae1fb7eb0551defd0823c94d37b860be7eb88a5d))
34
+ * properly-map-environment-name ([#226](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/226)) ([3c1d200](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/3c1d200e656ec9926fdc6d4627bb259963d06a2e))
35
+ * removed-dango-id-usage-in-mapper ([#229](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/229)) ([29c7613](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/29c761370a7e8d6d733a45293c60297843e7e1e7))
36
+ * use-default-on-jsonpath-import ([#231](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/231)) ([7a8d949](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/7a8d9498fbc0297c881b3f32fc8d0d024fe8366f))
37
+
38
+
39
+ ### CI
40
+
41
+ * use NPM trusted publishing ([#217](https://github.com/Flagsmith/flagsmith-nodejs-client/issues/217)) ([7d01563](https://github.com/Flagsmith/flagsmith-nodejs-client/commit/7d015635f4bc41246519799dddaea7ff8da2c50a))
42
+
3
43
  ## [6.2.0](https://github.com/Flagsmith/flagsmith-nodejs-client/compare/v6.1.0...v6.2.0) (2025-11-04)
4
44
 
5
45
 
@@ -18,5 +18,6 @@ export declare class EnvironmentModel {
18
18
  project: ProjectModel;
19
19
  featureStates: FeatureStateModel[];
20
20
  identityOverrides: IdentityModel[];
21
- constructor(id: number, apiKey: string, project: ProjectModel);
21
+ name: string;
22
+ constructor(id: number, apiKey: string, project: ProjectModel, name: string);
22
23
  }
@@ -28,10 +28,12 @@ class EnvironmentModel {
28
28
  project;
29
29
  featureStates = [];
30
30
  identityOverrides = [];
31
- constructor(id, apiKey, project) {
31
+ name;
32
+ constructor(id, apiKey, project, name) {
32
33
  this.id = id;
33
34
  this.apiKey = apiKey;
34
35
  this.project = project;
36
+ this.name = name;
35
37
  }
36
38
  }
37
39
  exports.EnvironmentModel = EnvironmentModel;
@@ -8,7 +8,7 @@ const models_js_1 = require("./models.js");
8
8
  function buildEnvironmentModel(environmentJSON) {
9
9
  const project = (0, util_js_3.buildProjectModel)(environmentJSON.project);
10
10
  const featureStates = environmentJSON.feature_states.map((fs) => (0, util_js_1.buildFeatureStateModel)(fs));
11
- const environmentModel = new models_js_1.EnvironmentModel(environmentJSON.id, environmentJSON.api_key, project);
11
+ const environmentModel = new models_js_1.EnvironmentModel(environmentJSON.id, environmentJSON.api_key, project, environmentJSON.name);
12
12
  environmentModel.featureStates = featureStates;
13
13
  if (!!environmentJSON.identity_overrides) {
14
14
  environmentModel.identityOverrides = environmentJSON.identity_overrides.map((identityData) => (0, util_js_2.buildIdentityModel)(identityData));
@@ -0,0 +1,230 @@
1
+ /**
2
+ * This file was automatically generated by json-schema-to-typescript.
3
+ * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
4
+ * and run json-schema-to-typescript to regenerate this file.
5
+ */
6
+ /**
7
+ * Unique environment key. May be used for selecting a value for a multivariate feature, or for % split segmentation.
8
+ */
9
+ export type Key = string;
10
+ /**
11
+ * An environment's human-readable name.
12
+ */
13
+ export type Name = string;
14
+ /**
15
+ * A unique identifier for an identity as displayed in the Flagsmith UI.
16
+ */
17
+ export type Identifier = string;
18
+ /**
19
+ * Key used when selecting a value for a multivariate feature, or for % split segmentation. Set to an internal identifier or a composite value based on the environment key and identifier, depending on Flagsmith implementation.
20
+ */
21
+ export type Key1 = string;
22
+ /**
23
+ * Unique segment key used for % split segmentation.
24
+ */
25
+ export type Key2 = string;
26
+ /**
27
+ * The name of the segment.
28
+ */
29
+ export type Name1 = string;
30
+ /**
31
+ * Segment rule type. Represents a logical quantifier for the conditions and sub-rules.
32
+ */
33
+ export type Type = 'ALL' | 'ANY' | 'NONE';
34
+ export type SegmentCondition = SegmentCondition1 | InSegmentCondition;
35
+ /**
36
+ * A reference to the identity trait or value in the evaluation context.
37
+ */
38
+ export type Property = string;
39
+ /**
40
+ * The operator to use for evaluating the condition.
41
+ */
42
+ export type Operator = 'EQUAL' | 'GREATER_THAN' | 'LESS_THAN' | 'LESS_THAN_INCLUSIVE' | 'CONTAINS' | 'GREATER_THAN_INCLUSIVE' | 'NOT_CONTAINS' | 'NOT_EQUAL' | 'REGEX' | 'PERCENTAGE_SPLIT' | 'MODULO' | 'IS_SET' | 'IS_NOT_SET' | 'IN';
43
+ /**
44
+ * The value to compare against the trait or context value.
45
+ */
46
+ export type Value = string;
47
+ /**
48
+ * A reference to the identity trait or value in the evaluation context.
49
+ */
50
+ export type Property1 = string;
51
+ /**
52
+ * The operator to use for evaluating the condition.
53
+ */
54
+ export type Operator1 = 'IN';
55
+ /**
56
+ * The values to compare against the trait or context value.
57
+ */
58
+ export type Value1 = string[];
59
+ /**
60
+ * Conditions that must be met for the rule to apply.
61
+ */
62
+ export type Conditions = SegmentCondition[];
63
+ /**
64
+ * Sub-rules nested within the segment rule.
65
+ */
66
+ export type SubRules = SegmentRule[];
67
+ /**
68
+ * Rules that define the segment.
69
+ */
70
+ export type Rules = SegmentRule[];
71
+ /**
72
+ * Unique feature key used when selecting a variant if the feature is multivariate. Set to an internal identifier or a UUID, depending on Flagsmith implementation.
73
+ */
74
+ export type Key3 = string;
75
+ /**
76
+ * Feature name.
77
+ */
78
+ export type Name2 = string;
79
+ /**
80
+ * Indicates whether the feature is enabled in the environment.
81
+ */
82
+ export type Enabled = boolean;
83
+ /**
84
+ * A default environment value for the feature. If the feature is multivariate, this will be the control value.
85
+ */
86
+ export type Value2 = string | number | boolean | null;
87
+ /**
88
+ * The value of the feature.
89
+ */
90
+ export type Value3 = string | number | boolean | null;
91
+ /**
92
+ * The weight of the feature value variant, as a percentage number (i.e. 100.0).
93
+ */
94
+ export type Weight = number;
95
+ /**
96
+ * Priority of the feature flag variant. Lower values indicate a higher priority when multiple variants apply to the same context key.
97
+ */
98
+ export type VariantPriority = number;
99
+ /**
100
+ * An array of environment default values associated with the feature. Empty for standard features, or contains multiple values for multivariate features.
101
+ */
102
+ export type Variants = FeatureValue[];
103
+ /**
104
+ * Priority of the feature context. Lower values indicate a higher priority when multiple contexts apply to the same feature.
105
+ */
106
+ export type FeaturePriority = number;
107
+ /**
108
+ * Feature overrides for the segment.
109
+ */
110
+ export type Overrides = FeatureContext[];
111
+ /**
112
+ * A context object containing the necessary information to evaluate Flagsmith feature flags.
113
+ */
114
+ export interface EvaluationContext {
115
+ environment: EnvironmentContext;
116
+ /**
117
+ * Identity context used for identity-based evaluation.
118
+ */
119
+ identity?: IdentityContext | null;
120
+ segments?: Segments;
121
+ features?: Features;
122
+ [k: string]: unknown;
123
+ }
124
+ /**
125
+ * Environment context required for evaluation.
126
+ */
127
+ export interface EnvironmentContext {
128
+ key: Key;
129
+ name: Name;
130
+ [k: string]: unknown;
131
+ }
132
+ /**
133
+ * Represents an identity context for feature flag evaluation.
134
+ */
135
+ export interface IdentityContext {
136
+ identifier: Identifier;
137
+ key?: Key1;
138
+ traits?: Traits;
139
+ [k: string]: unknown;
140
+ }
141
+ /**
142
+ * A map of traits associated with the identity, where the key is the trait name and the value is the trait value.
143
+ */
144
+ export interface Traits {
145
+ [k: string]: string | number | boolean | null;
146
+ }
147
+ /**
148
+ * Segments applicable to the evaluation context.
149
+ */
150
+ export interface Segments {
151
+ [k: string]: SegmentContext;
152
+ }
153
+ /**
154
+ * Represents a segment context for feature flag evaluation.
155
+ */
156
+ export interface SegmentContext {
157
+ key: Key2;
158
+ name: Name1;
159
+ rules: Rules;
160
+ overrides?: Overrides;
161
+ metadata?: SegmentMetadata;
162
+ [k: string]: unknown;
163
+ }
164
+ /**
165
+ * Represents a rule within a segment for feature flag evaluation.
166
+ */
167
+ export interface SegmentRule {
168
+ type: Type;
169
+ conditions?: Conditions;
170
+ rules?: SubRules;
171
+ [k: string]: unknown;
172
+ }
173
+ /**
174
+ * Represents a condition within a segment rule for feature flag evaluation.
175
+ */
176
+ export interface SegmentCondition1 {
177
+ property: Property;
178
+ operator: Operator;
179
+ value: Value;
180
+ [k: string]: unknown;
181
+ }
182
+ /**
183
+ * Represents an IN condition within a segment rule for feature flag evaluation.
184
+ */
185
+ export interface InSegmentCondition {
186
+ property: Property1;
187
+ operator: Operator1;
188
+ value: Value1;
189
+ [k: string]: unknown;
190
+ }
191
+ /**
192
+ * Represents a feature context for feature flag evaluation.
193
+ */
194
+ export interface FeatureContext {
195
+ key: Key3;
196
+ name: Name2;
197
+ enabled: Enabled;
198
+ value: Value2;
199
+ variants?: Variants;
200
+ priority?: FeaturePriority;
201
+ metadata?: FeatureMetadata;
202
+ [k: string]: unknown;
203
+ }
204
+ /**
205
+ * Represents a multivariate value for a feature flag.
206
+ */
207
+ export interface FeatureValue {
208
+ value: Value3;
209
+ weight: Weight;
210
+ priority: VariantPriority;
211
+ [k: string]: unknown;
212
+ }
213
+ /**
214
+ * Additional metadata associated with the feature.
215
+ */
216
+ export interface FeatureMetadata {
217
+ [k: string]: unknown;
218
+ }
219
+ /**
220
+ * Additional metadata associated with the segment.
221
+ */
222
+ export interface SegmentMetadata {
223
+ [k: string]: unknown;
224
+ }
225
+ /**
226
+ * Features to be evaluated in the context.
227
+ */
228
+ export interface Features {
229
+ [k: string]: FeatureContext;
230
+ }
@@ -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
+ }