flagsmith-nodejs 1.0.9 → 1.1.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 (75) hide show
  1. package/.idea/flagsmith-nodejs-client.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/vcs.xml +6 -0
  4. package/.prettierignore +1 -0
  5. package/.prettierrc.js +9 -0
  6. package/CONTRIBUTING.md +5 -4
  7. package/{LICENCE.md → LICENCE} +5 -6
  8. package/README.md +1 -1
  9. package/build/flagsmith-engine/environments/integrations/models.d.ts +4 -0
  10. package/build/flagsmith-engine/environments/integrations/models.js +8 -0
  11. package/build/flagsmith-engine/environments/models.d.ts +25 -0
  12. package/build/flagsmith-engine/environments/models.js +40 -0
  13. package/build/flagsmith-engine/environments/util.d.ts +3 -0
  14. package/build/flagsmith-engine/environments/util.js +19 -0
  15. package/build/flagsmith-engine/features/constants.d.ts +4 -0
  16. package/build/flagsmith-engine/features/constants.js +7 -0
  17. package/build/flagsmith-engine/features/models.d.ts +32 -0
  18. package/build/flagsmith-engine/features/models.js +85 -0
  19. package/build/flagsmith-engine/features/util.d.ts +3 -0
  20. package/build/flagsmith-engine/features/util.js +20 -0
  21. package/build/flagsmith-engine/identities/models.d.ts +15 -0
  22. package/build/flagsmith-engine/identities/models.js +47 -0
  23. package/build/flagsmith-engine/identities/traits/models.d.ts +5 -0
  24. package/build/flagsmith-engine/identities/traits/models.js +12 -0
  25. package/build/flagsmith-engine/identities/util.d.ts +4 -0
  26. package/build/flagsmith-engine/identities/util.js +22 -0
  27. package/build/flagsmith-engine/index.d.ts +8 -0
  28. package/build/flagsmith-engine/index.js +61 -0
  29. package/build/flagsmith-engine/organisations/models.d.ts +9 -0
  30. package/build/flagsmith-engine/organisations/models.js +21 -0
  31. package/build/flagsmith-engine/organisations/util.d.ts +2 -0
  32. package/build/flagsmith-engine/organisations/util.js +8 -0
  33. package/build/flagsmith-engine/projects/models.d.ts +10 -0
  34. package/build/flagsmith-engine/projects/models.js +17 -0
  35. package/build/flagsmith-engine/projects/util.d.ts +2 -0
  36. package/build/flagsmith-engine/projects/util.js +15 -0
  37. package/build/flagsmith-engine/segments/constants.d.ts +26 -0
  38. package/build/flagsmith-engine/segments/constants.js +31 -0
  39. package/build/flagsmith-engine/segments/evaluators.d.ts +6 -0
  40. package/build/flagsmith-engine/segments/evaluators.js +29 -0
  41. package/build/flagsmith-engine/segments/models.d.ts +31 -0
  42. package/build/flagsmith-engine/segments/models.js +83 -0
  43. package/build/flagsmith-engine/segments/util.d.ts +4 -0
  44. package/build/flagsmith-engine/segments/util.js +23 -0
  45. package/build/flagsmith-engine/utils/collections.d.ts +4 -0
  46. package/build/flagsmith-engine/utils/collections.js +16 -0
  47. package/build/flagsmith-engine/utils/errors.d.ts +2 -0
  48. package/build/flagsmith-engine/utils/errors.js +6 -0
  49. package/build/flagsmith-engine/utils/hashing/index.d.ts +9 -0
  50. package/build/flagsmith-engine/utils/hashing/index.js +54 -0
  51. package/build/flagsmith-engine/utils/index.d.ts +1 -0
  52. package/build/flagsmith-engine/utils/index.js +14 -0
  53. package/build/index.d.ts +1 -0
  54. package/build/index.js +11 -0
  55. package/build/sdk/analytics.d.ts +28 -0
  56. package/build/sdk/analytics.js +60 -0
  57. package/build/sdk/errors.d.ts +4 -0
  58. package/build/sdk/errors.js +9 -0
  59. package/build/sdk/index.d.ts +98 -0
  60. package/build/sdk/index.js +215 -0
  61. package/build/sdk/models.d.ts +55 -0
  62. package/build/sdk/models.js +102 -0
  63. package/build/sdk/polling_manager.d.ts +9 -0
  64. package/build/sdk/polling_manager.js +31 -0
  65. package/build/sdk/utils.d.ts +12 -0
  66. package/build/sdk/utils.js +45 -0
  67. package/example/README.md +10 -10
  68. package/example/package-lock.json +12 -12
  69. package/example/package.json +1 -1
  70. package/example/server/api/index.js +16 -17
  71. package/example/server/index.js +3 -6
  72. package/flagsmith-core.js +189 -184
  73. package/index.d.ts +28 -21
  74. package/index.js +2 -4
  75. package/package.json +7 -1
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/flagsmith-nodejs-client.iml" filepath="$PROJECT_DIR$/.idea/flagsmith-nodejs-client.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
@@ -0,0 +1 @@
1
+ *.json
package/.prettierrc.js ADDED
@@ -0,0 +1,9 @@
1
+ module.exports = {
2
+ bracketSpacing: true,
3
+ printWidth: 100,
4
+ singleQuote: true,
5
+ tabWidth: 4,
6
+ trailingComma: 'none',
7
+ useTabs: false,
8
+ arrowParens: 'avoid'
9
+ };
package/CONTRIBUTING.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Contributing
2
+
2
3
  We're always looking to improve this project, open source contribution is encouraged so long as they adhere to our guidelines.
3
4
 
4
5
  # Pull Requests
@@ -7,7 +8,7 @@ The Solid State team will be monitoring for pull requests. When we get one, a me
7
8
 
8
9
  **A couple things to keep in mind:**
9
10
 
10
- - If you've changed APIs, update the documentation.
11
- - Keep the code style (indents, wrapping) consistent.
12
- - If your PR involves a lot of commits, squash them using ```git rebase -i``` as this makes it easier for us to review.
13
- - Keep lines under 80 characters.
11
+ - If you've changed APIs, update the documentation.
12
+ - Keep the code style (indents, wrapping) consistent.
13
+ - If your PR involves a lot of commits, squash them using `git rebase -i` as this makes it easier for us to review.
14
+ - Keep lines under 80 characters.
@@ -1,12 +1,11 @@
1
- Copyright (c) 2017 Solid State Technology Ltd (https://www.solidstategroup.com/) and individual contributors.
2
- All rights reserved.
1
+ Copyright 2022 Bullet Train Ltd. A UK company.
3
2
 
4
3
  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
4
 
6
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
6
 
8
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
8
 
10
- 3. Neither the name of the Sentry nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
10
 
12
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- <img width="100%" src="https://raw.githubusercontent.com/SolidStateGroup/bullet-train-frontend/master/hero.png"/>
1
+ <img width="100%" src="https://github.com/Flagsmith/flagsmith/raw/main/static-files/hero.png"/>
2
2
 
3
3
  # Flagsmith NodeJS Client
4
4
 
@@ -0,0 +1,4 @@
1
+ export declare class IntegrationModel {
2
+ api_key?: string;
3
+ base_url?: string;
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IntegrationModel = void 0;
4
+ class IntegrationModel {
5
+ api_key = undefined;
6
+ base_url = undefined;
7
+ }
8
+ exports.IntegrationModel = IntegrationModel;
@@ -0,0 +1,25 @@
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
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EnvironmentModel = exports.EnvironmentAPIKeyModel = void 0;
4
+ class EnvironmentAPIKeyModel {
5
+ id;
6
+ key;
7
+ createdAt;
8
+ name;
9
+ clientApiKey;
10
+ expiresAt;
11
+ active = true;
12
+ constructor(id, key, createdAt, name, clientApiKey, expiresAt) {
13
+ this.id = id;
14
+ this.key = key;
15
+ this.createdAt = createdAt;
16
+ this.name = name;
17
+ this.clientApiKey = clientApiKey;
18
+ this.expiresAt = expiresAt;
19
+ }
20
+ isValid() {
21
+ return !!this.active && (!this.expiresAt || this.expiresAt > Date.now());
22
+ }
23
+ }
24
+ exports.EnvironmentAPIKeyModel = EnvironmentAPIKeyModel;
25
+ class EnvironmentModel {
26
+ id;
27
+ apiKey;
28
+ project;
29
+ featureStates = [];
30
+ amplitude_config;
31
+ segment_config;
32
+ mixpanel_config;
33
+ heap_config;
34
+ constructor(id, apiKey, project) {
35
+ this.id = id;
36
+ this.apiKey = apiKey;
37
+ this.project = project;
38
+ }
39
+ }
40
+ exports.EnvironmentModel = EnvironmentModel;
@@ -0,0 +1,3 @@
1
+ import { EnvironmentAPIKeyModel, EnvironmentModel } from './models';
2
+ export declare function buildEnvironmentModel(environmentJSON: any): EnvironmentModel;
3
+ export declare function buildEnvironmentAPIKeyModel(apiKeyJSON: any): EnvironmentAPIKeyModel;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildEnvironmentAPIKeyModel = exports.buildEnvironmentModel = void 0;
4
+ const util_1 = require("../features/util");
5
+ const util_2 = require("../projects/util");
6
+ const models_1 = require("./models");
7
+ function buildEnvironmentModel(environmentJSON) {
8
+ const project = (0, util_2.buildProjectModel)(environmentJSON.project);
9
+ const featureStates = environmentJSON.feature_states.map((fs) => (0, util_1.buildFeatureStateModel)(fs));
10
+ const environmentModel = new models_1.EnvironmentModel(environmentJSON.id, environmentJSON.api_key, project);
11
+ environmentModel.featureStates = featureStates;
12
+ return environmentModel;
13
+ }
14
+ exports.buildEnvironmentModel = buildEnvironmentModel;
15
+ function buildEnvironmentAPIKeyModel(apiKeyJSON) {
16
+ const model = new models_1.EnvironmentAPIKeyModel(apiKeyJSON.id, apiKeyJSON.key, Date.parse(apiKeyJSON.created_at), apiKeyJSON.name, apiKeyJSON.client_api_key);
17
+ return model;
18
+ }
19
+ exports.buildEnvironmentAPIKeyModel = buildEnvironmentAPIKeyModel;
@@ -0,0 +1,4 @@
1
+ export declare const CONSTANTS: {
2
+ STANDARD: string;
3
+ MULTIVARIATE: string;
4
+ };
@@ -0,0 +1,7 @@
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
+ };
@@ -0,0 +1,32 @@
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
+ private value;
26
+ multivariateFeatureStateValues: MultivariateFeatureStateValueModel[];
27
+ constructor(feature: FeatureModel, enabled: boolean, djangoID: number, value?: any, featurestateUuid?: string);
28
+ setValue(value: any): void;
29
+ getValue(identityId?: number | string): any;
30
+ get_feature_state_value(): any;
31
+ getMultivariateValue(identityID: number | string): any;
32
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FeatureStateModel = exports.MultivariateFeatureStateValueModel = exports.MultivariateFeatureOptionModel = exports.FeatureModel = void 0;
4
+ const uuid_1 = require("uuid");
5
+ const hashing_1 = require("../utils/hashing");
6
+ class FeatureModel {
7
+ id;
8
+ name;
9
+ type;
10
+ constructor(id, name, type) {
11
+ this.id = id;
12
+ this.name = name;
13
+ this.type = type;
14
+ }
15
+ eq(other) {
16
+ return !!other && this.id === other.id;
17
+ }
18
+ }
19
+ exports.FeatureModel = FeatureModel;
20
+ class MultivariateFeatureOptionModel {
21
+ value;
22
+ id;
23
+ constructor(value, id) {
24
+ this.value = value;
25
+ this.id = id;
26
+ }
27
+ }
28
+ exports.MultivariateFeatureOptionModel = MultivariateFeatureOptionModel;
29
+ class MultivariateFeatureStateValueModel {
30
+ multivariateFeatureOption;
31
+ percentageAllocation;
32
+ id;
33
+ mvFsValueUuid = (0, uuid_1.v4)();
34
+ constructor(multivariate_feature_option, percentage_allocation, id, mvFsValueUuid) {
35
+ this.id = id;
36
+ this.percentageAllocation = percentage_allocation;
37
+ this.multivariateFeatureOption = multivariate_feature_option;
38
+ this.mvFsValueUuid = mvFsValueUuid || this.mvFsValueUuid;
39
+ }
40
+ }
41
+ exports.MultivariateFeatureStateValueModel = MultivariateFeatureStateValueModel;
42
+ class FeatureStateModel {
43
+ feature;
44
+ enabled;
45
+ djangoID;
46
+ featurestateUUID = (0, uuid_1.v4)();
47
+ value;
48
+ multivariateFeatureStateValues = [];
49
+ constructor(feature, enabled, djangoID, value, featurestateUuid = (0, uuid_1.v4)()) {
50
+ this.feature = feature;
51
+ this.enabled = enabled;
52
+ this.djangoID = djangoID;
53
+ this.value = value;
54
+ this.featurestateUUID = featurestateUuid;
55
+ }
56
+ setValue(value) {
57
+ this.value = value;
58
+ }
59
+ getValue(identityId) {
60
+ if (!!identityId && this.multivariateFeatureStateValues.length > 0) {
61
+ return this.getMultivariateValue(identityId);
62
+ }
63
+ return this.value;
64
+ }
65
+ get_feature_state_value() {
66
+ return this.getValue();
67
+ }
68
+ getMultivariateValue(identityID) {
69
+ const percentageValue = (0, hashing_1.getHashedPercentateForObjIds)([
70
+ this.djangoID || this.featurestateUUID,
71
+ identityID
72
+ ]);
73
+ let startPercentage = 0;
74
+ const sortedF = this.multivariateFeatureStateValues.sort((a, b) => !!(a.id && b.id) ? a.id - b.id : a.mvFsValueUuid > b.mvFsValueUuid ? -1 : 1);
75
+ for (const myValue of sortedF) {
76
+ const limit = myValue.percentageAllocation + startPercentage;
77
+ if (startPercentage <= percentageValue && percentageValue < limit) {
78
+ return myValue.multivariateFeatureOption.value;
79
+ }
80
+ startPercentage = limit;
81
+ }
82
+ return this.value;
83
+ }
84
+ }
85
+ exports.FeatureStateModel = FeatureStateModel;
@@ -0,0 +1,3 @@
1
+ import { FeatureModel, FeatureStateModel } from './models';
2
+ export declare function buildFeatureModel(featuresModelJSON: any): FeatureModel;
3
+ export declare function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStateModel;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildFeatureStateModel = exports.buildFeatureModel = void 0;
4
+ const 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
+ const featureStateModel = new models_1.FeatureStateModel(buildFeatureModel(featuresStateModelJSON.feature), featuresStateModelJSON.enabled, featuresStateModelJSON.django_id, featuresStateModelJSON.feature_state_value, featuresStateModelJSON.uuid);
11
+ const multivariateFeatureStateValues = featuresStateModelJSON.multivariate_feature_state_values
12
+ ? featuresStateModelJSON.multivariate_feature_state_values.map((fsv) => {
13
+ const featureOption = new models_1.MultivariateFeatureOptionModel(fsv.multivariate_feature_option.value, fsv.multivariate_feature_option.id);
14
+ return new models_1.MultivariateFeatureStateValueModel(featureOption, fsv.percentage_allocation, fsv.id);
15
+ })
16
+ : [];
17
+ featureStateModel.multivariateFeatureStateValues = multivariateFeatureStateValues;
18
+ return featureStateModel;
19
+ }
20
+ exports.buildFeatureStateModel = buildFeatureStateModel;
@@ -0,0 +1,15 @@
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
+ update_traits(traits: TraitModel[]): void;
15
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdentityModel = void 0;
4
+ const collections_1 = require("../utils/collections");
5
+ const { v4: uuidv4 } = require('uuid');
6
+ class IdentityModel {
7
+ identifier;
8
+ environmentApiKey;
9
+ createdDate;
10
+ identityFeatures;
11
+ identityTraits;
12
+ identityUuid;
13
+ djangoID;
14
+ constructor(created_date, identityTraits, identityFeatures, environmentApiKey, identifier, identityUuid) {
15
+ this.identityUuid = identityUuid || uuidv4();
16
+ this.createdDate = Date.parse(created_date) || Date.now();
17
+ this.identityTraits = identityTraits;
18
+ this.identityFeatures = new collections_1.IdentityFeaturesList(...identityFeatures);
19
+ this.environmentApiKey = environmentApiKey;
20
+ this.identifier = identifier;
21
+ }
22
+ get compositeKey() {
23
+ return IdentityModel.generateCompositeKey(this.environmentApiKey, this.identifier);
24
+ }
25
+ static generateCompositeKey(env_key, identifier) {
26
+ return `${env_key}_${identifier}`;
27
+ }
28
+ update_traits(traits) {
29
+ const existingTraits = new Map();
30
+ for (const trait of this.identityTraits) {
31
+ existingTraits.set(trait.traitKey, trait);
32
+ }
33
+ for (const trait of traits) {
34
+ if (!!trait.traitValue) {
35
+ existingTraits.set(trait.traitKey, trait);
36
+ }
37
+ else {
38
+ existingTraits.delete(trait.traitKey);
39
+ }
40
+ }
41
+ this.identityTraits = [];
42
+ for (const [k, v] of existingTraits.entries()) {
43
+ this.identityTraits.push(v);
44
+ }
45
+ }
46
+ }
47
+ exports.IdentityModel = IdentityModel;
@@ -0,0 +1,5 @@
1
+ export declare class TraitModel {
2
+ traitKey: string;
3
+ traitValue: any;
4
+ constructor(key: string, value: any);
5
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TraitModel = void 0;
4
+ class TraitModel {
5
+ traitKey;
6
+ traitValue;
7
+ constructor(key, value) {
8
+ this.traitKey = key;
9
+ this.traitValue = value;
10
+ }
11
+ }
12
+ exports.TraitModel = TraitModel;
@@ -0,0 +1,4 @@
1
+ import { IdentityModel } from './models';
2
+ import { TraitModel } from './traits/models';
3
+ export declare function buildTraitModel(traitJSON: any): TraitModel;
4
+ export declare function buildIdentityModel(identityJSON: any): IdentityModel;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildIdentityModel = exports.buildTraitModel = void 0;
4
+ const util_1 = require("../features/util");
5
+ const collections_1 = require("../utils/collections");
6
+ const models_1 = require("./models");
7
+ const models_2 = require("./traits/models");
8
+ function buildTraitModel(traitJSON) {
9
+ return new models_2.TraitModel(traitJSON.trait_key, traitJSON.trait_value);
10
+ }
11
+ exports.buildTraitModel = buildTraitModel;
12
+ function buildIdentityModel(identityJSON) {
13
+ const featureList = identityJSON.identity_features
14
+ ? new collections_1.IdentityFeaturesList(...identityJSON.identity_features.map((f) => (0, util_1.buildFeatureStateModel)(f)))
15
+ : [];
16
+ const model = new models_1.IdentityModel(identityJSON.created_date, identityJSON.identity_traits
17
+ ? identityJSON.identity_traits.map((trait) => buildTraitModel(trait))
18
+ : [], featureList, identityJSON.environment_api_key, identityJSON.identifier, identityJSON.identity_uuid);
19
+ model.djangoID = identityJSON.django_id;
20
+ return model;
21
+ }
22
+ exports.buildIdentityModel = buildIdentityModel;
@@ -0,0 +1,8 @@
1
+ import { EnvironmentModel } from './environments/models';
2
+ import { FeatureStateModel } from './features/models';
3
+ import { IdentityModel } from './identities/models';
4
+ import { TraitModel } from './identities/traits/models';
5
+ export declare function getIdentityFeatureState(environment: EnvironmentModel, identity: IdentityModel, featureName: string, overrideTraits?: TraitModel[]): FeatureStateModel;
6
+ export declare function getIdentityFeatureStates(environment: EnvironmentModel, identity: IdentityModel, overrideTraits?: TraitModel[]): FeatureStateModel[];
7
+ export declare function getEnvironmentFeatureState(environment: EnvironmentModel, featureName: string): FeatureStateModel;
8
+ export declare function getEnvironmentFeatureStates(environment: EnvironmentModel): FeatureStateModel[];
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEnvironmentFeatureStates = exports.getEnvironmentFeatureState = exports.getIdentityFeatureStates = exports.getIdentityFeatureState = void 0;
4
+ const evaluators_1 = require("./segments/evaluators");
5
+ const errors_1 = require("./utils/errors");
6
+ function getIdentityFeatureStatesDict(environment, identity, overrideTraits) {
7
+ // Get feature states from the environment
8
+ const featureStates = {};
9
+ for (const fs of environment.featureStates) {
10
+ featureStates[fs.feature.id] = fs;
11
+ }
12
+ // Override with any feature states defined by matching segments
13
+ const identitySegments = (0, evaluators_1.getIdentitySegments)(environment, identity, overrideTraits);
14
+ for (const matchingSegment of identitySegments) {
15
+ for (const featureState of matchingSegment.featureStates) {
16
+ // note that feature states are stored on the segment in descending priority
17
+ // order so we only care that the last one is added
18
+ // TODO: can we optimise this?
19
+ featureStates[featureState.feature.id] = featureState;
20
+ }
21
+ }
22
+ // Override with any feature states defined directly the identity
23
+ for (const fs of identity.identityFeatures || []) {
24
+ if (featureStates[fs.feature.id]) {
25
+ featureStates[fs.feature.id] = fs;
26
+ }
27
+ }
28
+ return featureStates;
29
+ }
30
+ function getIdentityFeatureState(environment, identity, featureName, overrideTraits) {
31
+ const featureStates = getIdentityFeatureStatesDict(environment, identity, overrideTraits);
32
+ const matchingFeature = Object.values(featureStates).filter(f => f.feature.name === featureName);
33
+ if (matchingFeature.length === 0) {
34
+ throw new errors_1.FeatureStateNotFound('Feature State Not Found');
35
+ }
36
+ return matchingFeature[0];
37
+ }
38
+ exports.getIdentityFeatureState = getIdentityFeatureState;
39
+ function getIdentityFeatureStates(environment, identity, overrideTraits) {
40
+ const featureStates = Object.values(getIdentityFeatureStatesDict(environment, identity, overrideTraits));
41
+ if (environment.project.hideDisabledFlags) {
42
+ return featureStates.filter(fs => !!fs.enabled);
43
+ }
44
+ return featureStates;
45
+ }
46
+ exports.getIdentityFeatureStates = getIdentityFeatureStates;
47
+ function getEnvironmentFeatureState(environment, featureName) {
48
+ const featuresStates = environment.featureStates.filter(f => f.feature.name === featureName);
49
+ if (featuresStates.length === 0) {
50
+ throw new Error('Feature State Not Found');
51
+ }
52
+ return featuresStates[0];
53
+ }
54
+ exports.getEnvironmentFeatureState = getEnvironmentFeatureState;
55
+ function getEnvironmentFeatureStates(environment) {
56
+ if (environment.project.hideDisabledFlags) {
57
+ return environment.featureStates.filter(fs => !!fs.enabled);
58
+ }
59
+ return environment.featureStates;
60
+ }
61
+ exports.getEnvironmentFeatureStates = getEnvironmentFeatureStates;
@@ -0,0 +1,9 @@
1
+ export declare class OrganisationModel {
2
+ id: number;
3
+ name: string;
4
+ featureAnalytics: boolean;
5
+ stopServingFlags: boolean;
6
+ persistTraitData: boolean;
7
+ constructor(id: number, name: string, featureAnalytics: boolean, stopServingFlags: boolean, persistTraitData: boolean);
8
+ get unique_slug(): string;
9
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrganisationModel = void 0;
4
+ class OrganisationModel {
5
+ id;
6
+ name;
7
+ featureAnalytics;
8
+ stopServingFlags;
9
+ persistTraitData;
10
+ constructor(id, name, featureAnalytics, stopServingFlags, persistTraitData) {
11
+ this.id = id;
12
+ this.name = name;
13
+ this.featureAnalytics = featureAnalytics;
14
+ this.stopServingFlags = stopServingFlags;
15
+ this.persistTraitData = persistTraitData;
16
+ }
17
+ get unique_slug() {
18
+ return this.id.toString() + '-' + this.name;
19
+ }
20
+ }
21
+ exports.OrganisationModel = OrganisationModel;
@@ -0,0 +1,2 @@
1
+ import { OrganisationModel } from './models';
2
+ export declare function buildOrganizationModel(organizationJSON: any): OrganisationModel;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildOrganizationModel = void 0;
4
+ const models_1 = require("./models");
5
+ function buildOrganizationModel(organizationJSON) {
6
+ return new models_1.OrganisationModel(organizationJSON.id, organizationJSON.name, organizationJSON.feature_analytics, organizationJSON.stop_serving_flags, organizationJSON.persist_trait_data);
7
+ }
8
+ exports.buildOrganizationModel = buildOrganizationModel;
@@ -0,0 +1,10 @@
1
+ import { OrganisationModel } from '../organisations/models';
2
+ import { SegmentModel } from '../segments/models';
3
+ export declare class ProjectModel {
4
+ id: number;
5
+ name: string;
6
+ organisation: OrganisationModel;
7
+ hideDisabledFlags: boolean;
8
+ segments: SegmentModel[];
9
+ constructor(id: number, name: string, hideDisabledFlags: boolean, organization: OrganisationModel);
10
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProjectModel = void 0;
4
+ class ProjectModel {
5
+ id;
6
+ name;
7
+ organisation;
8
+ hideDisabledFlags;
9
+ segments = [];
10
+ constructor(id, name, hideDisabledFlags, organization) {
11
+ this.id = id;
12
+ this.name = name;
13
+ this.hideDisabledFlags = hideDisabledFlags;
14
+ this.organisation = organization;
15
+ }
16
+ }
17
+ exports.ProjectModel = ProjectModel;
@@ -0,0 +1,2 @@
1
+ import { ProjectModel } from './models';
2
+ export declare function buildProjectModel(projectJSON: any): ProjectModel;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildProjectModel = void 0;
4
+ const util_1 = require("../organisations/util");
5
+ const util_2 = require("../segments/util");
6
+ const models_1 = require("./models");
7
+ function buildProjectModel(projectJSON) {
8
+ const segments = projectJSON['segments']
9
+ ? projectJSON['segments'].map((s) => (0, util_2.buildSegmentModel)(s))
10
+ : [];
11
+ const model = new models_1.ProjectModel(projectJSON.id, projectJSON.name, projectJSON.hide_disabled_flags, (0, util_1.buildOrganizationModel)(projectJSON.organisation));
12
+ model.segments = segments;
13
+ return model;
14
+ }
15
+ exports.buildProjectModel = buildProjectModel;