flagsmith-nodejs 2.5.1 → 3.0.0

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 (108) hide show
  1. package/.github/workflows/publish.yml +25 -0
  2. package/examples/README.md +3 -0
  3. package/flagsmith-engine/identities/models.ts +3 -1
  4. package/flagsmith-engine/segments/evaluators.ts +4 -3
  5. package/package.json +1 -1
  6. package/sdk/index.ts +2 -2
  7. package/sdk/utils.ts +13 -7
  8. package/tests/engine/unit/segments/segment_evaluators.test.ts +46 -6
  9. package/tests/engine/unit/utils/utils.test.ts +0 -1
  10. package/tests/sdk/flagsmith.test.ts +27 -1
  11. package/.vscode/launch.json +0 -18
  12. package/build/flagsmith-engine/environments/integrations/models.d.ts +0 -4
  13. package/build/flagsmith-engine/environments/integrations/models.js +0 -11
  14. package/build/flagsmith-engine/environments/models.d.ts +0 -25
  15. package/build/flagsmith-engine/environments/models.js +0 -29
  16. package/build/flagsmith-engine/environments/util.d.ts +0 -3
  17. package/build/flagsmith-engine/environments/util.js +0 -21
  18. package/build/flagsmith-engine/features/constants.d.ts +0 -4
  19. package/build/flagsmith-engine/features/constants.js +0 -7
  20. package/build/flagsmith-engine/features/models.d.ts +0 -37
  21. package/build/flagsmith-engine/features/models.js +0 -122
  22. package/build/flagsmith-engine/features/util.d.ts +0 -4
  23. package/build/flagsmith-engine/features/util.js +0 -27
  24. package/build/flagsmith-engine/identities/models.d.ts +0 -15
  25. package/build/flagsmith-engine/identities/models.js +0 -112
  26. package/build/flagsmith-engine/identities/traits/models.d.ts +0 -5
  27. package/build/flagsmith-engine/identities/traits/models.js +0 -11
  28. package/build/flagsmith-engine/identities/util.d.ts +0 -4
  29. package/build/flagsmith-engine/identities/util.js +0 -46
  30. package/build/flagsmith-engine/index.d.ts +0 -15
  31. package/build/flagsmith-engine/index.js +0 -129
  32. package/build/flagsmith-engine/organisations/models.d.ts +0 -9
  33. package/build/flagsmith-engine/organisations/models.js +0 -21
  34. package/build/flagsmith-engine/organisations/util.d.ts +0 -2
  35. package/build/flagsmith-engine/organisations/util.js +0 -8
  36. package/build/flagsmith-engine/projects/models.d.ts +0 -10
  37. package/build/flagsmith-engine/projects/models.js +0 -14
  38. package/build/flagsmith-engine/projects/util.d.ts +0 -2
  39. package/build/flagsmith-engine/projects/util.js +0 -15
  40. package/build/flagsmith-engine/segments/constants.d.ts +0 -34
  41. package/build/flagsmith-engine/segments/constants.js +0 -39
  42. package/build/flagsmith-engine/segments/evaluators.d.ts +0 -7
  43. package/build/flagsmith-engine/segments/evaluators.js +0 -44
  44. package/build/flagsmith-engine/segments/models.d.ts +0 -37
  45. package/build/flagsmith-engine/segments/models.js +0 -144
  46. package/build/flagsmith-engine/segments/util.d.ts +0 -6
  47. package/build/flagsmith-engine/segments/util.js +0 -33
  48. package/build/flagsmith-engine/utils/collections.d.ts +0 -3
  49. package/build/flagsmith-engine/utils/collections.js +0 -26
  50. package/build/flagsmith-engine/utils/errors.d.ts +0 -2
  51. package/build/flagsmith-engine/utils/errors.js +0 -26
  52. package/build/flagsmith-engine/utils/hashing/index.d.ts +0 -9
  53. package/build/flagsmith-engine/utils/hashing/index.js +0 -60
  54. package/build/flagsmith-engine/utils/index.d.ts +0 -1
  55. package/build/flagsmith-engine/utils/index.js +0 -17
  56. package/build/index.d.ts +0 -2
  57. package/build/index.js +0 -24
  58. package/build/sdk/analytics.d.ts +0 -31
  59. package/build/sdk/analytics.js +0 -116
  60. package/build/sdk/errors.d.ts +0 -4
  61. package/build/sdk/errors.js +0 -34
  62. package/build/sdk/index.d.ts +0 -120
  63. package/build/sdk/index.js +0 -528
  64. package/build/sdk/models.d.ts +0 -55
  65. package/build/sdk/models.js +0 -149
  66. package/build/sdk/polling_manager.d.ts +0 -9
  67. package/build/sdk/polling_manager.js +0 -72
  68. package/build/sdk/types.d.ts +0 -27
  69. package/build/sdk/types.js +0 -2
  70. package/build/sdk/utils.d.ts +0 -12
  71. package/build/sdk/utils.js +0 -93
  72. package/examples/api-proxy/.babelrc +0 -3
  73. package/examples/api-proxy/.eslintrc +0 -8
  74. package/examples/api-proxy/README.md +0 -12
  75. package/examples/api-proxy/package-lock.json +0 -10889
  76. package/examples/api-proxy/package.json +0 -57
  77. package/examples/api-proxy/src/api/index.js +0 -42
  78. package/examples/api-proxy/src/index.js +0 -29
  79. package/examples/basic/.babelrc +0 -3
  80. package/examples/basic/.eslintrc +0 -8
  81. package/examples/basic/README.md +0 -10
  82. package/examples/basic/package-lock.json +0 -10779
  83. package/examples/basic/package.json +0 -56
  84. package/examples/basic/src/api/index.js +0 -33
  85. package/examples/basic/src/index.js +0 -29
  86. package/examples/caching/.babelrc +0 -3
  87. package/examples/caching/.eslintrc +0 -8
  88. package/examples/caching/README.md +0 -9
  89. package/examples/caching/package-lock.json +0 -6750
  90. package/examples/caching/package.json +0 -56
  91. package/examples/caching/src/api/index.js +0 -38
  92. package/examples/caching/src/index.js +0 -29
  93. package/examples/custom-fetch-agent/.babelrc +0 -3
  94. package/examples/custom-fetch-agent/.eslintrc +0 -8
  95. package/examples/custom-fetch-agent/README.md +0 -12
  96. package/examples/custom-fetch-agent/package-lock.json +0 -6750
  97. package/examples/custom-fetch-agent/package.json +0 -56
  98. package/examples/custom-fetch-agent/src/api/index.js +0 -34
  99. package/examples/custom-fetch-agent/src/index.js +0 -29
  100. package/examples/local-evaluation/.babelrc +0 -3
  101. package/examples/local-evaluation/.eslintrc +0 -8
  102. package/examples/local-evaluation/README.md +0 -18
  103. package/examples/local-evaluation/package-lock.json +0 -6668
  104. package/examples/local-evaluation/package.json +0 -56
  105. package/examples/local-evaluation/src/api/index.js +0 -39
  106. package/examples/local-evaluation/src/index.js +0 -29
  107. package/tests/engine/engine-tests/engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json +0 -12591
  108. package/tests/engine/engine-tests/engine-test-data/readme.md +0 -30
@@ -0,0 +1,25 @@
1
+ name: Publish NPM Package
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - '*'
7
+
8
+ jobs:
9
+ package:
10
+ runs-on: ubuntu-latest
11
+ name: Publish NPM Package
12
+
13
+ steps:
14
+ - name: Cloning repo
15
+ uses: actions/checkout@v3
16
+
17
+ - uses: actions/setup-node@v3
18
+ with:
19
+ node-version: '16.x'
20
+ registry-url: 'https://registry.npmjs.org'
21
+
22
+ - run: npm ci
23
+ - run: npm publish
24
+ env:
25
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,3 @@
1
+ # Flagsmith NodeJS Examples
2
+
3
+ Check out our [NodeJS Examples repository](https://github.com/Flagsmith/flagsmith-nodejs-examples).
@@ -19,7 +19,8 @@ export class IdentityModel {
19
19
  identityFeatures: IdentityFeaturesList,
20
20
  environmentApiKey: string,
21
21
  identifier: string,
22
- identityUuid?: string
22
+ identityUuid?: string,
23
+ djangoID?: number
23
24
  ) {
24
25
  this.identityUuid = identityUuid || uuidv4();
25
26
  this.createdDate = Date.parse(created_date) || Date.now();
@@ -27,6 +28,7 @@ export class IdentityModel {
27
28
  this.identityFeatures = new IdentityFeaturesList(...identityFeatures);
28
29
  this.environmentApiKey = environmentApiKey;
29
30
  this.identifier = identifier;
31
+ this.djangoID = djangoID;
30
32
  }
31
33
 
32
34
  get compositeKey() {
@@ -27,7 +27,7 @@ export function evaluateIdentityInSegment(
27
27
  overrideTraits || identity.identityTraits,
28
28
  rule,
29
29
  segment.id,
30
- identity.compositeKey
30
+ identity.djangoID || identity.compositeKey
31
31
  )
32
32
  ).length === segment.rules.length
33
33
  );
@@ -62,7 +62,8 @@ export function traitsMatchSegmentCondition(
62
62
  identityId: number | string
63
63
  ): boolean {
64
64
  if (condition.operator == PERCENTAGE_SPLIT) {
65
- return getHashedPercentateForObjIds([segmentId, identityId]) <= parseFloat(String(condition.value));
65
+ var hashedPercentage = getHashedPercentateForObjIds([segmentId, identityId]);
66
+ return hashedPercentage <= parseFloat(String(condition.value));
66
67
  }
67
68
  const traits = identityTraits.filter(t => t.traitKey === condition.property_);
68
69
  const trait = traits.length > 0 ? traits[0] : undefined;
@@ -73,4 +74,4 @@ export function traitsMatchSegmentCondition(
73
74
  }
74
75
  return trait ? condition.matchesTraitValue(trait.traitValue) : false;
75
76
 
76
- }
77
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "2.5.1",
3
+ "version": "3.0.0",
4
4
  "description": "Flagsmith lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.",
5
5
  "main": "build/index.js",
6
6
  "repository": {
package/sdk/index.ts CHANGED
@@ -283,11 +283,11 @@ export class Flagsmith {
283
283
  {
284
284
  agent: this.agent,
285
285
  method: method,
286
- timeout: this.requestTimeoutMs || undefined,
287
286
  body: JSON.stringify(body),
288
287
  headers: headers
289
288
  },
290
- this.retries
289
+ this.retries,
290
+ this.requestTimeoutMs || undefined,
291
291
  );
292
292
 
293
293
  if (data.status !== 200) {
package/sdk/utils.ts CHANGED
@@ -23,22 +23,28 @@ export const retryFetch = (
23
23
  timeout?: number // set an overall timeout for this function
24
24
  ): Promise<Response> => {
25
25
  return new Promise((resolve, reject) => {
26
- // check for timeout
27
- if (timeout) setTimeout(() => reject('error: timeout'), timeout);
28
-
29
- const wrapper = (n: number) => {
30
- fetch(url, fetchOptions)
26
+ const retryWrapper = (n: number) => {
27
+ requestWrapper()
31
28
  .then(res => resolve(res))
32
29
  .catch(async err => {
33
30
  if (n > 0) {
34
31
  await delay(1000);
35
- wrapper(--n);
32
+ retryWrapper(--n);
36
33
  } else {
37
34
  reject(err);
38
35
  }
39
36
  });
40
37
  };
41
38
 
42
- wrapper(retries);
39
+ const requestWrapper = (): Promise<Response> => {
40
+ return new Promise((resolve, reject) => {
41
+ if (timeout) setTimeout(() => reject('error: timeout'), timeout);
42
+ return fetch(url, fetchOptions)
43
+ .then(res => resolve(res))
44
+ .catch(err => reject(err))
45
+ })
46
+ }
47
+
48
+ retryWrapper(retries);
43
49
  });
44
50
  };
@@ -1,11 +1,21 @@
1
1
  import {
2
+ ALL_RULE,
2
3
  CONDITION_OPERATORS,
4
+ PERCENTAGE_SPLIT,
3
5
  } from '../../../../flagsmith-engine/segments/constants';
4
- import {
5
- SegmentConditionModel,
6
- } from '../../../../flagsmith-engine/segments/models';
7
- import {traitsMatchSegmentCondition} from "../../../../flagsmith-engine/segments/evaluators";
8
- import {TraitModel} from "../../../../flagsmith-engine";
6
+ import {SegmentConditionModel} from '../../../../flagsmith-engine/segments/models';
7
+ import {traitsMatchSegmentCondition, evaluateIdentityInSegment} from "../../../../flagsmith-engine/segments/evaluators";
8
+ import {TraitModel, IdentityModel} from "../../../../flagsmith-engine";
9
+ import {environment} from "../utils";
10
+ import { buildSegmentModel } from '../../../../flagsmith-engine/segments/util';
11
+ import { getHashedPercentateForObjIds } from '../../../../flagsmith-engine/utils/hashing';
12
+
13
+
14
+ // todo: work out how to implement this in a test function or before hook
15
+ jest.mock('../../../../flagsmith-engine/utils/hashing', () => ({
16
+ getHashedPercentateForObjIds: jest.fn(() => 1)
17
+ }));
18
+
9
19
 
10
20
  let traitExistenceTestCases: [string, string | null | undefined, string | null | undefined, TraitModel [],boolean][] = [
11
21
  [CONDITION_OPERATORS.IS_SET,'foo', null,[] , false],
@@ -24,4 +34,34 @@ test('test_traits_match_segment_condition_for_trait_existence_operators', () =>
24
34
  traitsMatchSegmentCondition (traits, segmentModel, 'any','any')
25
35
  ).toBe(expectedResult);
26
36
  }
27
- });
37
+ });
38
+
39
+
40
+ test('evaluateIdentityInSegment uses django ID for hashed percentage when present', () => {
41
+ var identityModel = new IdentityModel(Date.now().toString(), [], [], environment().apiKey, 'identity_1', undefined, 1);
42
+ const segmentDefinition = {
43
+ id: 1,
44
+ name: 'percentage_split_segment',
45
+ rules: [
46
+ {
47
+ type: ALL_RULE,
48
+ conditions: [
49
+ {
50
+ operator: PERCENTAGE_SPLIT,
51
+ property_: null,
52
+ value: "10"
53
+ }
54
+ ],
55
+ rules: []
56
+ }
57
+ ],
58
+ feature_states: []
59
+ };
60
+ const segmentModel = buildSegmentModel(segmentDefinition);
61
+
62
+ var result = evaluateIdentityInSegment(identityModel, segmentModel);
63
+
64
+ expect(result).toBe(true);
65
+ expect(getHashedPercentateForObjIds).toHaveBeenCalledTimes(1)
66
+ expect(getHashedPercentateForObjIds).toHaveBeenCalledWith([segmentModel.id, identityModel.djangoID])
67
+ });
@@ -42,7 +42,6 @@ describe('getHashedPercentageForObjIds', () => {
42
42
  let objectIdPairs = Array.from(Array(testSample).keys()).flatMap(d => Array.from(Array(testSample).keys()).map(e => [d, e].flat()))
43
43
 
44
44
  // When
45
- console.log(objectIdPairs);
46
45
  let values = objectIdPairs.map((objIds) => getHashedPercentateForObjIds(objIds));
47
46
 
48
47
  // Then
@@ -172,6 +172,27 @@ test('test_default_flag_used_after_multiple_API_errors', async () => {
172
172
  expect(flag.value).toBe(defaultFlag.value);
173
173
  });
174
174
 
175
+ test('default flag handler used when timeout occurs', async () => {
176
+ // @ts-ignore
177
+ fetch.mockReturnValue(Promise.resolve(sleep(10000)));
178
+
179
+ const defaultFlag = new DefaultFlag('some-default-value', true);
180
+
181
+ const defaultFlagHandler = (featureName: string) => defaultFlag;
182
+
183
+ const flg = new Flagsmith({
184
+ environmentKey: 'key',
185
+ defaultFlagHandler: defaultFlagHandler,
186
+ requestTimeoutSeconds: 0.1,
187
+ });
188
+
189
+ const flags = await flg.getEnvironmentFlags();
190
+ const flag = flags.getFlag('some_feature');
191
+ expect(flag.isDefault).toBe(true);
192
+ expect(flag.enabled).toBe(defaultFlag.enabled);
193
+ expect(flag.value).toBe(defaultFlag.value);
194
+ })
195
+
175
196
  test('test_throws_when_no_identity_flags_returned_due_to_error', async () => {
176
197
  // @ts-ignore
177
198
  fetch.mockReturnValue(Promise.resolve(new Response('bad data')));
@@ -245,7 +266,6 @@ test('getIdentitySegments throws error if identifier is empty string', () => {
245
266
  })
246
267
 
247
268
 
248
-
249
269
  async function wipeFeatureStateUUIDs (enviromentModel: EnvironmentModel) {
250
270
  // TODO: this has been pulled out of tests above as a helper function.
251
271
  // I'm not entirely sure why it's necessary, however, we should look to remove.
@@ -264,3 +284,9 @@ async function wipeFeatureStateUUIDs (enviromentModel: EnvironmentModel) {
264
284
  })
265
285
  })
266
286
  }
287
+
288
+ function sleep(ms: number) {
289
+ return new Promise((resolve) => {
290
+ setTimeout(resolve, ms);
291
+ });
292
+ }
@@ -1,18 +0,0 @@
1
- {
2
- "version": "1.0.0",
3
- "configurations": [
4
- {
5
- "type": "node",
6
- "request": "launch",
7
- "name": "Jest: current file",
8
- //"env": { "NODE_ENV": "test" },
9
- "program": "${workspaceFolder}/node_modules/.bin/jest",
10
- "args": ["${file}", "--config", "jest.config.js"],
11
- "console": "integratedTerminal",
12
- "disableOptimisticBPs": true,
13
- "windows": {
14
- "program": "${workspaceFolder}/node_modules/jest/bin/jest"
15
- }
16
- }
17
- ]
18
- }
@@ -1,4 +0,0 @@
1
- export declare class IntegrationModel {
2
- api_key?: string;
3
- base_url?: string;
4
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IntegrationModel = void 0;
4
- var IntegrationModel = /** @class */ (function () {
5
- function IntegrationModel() {
6
- this.api_key = undefined;
7
- this.base_url = undefined;
8
- }
9
- return IntegrationModel;
10
- }());
11
- exports.IntegrationModel = IntegrationModel;
@@ -1,25 +0,0 @@
1
- import { FeatureStateModel } from '../features/models';
2
- import { ProjectModel } from '../projects/models';
3
- import { IntegrationModel } from './integrations/models';
4
- export declare class EnvironmentAPIKeyModel {
5
- id: number;
6
- key: string;
7
- createdAt: number;
8
- name: string;
9
- clientApiKey: string;
10
- expiresAt?: number;
11
- active: boolean;
12
- constructor(id: number, key: string, createdAt: number, name: string, clientApiKey: string, expiresAt?: number);
13
- isValid(): boolean;
14
- }
15
- export declare class EnvironmentModel {
16
- id: number;
17
- apiKey: string;
18
- project: ProjectModel;
19
- featureStates: FeatureStateModel[];
20
- amplitude_config?: IntegrationModel;
21
- segment_config?: IntegrationModel;
22
- mixpanel_config?: IntegrationModel;
23
- heap_config?: IntegrationModel;
24
- constructor(id: number, apiKey: string, project: ProjectModel);
25
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EnvironmentModel = exports.EnvironmentAPIKeyModel = void 0;
4
- var EnvironmentAPIKeyModel = /** @class */ (function () {
5
- function EnvironmentAPIKeyModel(id, key, createdAt, name, clientApiKey, expiresAt) {
6
- this.active = true;
7
- this.id = id;
8
- this.key = key;
9
- this.createdAt = createdAt;
10
- this.name = name;
11
- this.clientApiKey = clientApiKey;
12
- this.expiresAt = expiresAt;
13
- }
14
- EnvironmentAPIKeyModel.prototype.isValid = function () {
15
- return !!this.active && (!this.expiresAt || this.expiresAt > Date.now());
16
- };
17
- return EnvironmentAPIKeyModel;
18
- }());
19
- exports.EnvironmentAPIKeyModel = EnvironmentAPIKeyModel;
20
- var EnvironmentModel = /** @class */ (function () {
21
- function EnvironmentModel(id, apiKey, project) {
22
- this.featureStates = [];
23
- this.id = id;
24
- this.apiKey = apiKey;
25
- this.project = project;
26
- }
27
- return EnvironmentModel;
28
- }());
29
- exports.EnvironmentModel = EnvironmentModel;
@@ -1,3 +0,0 @@
1
- import { EnvironmentAPIKeyModel, EnvironmentModel } from './models';
2
- export declare function buildEnvironmentModel(environmentJSON: any): EnvironmentModel;
3
- export declare function buildEnvironmentAPIKeyModel(apiKeyJSON: any): EnvironmentAPIKeyModel;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildEnvironmentAPIKeyModel = exports.buildEnvironmentModel = void 0;
4
- var util_1 = require("../features/util");
5
- var util_2 = require("../projects/util");
6
- var models_1 = require("./models");
7
- function buildEnvironmentModel(environmentJSON) {
8
- var project = (0, util_2.buildProjectModel)(environmentJSON.project);
9
- var featureStates = environmentJSON.feature_states.map(function (fs) {
10
- return (0, util_1.buildFeatureStateModel)(fs);
11
- });
12
- var environmentModel = new models_1.EnvironmentModel(environmentJSON.id, environmentJSON.api_key, project);
13
- environmentModel.featureStates = featureStates;
14
- return environmentModel;
15
- }
16
- exports.buildEnvironmentModel = buildEnvironmentModel;
17
- function buildEnvironmentAPIKeyModel(apiKeyJSON) {
18
- var model = new models_1.EnvironmentAPIKeyModel(apiKeyJSON.id, apiKeyJSON.key, Date.parse(apiKeyJSON.created_at), apiKeyJSON.name, apiKeyJSON.client_api_key);
19
- return model;
20
- }
21
- exports.buildEnvironmentAPIKeyModel = buildEnvironmentAPIKeyModel;
@@ -1,4 +0,0 @@
1
- export declare const CONSTANTS: {
2
- STANDARD: string;
3
- MULTIVARIATE: string;
4
- };
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONSTANTS = void 0;
4
- exports.CONSTANTS = {
5
- STANDARD: 'STANDARD',
6
- MULTIVARIATE: 'MULTIVARIATE'
7
- };
@@ -1,37 +0,0 @@
1
- export declare class FeatureModel {
2
- id: number;
3
- name: string;
4
- type: string;
5
- constructor(id: number, name: string, type: string);
6
- eq(other: FeatureModel): boolean;
7
- }
8
- export declare class MultivariateFeatureOptionModel {
9
- value: any;
10
- id: number | undefined;
11
- constructor(value: any, id?: number);
12
- }
13
- export declare class MultivariateFeatureStateValueModel {
14
- multivariateFeatureOption: MultivariateFeatureOptionModel;
15
- percentageAllocation: number;
16
- id: number;
17
- mvFsValueUuid: string;
18
- constructor(multivariate_feature_option: MultivariateFeatureOptionModel, percentage_allocation: number, id: number, mvFsValueUuid?: string);
19
- }
20
- export declare class FeatureStateModel {
21
- feature: FeatureModel;
22
- enabled: boolean;
23
- djangoID: number;
24
- featurestateUUID: string;
25
- featureSegment?: FeatureSegment;
26
- private value;
27
- multivariateFeatureStateValues: MultivariateFeatureStateValueModel[];
28
- constructor(feature: FeatureModel, enabled: boolean, djangoID: number, value?: any, featurestateUuid?: string);
29
- setValue(value: any): void;
30
- getValue(identityId?: number | string): any;
31
- isHigherSegmentPriority(other: FeatureStateModel): boolean;
32
- getMultivariateValue(identityID: number | string): any;
33
- }
34
- export declare class FeatureSegment {
35
- priority: number;
36
- constructor(priority: number);
37
- }
@@ -1,122 +0,0 @@
1
- "use strict";
2
- var __values = (this && this.__values) || function(o) {
3
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
- if (m) return m.call(o);
5
- if (o && typeof o.length === "number") return {
6
- next: function () {
7
- if (o && i >= o.length) o = void 0;
8
- return { value: o && o[i++], done: !o };
9
- }
10
- };
11
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.FeatureSegment = exports.FeatureStateModel = exports.MultivariateFeatureStateValueModel = exports.MultivariateFeatureOptionModel = exports.FeatureModel = void 0;
15
- var uuid_1 = require("uuid");
16
- var hashing_1 = require("../utils/hashing");
17
- var FeatureModel = /** @class */ (function () {
18
- function FeatureModel(id, name, type) {
19
- this.id = id;
20
- this.name = name;
21
- this.type = type;
22
- }
23
- FeatureModel.prototype.eq = function (other) {
24
- return !!other && this.id === other.id;
25
- };
26
- return FeatureModel;
27
- }());
28
- exports.FeatureModel = FeatureModel;
29
- var MultivariateFeatureOptionModel = /** @class */ (function () {
30
- function MultivariateFeatureOptionModel(value, id) {
31
- this.value = value;
32
- this.id = id;
33
- }
34
- return MultivariateFeatureOptionModel;
35
- }());
36
- exports.MultivariateFeatureOptionModel = MultivariateFeatureOptionModel;
37
- var MultivariateFeatureStateValueModel = /** @class */ (function () {
38
- function MultivariateFeatureStateValueModel(multivariate_feature_option, percentage_allocation, id, mvFsValueUuid) {
39
- this.mvFsValueUuid = (0, uuid_1.v4)();
40
- this.id = id;
41
- this.percentageAllocation = percentage_allocation;
42
- this.multivariateFeatureOption = multivariate_feature_option;
43
- this.mvFsValueUuid = mvFsValueUuid || this.mvFsValueUuid;
44
- }
45
- return MultivariateFeatureStateValueModel;
46
- }());
47
- exports.MultivariateFeatureStateValueModel = MultivariateFeatureStateValueModel;
48
- var FeatureStateModel = /** @class */ (function () {
49
- function FeatureStateModel(feature, enabled, djangoID, value, featurestateUuid) {
50
- if (featurestateUuid === void 0) { featurestateUuid = (0, uuid_1.v4)(); }
51
- this.featurestateUUID = (0, uuid_1.v4)();
52
- this.multivariateFeatureStateValues = [];
53
- this.feature = feature;
54
- this.enabled = enabled;
55
- this.djangoID = djangoID;
56
- this.value = value;
57
- this.featurestateUUID = featurestateUuid;
58
- }
59
- FeatureStateModel.prototype.setValue = function (value) {
60
- this.value = value;
61
- };
62
- FeatureStateModel.prototype.getValue = function (identityId) {
63
- if (!!identityId && this.multivariateFeatureStateValues.length > 0) {
64
- return this.getMultivariateValue(identityId);
65
- }
66
- return this.value;
67
- };
68
- /*
69
- Returns `True` if `this` is higher segment priority than `other`
70
- (i.e. has lower value for featureSegment.priority)
71
- NOTE:
72
- A segment will be considered higher priority only if:
73
- 1. `other` does not have a feature segment(i.e: it is an environment feature state or it's a
74
- feature state with feature segment but from an old document that does not have `featureSegment.priority`)
75
- but `this` does.
76
- 2. `other` have a feature segment with high priority
77
- */
78
- FeatureStateModel.prototype.isHigherSegmentPriority = function (other) {
79
- if (!other.featureSegment || !this.featureSegment) {
80
- return !!this.featureSegment && !other.featureSegment;
81
- }
82
- return this.featureSegment.priority < other.featureSegment.priority;
83
- };
84
- FeatureStateModel.prototype.getMultivariateValue = function (identityID) {
85
- var e_1, _a;
86
- var percentageValue = (0, hashing_1.getHashedPercentateForObjIds)([
87
- this.djangoID || this.featurestateUUID,
88
- identityID
89
- ]);
90
- var startPercentage = 0;
91
- var sortedF = this.multivariateFeatureStateValues.sort(function (a, b) {
92
- return a.id - b.id;
93
- });
94
- try {
95
- for (var sortedF_1 = __values(sortedF), sortedF_1_1 = sortedF_1.next(); !sortedF_1_1.done; sortedF_1_1 = sortedF_1.next()) {
96
- var myValue = sortedF_1_1.value;
97
- var limit = myValue.percentageAllocation + startPercentage;
98
- if (startPercentage <= percentageValue && percentageValue < limit) {
99
- return myValue.multivariateFeatureOption.value;
100
- }
101
- startPercentage = limit;
102
- }
103
- }
104
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
105
- finally {
106
- try {
107
- if (sortedF_1_1 && !sortedF_1_1.done && (_a = sortedF_1.return)) _a.call(sortedF_1);
108
- }
109
- finally { if (e_1) throw e_1.error; }
110
- }
111
- return this.value;
112
- };
113
- return FeatureStateModel;
114
- }());
115
- exports.FeatureStateModel = FeatureStateModel;
116
- var FeatureSegment = /** @class */ (function () {
117
- function FeatureSegment(priority) {
118
- this.priority = priority;
119
- }
120
- return FeatureSegment;
121
- }());
122
- exports.FeatureSegment = FeatureSegment;
@@ -1,4 +0,0 @@
1
- import { FeatureModel, FeatureSegment, FeatureStateModel } from './models';
2
- export declare function buildFeatureModel(featuresModelJSON: any): FeatureModel;
3
- export declare function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStateModel;
4
- export declare function buildFeatureSegment(featureSegmentJSON: any): FeatureSegment;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildFeatureSegment = exports.buildFeatureStateModel = exports.buildFeatureModel = void 0;
4
- var models_1 = require("./models");
5
- function buildFeatureModel(featuresModelJSON) {
6
- return new models_1.FeatureModel(featuresModelJSON.id, featuresModelJSON.name, featuresModelJSON.type);
7
- }
8
- exports.buildFeatureModel = buildFeatureModel;
9
- function buildFeatureStateModel(featuresStateModelJSON) {
10
- var featureStateModel = new models_1.FeatureStateModel(buildFeatureModel(featuresStateModelJSON.feature), featuresStateModelJSON.enabled, featuresStateModelJSON.django_id, featuresStateModelJSON.feature_state_value, featuresStateModelJSON.uuid);
11
- featureStateModel.featureSegment = featuresStateModelJSON.feature_segment ?
12
- buildFeatureSegment(featuresStateModelJSON.feature_segment) :
13
- undefined;
14
- var multivariateFeatureStateValues = featuresStateModelJSON.multivariate_feature_state_values
15
- ? featuresStateModelJSON.multivariate_feature_state_values.map(function (fsv) {
16
- var featureOption = new models_1.MultivariateFeatureOptionModel(fsv.multivariate_feature_option.value, fsv.multivariate_feature_option.id);
17
- return new models_1.MultivariateFeatureStateValueModel(featureOption, fsv.percentage_allocation, fsv.id);
18
- })
19
- : [];
20
- featureStateModel.multivariateFeatureStateValues = multivariateFeatureStateValues;
21
- return featureStateModel;
22
- }
23
- exports.buildFeatureStateModel = buildFeatureStateModel;
24
- function buildFeatureSegment(featureSegmentJSON) {
25
- return new models_1.FeatureSegment(featureSegmentJSON.priority);
26
- }
27
- exports.buildFeatureSegment = buildFeatureSegment;
@@ -1,15 +0,0 @@
1
- import { IdentityFeaturesList } from '../utils/collections';
2
- import { TraitModel } from './traits/models';
3
- export declare class IdentityModel {
4
- identifier: string;
5
- environmentApiKey: string;
6
- createdDate?: number;
7
- identityFeatures: IdentityFeaturesList;
8
- identityTraits: TraitModel[];
9
- identityUuid: string;
10
- djangoID: number | undefined;
11
- constructor(created_date: string, identityTraits: TraitModel[], identityFeatures: IdentityFeaturesList, environmentApiKey: string, identifier: string, identityUuid?: string);
12
- get compositeKey(): string;
13
- static generateCompositeKey(env_key: string, identifier: string): string;
14
- updateTraits(traits: TraitModel[]): void;
15
- }