flagsmith-nodejs 2.0.0-beta.5 → 2.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 (98) hide show
  1. package/.github/workflows/pull_request.yaml +37 -29
  2. package/.tool-versions +1 -0
  3. package/README.md +7 -0
  4. package/example/package-lock.json +1 -996
  5. package/flagsmith-engine/features/models.ts +28 -6
  6. package/flagsmith-engine/features/util.ts +9 -0
  7. package/flagsmith-engine/identities/models.ts +1 -1
  8. package/flagsmith-engine/index.ts +7 -5
  9. package/flagsmith-engine/organisations/models.ts +1 -1
  10. package/flagsmith-engine/segments/models.ts +21 -4
  11. package/flagsmith-engine/segments/util.ts +8 -0
  12. package/flagsmith-engine/utils/collections.ts +1 -12
  13. package/flagsmith-engine/utils/hashing/index.ts +5 -2
  14. package/flagsmith-engine/utils/index.ts +6 -2
  15. package/package.json +4 -1
  16. package/sdk/analytics.ts +0 -2
  17. package/sdk/index.ts +84 -28
  18. package/sdk/polling_manager.ts +0 -1
  19. package/sdk/types.ts +8 -0
  20. package/sdk/utils.ts +4 -5
  21. package/tests/engine/unit/{egine.test.ts → engine.test.ts} +20 -0
  22. package/tests/engine/unit/features/models.test.ts +8 -4
  23. package/tests/engine/unit/identities/identities_builders.test.ts +13 -0
  24. package/tests/engine/unit/identities/identities_models.test.ts +3 -14
  25. package/tests/engine/unit/organization/models.test.ts +1 -1
  26. package/tests/engine/unit/segments/segments_model.test.ts +22 -1
  27. package/tests/engine/unit/utils.ts +1 -1
  28. package/tests/sdk/analytics.test.ts +11 -0
  29. package/tests/sdk/flagsmith-cache.test.ts +150 -0
  30. package/tests/sdk/flagsmith-environment-flags.test.ts +197 -0
  31. package/tests/sdk/flagsmith-identity-flags.test.ts +140 -0
  32. package/tests/sdk/flagsmith.test.ts +100 -85
  33. package/tests/sdk/polling.test.ts +25 -0
  34. package/tests/sdk/utils.ts +21 -2
  35. package/tsconfig.json +2 -1
  36. package/.idea/flagsmith-nodejs-client.iml +0 -12
  37. package/.idea/modules.xml +0 -8
  38. package/.idea/vcs.xml +0 -6
  39. package/build/flagsmith-engine/environments/integrations/models.d.ts +0 -4
  40. package/build/flagsmith-engine/environments/integrations/models.js +0 -8
  41. package/build/flagsmith-engine/environments/models.d.ts +0 -25
  42. package/build/flagsmith-engine/environments/models.js +0 -40
  43. package/build/flagsmith-engine/environments/util.d.ts +0 -3
  44. package/build/flagsmith-engine/environments/util.js +0 -19
  45. package/build/flagsmith-engine/features/constants.d.ts +0 -4
  46. package/build/flagsmith-engine/features/constants.js +0 -7
  47. package/build/flagsmith-engine/features/models.d.ts +0 -32
  48. package/build/flagsmith-engine/features/models.js +0 -85
  49. package/build/flagsmith-engine/features/util.d.ts +0 -3
  50. package/build/flagsmith-engine/features/util.js +0 -20
  51. package/build/flagsmith-engine/identities/models.d.ts +0 -15
  52. package/build/flagsmith-engine/identities/models.js +0 -47
  53. package/build/flagsmith-engine/identities/traits/models.d.ts +0 -5
  54. package/build/flagsmith-engine/identities/traits/models.js +0 -12
  55. package/build/flagsmith-engine/identities/util.d.ts +0 -4
  56. package/build/flagsmith-engine/identities/util.js +0 -22
  57. package/build/flagsmith-engine/index.d.ts +0 -8
  58. package/build/flagsmith-engine/index.js +0 -61
  59. package/build/flagsmith-engine/organisations/models.d.ts +0 -9
  60. package/build/flagsmith-engine/organisations/models.js +0 -21
  61. package/build/flagsmith-engine/organisations/util.d.ts +0 -2
  62. package/build/flagsmith-engine/organisations/util.js +0 -8
  63. package/build/flagsmith-engine/projects/models.d.ts +0 -10
  64. package/build/flagsmith-engine/projects/models.js +0 -17
  65. package/build/flagsmith-engine/projects/util.d.ts +0 -2
  66. package/build/flagsmith-engine/projects/util.js +0 -15
  67. package/build/flagsmith-engine/segments/constants.d.ts +0 -26
  68. package/build/flagsmith-engine/segments/constants.js +0 -31
  69. package/build/flagsmith-engine/segments/evaluators.d.ts +0 -6
  70. package/build/flagsmith-engine/segments/evaluators.js +0 -29
  71. package/build/flagsmith-engine/segments/models.d.ts +0 -31
  72. package/build/flagsmith-engine/segments/models.js +0 -83
  73. package/build/flagsmith-engine/segments/util.d.ts +0 -4
  74. package/build/flagsmith-engine/segments/util.js +0 -23
  75. package/build/flagsmith-engine/utils/collections.d.ts +0 -4
  76. package/build/flagsmith-engine/utils/collections.js +0 -16
  77. package/build/flagsmith-engine/utils/errors.d.ts +0 -2
  78. package/build/flagsmith-engine/utils/errors.js +0 -6
  79. package/build/flagsmith-engine/utils/hashing/index.d.ts +0 -9
  80. package/build/flagsmith-engine/utils/hashing/index.js +0 -54
  81. package/build/flagsmith-engine/utils/index.d.ts +0 -1
  82. package/build/flagsmith-engine/utils/index.js +0 -14
  83. package/build/index.d.ts +0 -1
  84. package/build/index.js +0 -11
  85. package/build/sdk/analytics.d.ts +0 -28
  86. package/build/sdk/analytics.js +0 -60
  87. package/build/sdk/errors.d.ts +0 -4
  88. package/build/sdk/errors.js +0 -9
  89. package/build/sdk/index.d.ts +0 -118
  90. package/build/sdk/index.js +0 -263
  91. package/build/sdk/models.d.ts +0 -55
  92. package/build/sdk/models.js +0 -101
  93. package/build/sdk/polling_manager.d.ts +0 -9
  94. package/build/sdk/polling_manager.js +0 -31
  95. package/build/sdk/utils.d.ts +0 -12
  96. package/build/sdk/utils.js +0 -45
  97. package/tests/engine/engine-tests/engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json +0 -12393
  98. package/tests/engine/engine-tests/engine-test-data/readme.md +0 -30
@@ -1,47 +0,0 @@
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;
@@ -1,5 +0,0 @@
1
- export declare class TraitModel {
2
- traitKey: string;
3
- traitValue: any;
4
- constructor(key: string, value: any);
5
- }
@@ -1,12 +0,0 @@
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;
@@ -1,4 +0,0 @@
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;
@@ -1,22 +0,0 @@
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;
@@ -1,8 +0,0 @@
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[];
@@ -1,61 +0,0 @@
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;
@@ -1,9 +0,0 @@
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
- }
@@ -1,21 +0,0 @@
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;
@@ -1,2 +0,0 @@
1
- import { OrganisationModel } from './models';
2
- export declare function buildOrganizationModel(organizationJSON: any): OrganisationModel;
@@ -1,8 +0,0 @@
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;
@@ -1,10 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
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;
@@ -1,2 +0,0 @@
1
- import { ProjectModel } from './models';
2
- export declare function buildProjectModel(projectJSON: any): ProjectModel;
@@ -1,15 +0,0 @@
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;
@@ -1,26 +0,0 @@
1
- export declare const ALL_RULE = "ALL";
2
- export declare const ANY_RULE = "ANY";
3
- export declare const NONE_RULE = "NONE";
4
- export declare const RULE_TYPES: string[];
5
- export declare const EQUAL = "EQUAL";
6
- export declare const GREATER_THAN = "GREATER_THAN";
7
- export declare const LESS_THAN = "LESS_THAN";
8
- export declare const LESS_THAN_INCLUSIVE = "LESS_THAN_INCLUSIVE";
9
- export declare const CONTAINS = "CONTAINS";
10
- export declare const GREATER_THAN_INCLUSIVE = "GREATER_THAN_INCLUSIVE";
11
- export declare const NOT_CONTAINS = "NOT_CONTAINS";
12
- export declare const NOT_EQUAL = "NOT_EQUAL";
13
- export declare const REGEX = "REGEX";
14
- export declare const PERCENTAGE_SPLIT = "PERCENTAGE_SPLIT";
15
- export declare const CONDITION_OPERATORS: {
16
- EQUAL: string;
17
- GREATER_THAN: string;
18
- LESS_THAN: string;
19
- LESS_THAN_INCLUSIVE: string;
20
- CONTAINS: string;
21
- GREATER_THAN_INCLUSIVE: string;
22
- NOT_CONTAINS: string;
23
- NOT_EQUAL: string;
24
- REGEX: string;
25
- PERCENTAGE_SPLIT: string;
26
- };
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONDITION_OPERATORS = exports.PERCENTAGE_SPLIT = exports.REGEX = exports.NOT_EQUAL = exports.NOT_CONTAINS = exports.GREATER_THAN_INCLUSIVE = exports.CONTAINS = exports.LESS_THAN_INCLUSIVE = exports.LESS_THAN = exports.GREATER_THAN = exports.EQUAL = exports.RULE_TYPES = exports.NONE_RULE = exports.ANY_RULE = exports.ALL_RULE = void 0;
4
- // Segment Rules
5
- exports.ALL_RULE = 'ALL';
6
- exports.ANY_RULE = 'ANY';
7
- exports.NONE_RULE = 'NONE';
8
- exports.RULE_TYPES = [exports.ALL_RULE, exports.ANY_RULE, exports.NONE_RULE];
9
- // Segment Condition Operators
10
- exports.EQUAL = 'EQUAL';
11
- exports.GREATER_THAN = 'GREATER_THAN';
12
- exports.LESS_THAN = 'LESS_THAN';
13
- exports.LESS_THAN_INCLUSIVE = 'LESS_THAN_INCLUSIVE';
14
- exports.CONTAINS = 'CONTAINS';
15
- exports.GREATER_THAN_INCLUSIVE = 'GREATER_THAN_INCLUSIVE';
16
- exports.NOT_CONTAINS = 'NOT_CONTAINS';
17
- exports.NOT_EQUAL = 'NOT_EQUAL';
18
- exports.REGEX = 'REGEX';
19
- exports.PERCENTAGE_SPLIT = 'PERCENTAGE_SPLIT';
20
- exports.CONDITION_OPERATORS = {
21
- EQUAL: exports.EQUAL,
22
- GREATER_THAN: exports.GREATER_THAN,
23
- LESS_THAN: exports.LESS_THAN,
24
- LESS_THAN_INCLUSIVE: exports.LESS_THAN_INCLUSIVE,
25
- CONTAINS: exports.CONTAINS,
26
- GREATER_THAN_INCLUSIVE: exports.GREATER_THAN_INCLUSIVE,
27
- NOT_CONTAINS: exports.NOT_CONTAINS,
28
- NOT_EQUAL: exports.NOT_EQUAL,
29
- REGEX: exports.REGEX,
30
- PERCENTAGE_SPLIT: exports.PERCENTAGE_SPLIT
31
- };
@@ -1,6 +0,0 @@
1
- import { EnvironmentModel } from '../environments/models';
2
- import { IdentityModel } from '../identities/models';
3
- import { TraitModel } from '../identities/traits/models';
4
- import { SegmentModel } from './models';
5
- export declare function getIdentitySegments(environment: EnvironmentModel, identity: IdentityModel, overrideTraits?: TraitModel[]): SegmentModel[];
6
- export declare function evaluateIdentityInSegment(identity: IdentityModel, segment: SegmentModel, overrideTraits?: TraitModel[]): boolean;
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.evaluateIdentityInSegment = exports.getIdentitySegments = void 0;
4
- const hashing_1 = require("../utils/hashing");
5
- const constants_1 = require("./constants");
6
- function getIdentitySegments(environment, identity, overrideTraits) {
7
- return environment.project.segments.filter(segment => evaluateIdentityInSegment(identity, segment, overrideTraits));
8
- }
9
- exports.getIdentitySegments = getIdentitySegments;
10
- function evaluateIdentityInSegment(identity, segment, overrideTraits) {
11
- return (segment.rules.length > 0 &&
12
- segment.rules.filter(rule => traitsMatchSegmentRule(overrideTraits || identity.identityTraits, rule, segment.id, identity.compositeKey)).length === segment.rules.length);
13
- }
14
- exports.evaluateIdentityInSegment = evaluateIdentityInSegment;
15
- function traitsMatchSegmentRule(identityTraits, rule, segmentId, identityId) {
16
- const matchesConditions = rule.conditions.length > 0
17
- ? rule.matchingFunction()(rule.conditions.map(condition => traitsMatchSegmentCondition(identityTraits, condition, segmentId, identityId)))
18
- : true;
19
- return (matchesConditions &&
20
- rule.rules.filter(rule => traitsMatchSegmentRule(identityTraits, rule, segmentId, identityId)).length === rule.rules.length);
21
- }
22
- function traitsMatchSegmentCondition(identityTraits, condition, segmentId, identityId) {
23
- if (condition.operator == constants_1.PERCENTAGE_SPLIT) {
24
- return (0, hashing_1.getHashedPercentateForObjIds)([segmentId, identityId]) <= parseFloat(condition.value);
25
- }
26
- const traits = identityTraits.filter(t => t.traitKey === condition.property_);
27
- const trait = traits.length > 0 ? traits[0] : undefined;
28
- return trait ? condition.matchesTraitValue(trait.traitValue) : false;
29
- }
@@ -1,31 +0,0 @@
1
- import { FeatureStateModel } from '../features/models';
2
- export declare const all: (iterable: Array<any>) => boolean;
3
- export declare const any: (iterable: Array<any>) => boolean;
4
- export declare const matchingFunctions: {
5
- [x: string]: (thisValue: any, otherValue: any) => any;
6
- };
7
- export declare class SegmentConditionModel {
8
- EXCEPTION_OPERATOR_METHODS: {
9
- [key: string]: string;
10
- };
11
- operator: string;
12
- value: string;
13
- property_: string | undefined;
14
- constructor(operator: string, value: string, property?: string);
15
- matchesTraitValue(traitValue: any): any;
16
- }
17
- export declare class SegmentRuleModel {
18
- type: string;
19
- rules: SegmentRuleModel[];
20
- conditions: SegmentConditionModel[];
21
- constructor(type: string);
22
- static none(iterable: Array<any>): boolean;
23
- matchingFunction(): CallableFunction;
24
- }
25
- export declare class SegmentModel {
26
- id: number;
27
- name: string;
28
- rules: SegmentRuleModel[];
29
- featureStates: FeatureStateModel[];
30
- constructor(id: number, name: string);
31
- }
@@ -1,83 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SegmentModel = exports.SegmentRuleModel = exports.SegmentConditionModel = exports.matchingFunctions = exports.any = exports.all = void 0;
4
- const utils_1 = require("../utils");
5
- const constants_1 = require("./constants");
6
- const all = (iterable) => iterable.filter(e => !!e).length === iterable.length;
7
- exports.all = all;
8
- const any = (iterable) => iterable.filter(e => !!e).length > 0;
9
- exports.any = any;
10
- exports.matchingFunctions = {
11
- [constants_1.CONDITION_OPERATORS.EQUAL]: (thisValue, otherValue) => thisValue == otherValue,
12
- [constants_1.CONDITION_OPERATORS.GREATER_THAN]: (thisValue, otherValue) => otherValue > thisValue,
13
- [constants_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue, otherValue) => otherValue >= thisValue,
14
- [constants_1.CONDITION_OPERATORS.LESS_THAN]: (thisValue, otherValue) => thisValue > otherValue,
15
- [constants_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue, otherValue) => thisValue >= otherValue,
16
- [constants_1.CONDITION_OPERATORS.NOT_EQUAL]: (thisValue, otherValue) => thisValue != otherValue,
17
- [constants_1.CONDITION_OPERATORS.CONTAINS]: (thisValue, otherValue) => otherValue.includes(thisValue),
18
- [constants_1.CONDITION_OPERATORS.NOT_CONTAINS]: (thisValue, otherValue) => !otherValue.includes(thisValue)
19
- };
20
- class SegmentConditionModel {
21
- EXCEPTION_OPERATOR_METHODS = {
22
- [constants_1.NOT_CONTAINS]: 'evaluateNotContains',
23
- [constants_1.REGEX]: 'evaluateRegex'
24
- };
25
- operator;
26
- value;
27
- property_;
28
- constructor(operator, value, property) {
29
- this.operator = operator;
30
- this.value = value;
31
- this.property_ = property;
32
- }
33
- matchesTraitValue(traitValue) {
34
- const evaluators = {
35
- evaluateNotContains: (traitValue) => {
36
- return !traitValue.includes(this.value);
37
- },
38
- evaluateRegex: (traitValue) => {
39
- return !!traitValue.match(new RegExp(this.value));
40
- }
41
- };
42
- // TODO: move this logic to the evaluator module
43
- if (this.EXCEPTION_OPERATOR_METHODS[this.operator]) {
44
- const evaluatorFunction = evaluators[this.EXCEPTION_OPERATOR_METHODS[this.operator]];
45
- return evaluatorFunction(traitValue);
46
- }
47
- const defaultFunction = (x, y) => false;
48
- const matchingFunction = exports.matchingFunctions[this.operator] || defaultFunction;
49
- const castToTypeOfTraitValue = (0, utils_1.getCastingFunction)(traitValue);
50
- return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
51
- }
52
- }
53
- exports.SegmentConditionModel = SegmentConditionModel;
54
- class SegmentRuleModel {
55
- type;
56
- rules = [];
57
- conditions = [];
58
- constructor(type) {
59
- this.type = type;
60
- }
61
- static none(iterable) {
62
- return iterable.filter(e => !!e).length === 0;
63
- }
64
- matchingFunction() {
65
- return {
66
- [constants_1.ANY_RULE]: exports.any,
67
- [constants_1.ALL_RULE]: exports.all,
68
- [constants_1.NONE_RULE]: SegmentRuleModel.none
69
- }[this.type];
70
- }
71
- }
72
- exports.SegmentRuleModel = SegmentRuleModel;
73
- class SegmentModel {
74
- id;
75
- name;
76
- rules = [];
77
- featureStates = [];
78
- constructor(id, name) {
79
- this.id = id;
80
- this.name = name;
81
- }
82
- }
83
- exports.SegmentModel = SegmentModel;
@@ -1,4 +0,0 @@
1
- import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models';
2
- export declare function buildSegmentConditionModel(segmentConditionJSON: any): SegmentConditionModel;
3
- export declare function buildSegmentRuleModel(ruleModelJSON: any): SegmentRuleModel;
4
- export declare function buildSegmentModel(segmentModelJSON: any): SegmentModel;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildSegmentModel = exports.buildSegmentRuleModel = exports.buildSegmentConditionModel = void 0;
4
- const util_1 = require("../features/util");
5
- const models_1 = require("./models");
6
- function buildSegmentConditionModel(segmentConditionJSON) {
7
- return new models_1.SegmentConditionModel(segmentConditionJSON.operator, segmentConditionJSON.value, segmentConditionJSON.property_);
8
- }
9
- exports.buildSegmentConditionModel = buildSegmentConditionModel;
10
- function buildSegmentRuleModel(ruleModelJSON) {
11
- const ruleModel = new models_1.SegmentRuleModel(ruleModelJSON.type);
12
- ruleModel.rules = ruleModelJSON.rules.map((r) => buildSegmentRuleModel(r));
13
- ruleModel.conditions = ruleModelJSON.conditions.map((c) => buildSegmentConditionModel(c));
14
- return ruleModel;
15
- }
16
- exports.buildSegmentRuleModel = buildSegmentRuleModel;
17
- function buildSegmentModel(segmentModelJSON) {
18
- const model = new models_1.SegmentModel(segmentModelJSON.id, segmentModelJSON.name);
19
- model.featureStates = segmentModelJSON['feature_states'].map((fs) => (0, util_1.buildFeatureStateModel)(fs));
20
- model.rules = segmentModelJSON['rules'].map((r) => buildSegmentRuleModel(r));
21
- return model;
22
- }
23
- exports.buildSegmentModel = buildSegmentModel;
@@ -1,4 +0,0 @@
1
- import { FeatureStateModel } from '../features/models';
2
- export declare class IdentityFeaturesList extends Array<FeatureStateModel> {
3
- push(...e: FeatureStateModel[]): number;
4
- }
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IdentityFeaturesList = void 0;
4
- class IdentityFeaturesList extends Array {
5
- push(...e) {
6
- for (const [_, item] of e.entries()) {
7
- for (const [k, v] of this.entries()) {
8
- if (v.djangoID === item.djangoID) {
9
- throw new Error('feature state for this feature already exists');
10
- }
11
- }
12
- }
13
- return super.push(...e);
14
- }
15
- }
16
- exports.IdentityFeaturesList = IdentityFeaturesList;
@@ -1,2 +0,0 @@
1
- export declare class FeatureStateNotFound extends Error {
2
- }
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FeatureStateNotFound = void 0;
4
- class FeatureStateNotFound extends Error {
5
- }
6
- exports.FeatureStateNotFound = FeatureStateNotFound;
@@ -1,9 +0,0 @@
1
- /**
2
- * Given a list of object ids, get a floating point number between 0 and 1 based on
3
- * the hash of those ids. This should give the same value every time for any list of ids.
4
- *
5
- * @param {Array<any>} objectIds list of object ids to calculate the has for
6
- * @param {} iterations=1 num times to include each id in the generated string to hash
7
- * @returns number number between 0 (inclusive) and 100 (exclusive)
8
- */
9
- export declare function getHashedPercentateForObjIds(objectIds: Array<any>, iterations?: number): number;
@@ -1,54 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getHashedPercentateForObjIds = void 0;
7
- const md5_1 = __importDefault(require("md5"));
8
- const big_integer_1 = __importDefault(require("big-integer"));
9
- const makeRepeated = (arr, repeats) => Array.from({ length: repeats }, () => arr).flat();
10
- // https://stackoverflow.com/questions/12532871/how-to-convert-a-very-large-hex-number-to-decimal-in-javascript
11
- function h2d(s) {
12
- function add(x, y) {
13
- var c = 0, r = [];
14
- var x = x.split('').map(Number);
15
- var y = y.split('').map(Number);
16
- while (x.length || y.length) {
17
- var s = (x.pop() || 0) + (y.pop() || 0) + c;
18
- r.unshift(s < 10 ? s : s - 10);
19
- c = s < 10 ? 0 : 1;
20
- }
21
- if (c)
22
- r.unshift(c);
23
- return r.join('');
24
- }
25
- var dec = '0';
26
- s.split('').forEach(function (chr) {
27
- var n = parseInt(chr, 16);
28
- for (var t = 8; t; t >>= 1) {
29
- dec = add(dec, dec);
30
- if (n & t)
31
- dec = add(dec, '1');
32
- }
33
- });
34
- return dec;
35
- }
36
- /**
37
- * Given a list of object ids, get a floating point number between 0 and 1 based on
38
- * the hash of those ids. This should give the same value every time for any list of ids.
39
- *
40
- * @param {Array<any>} objectIds list of object ids to calculate the has for
41
- * @param {} iterations=1 num times to include each id in the generated string to hash
42
- * @returns number number between 0 (inclusive) and 100 (exclusive)
43
- */
44
- function getHashedPercentateForObjIds(objectIds, iterations = 1) {
45
- let to_hash = makeRepeated(objectIds, iterations).join(',');
46
- const hashedValue = (0, md5_1.default)(to_hash);
47
- const hashedInt = (0, big_integer_1.default)(h2d(hashedValue));
48
- const value = (hashedInt.mod(9999).toJSNumber() / 9998) * 100;
49
- if (value === 100) {
50
- return getHashedPercentateForObjIds(objectIds, iterations + 1);
51
- }
52
- return value;
53
- }
54
- exports.getHashedPercentateForObjIds = getHashedPercentateForObjIds;
@@ -1 +0,0 @@
1
- export declare function getCastingFunction(input: any): CallableFunction;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCastingFunction = void 0;
4
- function getCastingFunction(input) {
5
- switch (typeof input) {
6
- case 'boolean':
7
- return (x) => !['False', 'false'].includes(x);
8
- case 'number':
9
- return (x) => parseFloat(x);
10
- default:
11
- return (x) => String(x);
12
- }
13
- }
14
- exports.getCastingFunction = getCastingFunction;
package/build/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { Flagsmith } from './sdk';
package/build/index.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Flagsmith = void 0;
7
- const sdk_1 = __importDefault(require("./sdk"));
8
- var sdk_2 = require("./sdk");
9
- Object.defineProperty(exports, "Flagsmith", { enumerable: true, get: function () { return sdk_2.Flagsmith; } });
10
- // export default Flagsmith;
11
- module.exports = sdk_1.default;