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.
Files changed (117) hide show
  1. package/.github/workflows/conventional-commit.yml +29 -0
  2. package/.github/workflows/publish.yml +20 -17
  3. package/.github/workflows/pull_request.yaml +36 -33
  4. package/.github/workflows/release-please.yml +18 -0
  5. package/.gitmodules +1 -0
  6. package/.husky/pre-commit +1 -0
  7. package/.nvmrc +1 -0
  8. package/.prettierrc.cjs +9 -1
  9. package/.release-please-manifest.json +1 -0
  10. package/CHANGELOG.md +592 -0
  11. package/CODEOWNERS +1 -0
  12. package/README.md +0 -2
  13. package/build/cjs/flagsmith-engine/environments/models.d.ts +2 -1
  14. package/build/cjs/flagsmith-engine/environments/models.js +3 -1
  15. package/build/cjs/flagsmith-engine/environments/util.js +1 -1
  16. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.d.ts +230 -0
  17. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.js +8 -0
  18. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/mappers.d.ts +5 -0
  19. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/mappers.js +156 -0
  20. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/types.d.ts +216 -0
  21. package/build/cjs/flagsmith-engine/evaluation/evaluationContext/types.js +8 -0
  22. package/build/cjs/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.d.ts +68 -0
  23. package/build/cjs/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.js +8 -0
  24. package/build/cjs/flagsmith-engine/evaluation/models.d.ts +50 -0
  25. package/build/cjs/flagsmith-engine/evaluation/models.js +26 -0
  26. package/build/cjs/flagsmith-engine/features/models.js +1 -1
  27. package/build/cjs/flagsmith-engine/features/types.d.ts +5 -0
  28. package/build/cjs/flagsmith-engine/features/types.js +9 -0
  29. package/build/cjs/flagsmith-engine/features/util.d.ts +1 -0
  30. package/build/cjs/flagsmith-engine/features/util.js +5 -1
  31. package/build/cjs/flagsmith-engine/index.d.ts +61 -9
  32. package/build/cjs/flagsmith-engine/index.js +176 -56
  33. package/build/cjs/flagsmith-engine/segments/constants.d.ts +1 -0
  34. package/build/cjs/flagsmith-engine/segments/constants.js +2 -1
  35. package/build/cjs/flagsmith-engine/segments/evaluators.d.ts +41 -7
  36. package/build/cjs/flagsmith-engine/segments/evaluators.js +136 -24
  37. package/build/cjs/flagsmith-engine/segments/models.d.ts +9 -4
  38. package/build/cjs/flagsmith-engine/segments/models.js +115 -13
  39. package/build/cjs/flagsmith-engine/utils/hashing/index.d.ts +1 -1
  40. package/build/cjs/flagsmith-engine/utils/hashing/index.js +4 -4
  41. package/build/cjs/sdk/analytics.js +3 -1
  42. package/build/cjs/sdk/index.d.ts +1 -3
  43. package/build/cjs/sdk/index.js +63 -24
  44. package/build/cjs/sdk/models.d.ts +8 -1
  45. package/build/cjs/sdk/models.js +29 -1
  46. package/build/cjs/sdk/utils.d.ts +1 -0
  47. package/build/cjs/sdk/utils.js +14 -1
  48. package/build/esm/flagsmith-engine/environments/models.d.ts +2 -1
  49. package/build/esm/flagsmith-engine/environments/models.js +3 -1
  50. package/build/esm/flagsmith-engine/environments/util.js +1 -1
  51. package/build/esm/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.d.ts +230 -0
  52. package/build/esm/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.js +7 -0
  53. package/build/esm/flagsmith-engine/evaluation/evaluationContext/mappers.d.ts +5 -0
  54. package/build/esm/flagsmith-engine/evaluation/evaluationContext/mappers.js +152 -0
  55. package/build/esm/flagsmith-engine/evaluation/evaluationContext/types.d.ts +216 -0
  56. package/build/esm/flagsmith-engine/evaluation/evaluationContext/types.js +7 -0
  57. package/build/esm/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.d.ts +68 -0
  58. package/build/esm/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.js +7 -0
  59. package/build/esm/flagsmith-engine/evaluation/models.d.ts +50 -0
  60. package/build/esm/flagsmith-engine/evaluation/models.js +9 -0
  61. package/build/esm/flagsmith-engine/features/models.js +2 -2
  62. package/build/esm/flagsmith-engine/features/types.d.ts +5 -0
  63. package/build/esm/flagsmith-engine/features/types.js +6 -0
  64. package/build/esm/flagsmith-engine/features/util.d.ts +1 -0
  65. package/build/esm/flagsmith-engine/features/util.js +3 -0
  66. package/build/esm/flagsmith-engine/index.d.ts +61 -9
  67. package/build/esm/flagsmith-engine/index.js +161 -43
  68. package/build/esm/flagsmith-engine/segments/constants.d.ts +1 -0
  69. package/build/esm/flagsmith-engine/segments/constants.js +1 -0
  70. package/build/esm/flagsmith-engine/segments/evaluators.d.ts +41 -7
  71. package/build/esm/flagsmith-engine/segments/evaluators.js +137 -25
  72. package/build/esm/flagsmith-engine/segments/models.d.ts +9 -4
  73. package/build/esm/flagsmith-engine/segments/models.js +115 -13
  74. package/build/esm/flagsmith-engine/utils/hashing/index.d.ts +1 -1
  75. package/build/esm/flagsmith-engine/utils/hashing/index.js +2 -2
  76. package/build/esm/sdk/analytics.js +3 -1
  77. package/build/esm/sdk/index.d.ts +1 -3
  78. package/build/esm/sdk/index.js +63 -24
  79. package/build/esm/sdk/models.d.ts +8 -1
  80. package/build/esm/sdk/models.js +29 -1
  81. package/build/esm/sdk/utils.d.ts +1 -0
  82. package/build/esm/sdk/utils.js +12 -0
  83. package/flagsmith-engine/environments/models.ts +3 -1
  84. package/flagsmith-engine/environments/util.ts +2 -1
  85. package/flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.ts +247 -0
  86. package/flagsmith-engine/evaluation/evaluationContext/mappers.ts +204 -0
  87. package/flagsmith-engine/evaluation/evaluationContext/types.ts +233 -0
  88. package/flagsmith-engine/evaluation/evaluationResult/evaluationResult.types.ts +71 -0
  89. package/flagsmith-engine/evaluation/models.ts +96 -0
  90. package/flagsmith-engine/features/models.ts +3 -2
  91. package/flagsmith-engine/features/types.ts +5 -0
  92. package/flagsmith-engine/features/util.ts +4 -0
  93. package/flagsmith-engine/index.ts +229 -72
  94. package/flagsmith-engine/segments/constants.ts +1 -0
  95. package/flagsmith-engine/segments/evaluators.ts +178 -62
  96. package/flagsmith-engine/segments/models.ts +171 -23
  97. package/flagsmith-engine/utils/hashing/index.ts +2 -2
  98. package/package.json +13 -2
  99. package/release-please-config.json +62 -0
  100. package/sdk/analytics.ts +3 -1
  101. package/sdk/index.ts +89 -30
  102. package/sdk/models.ts +44 -2
  103. package/sdk/utils.ts +13 -0
  104. package/tests/engine/e2e/engine.test.ts +43 -38
  105. package/tests/engine/unit/engine.test.ts +306 -60
  106. package/tests/engine/unit/mappers.test.ts +353 -0
  107. package/tests/engine/unit/segments/segment_evaluators.test.ts +391 -49
  108. package/tests/engine/unit/segments/segments_model.test.ts +85 -0
  109. package/tests/engine/unit/utils/utils.test.ts +7 -7
  110. package/tests/engine/unit/utils.ts +1 -1
  111. package/tests/sdk/analytics.test.ts +6 -1
  112. package/tests/sdk/data/environment.json +1 -0
  113. package/tests/sdk/flagsmith-environment-flags.test.ts +28 -0
  114. package/tests/sdk/flagsmith-identity-flags.test.ts +11 -2
  115. package/tests/sdk/flagsmith.test.ts +190 -3
  116. package/tests/sdk/offline-handlers.test.ts +3 -1
  117. package/vitest.config.esm.ts +34 -0
@@ -46,10 +46,15 @@ class Flag extends BaseFlag {
46
46
  * The programmatic name for this feature, unique per Flagsmith project.
47
47
  */
48
48
  featureName;
49
+ /**
50
+ * The reason for this feature, unique per Flagsmith project.
51
+ */
52
+ reason;
49
53
  constructor(params) {
50
54
  super(params.value, params.enabled, !!params.isDefault);
51
55
  this.featureId = params.featureId;
52
56
  this.featureName = params.featureName;
57
+ this.reason = params.reason;
53
58
  }
54
59
  static fromFeatureStateModel(fsm, identityId) {
55
60
  return new Flag({
@@ -64,7 +69,8 @@ class Flag extends BaseFlag {
64
69
  enabled: flagData['enabled'],
65
70
  value: flagData['feature_state_value'] ?? flagData['value'],
66
71
  featureId: flagData['feature']['id'],
67
- featureName: flagData['feature']['name']
72
+ featureName: flagData['feature']['name'],
73
+ reason: flagData['feature']['reason']
68
74
  });
69
75
  }
70
76
  }
@@ -78,6 +84,28 @@ class Flags {
78
84
  this.defaultFlagHandler = data.defaultFlagHandler;
79
85
  this.analyticsProcessor = data.analyticsProcessor;
80
86
  }
87
+ static fromEvaluationResult(evaluationResult, defaultFlagHandler, analyticsProcessor) {
88
+ const flags = {};
89
+ for (const flag of Object.values(evaluationResult.flags)) {
90
+ const flagMetadataId = flag.metadata?.id;
91
+ if (!flagMetadataId) {
92
+ throw new Error(`FlagResult metadata.id is missing for feature "${flag.name}". ` +
93
+ `This indicates a bug in the SDK, please report it.`);
94
+ }
95
+ flags[flag.name] = new Flag({
96
+ enabled: flag.enabled,
97
+ value: flag.value ?? null,
98
+ featureId: flagMetadataId,
99
+ featureName: flag.name,
100
+ reason: flag.reason
101
+ });
102
+ }
103
+ return new Flags({
104
+ flags: flags,
105
+ defaultFlagHandler: defaultFlagHandler,
106
+ analyticsProcessor: analyticsProcessor
107
+ });
108
+ }
81
109
  static fromFeatureStateModels(data) {
82
110
  const flags = {};
83
111
  for (const fs of data.featureStates) {
@@ -56,4 +56,5 @@ export declare class Deferred<T> {
56
56
  resolve(value: T | PromiseLike<T>): void;
57
57
  reject(reason?: unknown): void;
58
58
  }
59
+ export declare function getUserAgent(): string;
59
60
  export {};
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deferred = exports.retryFetch = exports.delay = exports.generateIdentitiesData = exports.isTraitConfig = void 0;
3
+ exports.getUserAgent = exports.Deferred = exports.retryFetch = exports.delay = exports.generateIdentitiesData = exports.isTraitConfig = void 0;
4
+ const FLAGSMITH_USER_AGENT = 'flagsmith-nodejs-sdk';
5
+ const FLAGSMITH_UNKNOWN_VERSION = 'unknown';
4
6
  function isTraitConfig(traitValue) {
5
7
  return !!traitValue && typeof traitValue == 'object' && traitValue.value !== undefined;
6
8
  }
@@ -93,3 +95,14 @@ class Deferred {
93
95
  }
94
96
  }
95
97
  exports.Deferred = Deferred;
98
+ function getUserAgent() {
99
+ try {
100
+ const packageJson = require('../package.json');
101
+ const version = packageJson?.version;
102
+ return version ? `${FLAGSMITH_USER_AGENT}/${version}` : FLAGSMITH_UNKNOWN_VERSION;
103
+ }
104
+ catch {
105
+ return FLAGSMITH_UNKNOWN_VERSION;
106
+ }
107
+ }
108
+ exports.getUserAgent = getUserAgent;
@@ -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
  }
@@ -24,9 +24,11 @@ export class EnvironmentModel {
24
24
  project;
25
25
  featureStates = [];
26
26
  identityOverrides = [];
27
- constructor(id, apiKey, project) {
27
+ name;
28
+ constructor(id, apiKey, project, name) {
28
29
  this.id = id;
29
30
  this.apiKey = apiKey;
30
31
  this.project = project;
32
+ this.name = name;
31
33
  }
32
34
  }
@@ -5,7 +5,7 @@ import { EnvironmentAPIKeyModel, EnvironmentModel } from './models.js';
5
5
  export function buildEnvironmentModel(environmentJSON) {
6
6
  const project = buildProjectModel(environmentJSON.project);
7
7
  const featureStates = environmentJSON.feature_states.map((fs) => buildFeatureStateModel(fs));
8
- const environmentModel = new EnvironmentModel(environmentJSON.id, environmentJSON.api_key, project);
8
+ const environmentModel = new EnvironmentModel(environmentJSON.id, environmentJSON.api_key, project, environmentJSON.name);
9
9
  environmentModel.featureStates = featureStates;
10
10
  if (!!environmentJSON.identity_overrides) {
11
11
  environmentModel.identityOverrides = environmentJSON.identity_overrides.map((identityData) => 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,7 @@
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
+ export {};
@@ -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
+ }