flagsmith-nodejs 2.0.1 → 2.0.4

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 (74) hide show
  1. package/README.md +1 -1
  2. package/build/flagsmith-engine/environments/integrations/models.d.ts +4 -0
  3. package/build/flagsmith-engine/environments/integrations/models.js +11 -0
  4. package/build/flagsmith-engine/environments/models.d.ts +25 -0
  5. package/build/flagsmith-engine/environments/models.js +29 -0
  6. package/build/flagsmith-engine/environments/util.d.ts +3 -0
  7. package/build/flagsmith-engine/environments/util.js +21 -0
  8. package/build/flagsmith-engine/features/constants.d.ts +4 -0
  9. package/build/flagsmith-engine/features/constants.js +7 -0
  10. package/build/flagsmith-engine/features/models.d.ts +37 -0
  11. package/build/flagsmith-engine/features/models.js +122 -0
  12. package/build/flagsmith-engine/features/util.d.ts +4 -0
  13. package/build/flagsmith-engine/features/util.js +27 -0
  14. package/build/flagsmith-engine/identities/models.d.ts +15 -0
  15. package/build/flagsmith-engine/identities/models.js +112 -0
  16. package/build/flagsmith-engine/identities/traits/models.d.ts +5 -0
  17. package/build/flagsmith-engine/identities/traits/models.js +11 -0
  18. package/build/flagsmith-engine/identities/util.d.ts +4 -0
  19. package/build/flagsmith-engine/identities/util.js +46 -0
  20. package/build/flagsmith-engine/index.d.ts +8 -0
  21. package/build/flagsmith-engine/index.js +115 -0
  22. package/build/flagsmith-engine/organisations/models.d.ts +9 -0
  23. package/build/flagsmith-engine/organisations/models.js +21 -0
  24. package/build/flagsmith-engine/organisations/util.d.ts +2 -0
  25. package/build/flagsmith-engine/organisations/util.js +8 -0
  26. package/build/flagsmith-engine/projects/models.d.ts +10 -0
  27. package/build/flagsmith-engine/projects/models.js +14 -0
  28. package/build/flagsmith-engine/projects/util.d.ts +2 -0
  29. package/build/flagsmith-engine/projects/util.js +15 -0
  30. package/build/flagsmith-engine/segments/constants.d.ts +26 -0
  31. package/build/flagsmith-engine/segments/constants.js +31 -0
  32. package/build/flagsmith-engine/segments/evaluators.d.ts +6 -0
  33. package/build/flagsmith-engine/segments/evaluators.js +37 -0
  34. package/build/flagsmith-engine/segments/models.d.ts +37 -0
  35. package/build/flagsmith-engine/segments/models.js +114 -0
  36. package/build/flagsmith-engine/segments/util.d.ts +6 -0
  37. package/build/flagsmith-engine/segments/util.js +33 -0
  38. package/build/flagsmith-engine/utils/collections.d.ts +3 -0
  39. package/build/flagsmith-engine/utils/collections.js +26 -0
  40. package/build/flagsmith-engine/utils/errors.d.ts +2 -0
  41. package/build/flagsmith-engine/utils/errors.js +26 -0
  42. package/build/flagsmith-engine/utils/hashing/index.d.ts +9 -0
  43. package/build/flagsmith-engine/utils/hashing/index.js +60 -0
  44. package/build/flagsmith-engine/utils/index.d.ts +1 -0
  45. package/build/flagsmith-engine/utils/index.js +17 -0
  46. package/build/index.d.ts +1 -0
  47. package/build/index.js +11 -0
  48. package/build/sdk/analytics.d.ts +28 -0
  49. package/build/sdk/analytics.js +102 -0
  50. package/build/sdk/errors.d.ts +4 -0
  51. package/build/sdk/errors.js +34 -0
  52. package/build/sdk/index.d.ts +123 -0
  53. package/build/sdk/index.js +492 -0
  54. package/build/sdk/models.d.ts +55 -0
  55. package/build/sdk/models.js +149 -0
  56. package/build/sdk/polling_manager.d.ts +9 -0
  57. package/build/sdk/polling_manager.js +72 -0
  58. package/build/sdk/types.d.ts +7 -0
  59. package/build/sdk/types.js +2 -0
  60. package/build/sdk/utils.d.ts +12 -0
  61. package/build/sdk/utils.js +92 -0
  62. package/example/package-lock.json +997 -21
  63. package/example/server/api/index.js +6 -3
  64. package/example/server/index.js +1 -1
  65. package/package.json +3 -2
  66. package/sdk/analytics.ts +2 -2
  67. package/sdk/index.ts +4 -0
  68. package/sdk/models.ts +1 -1
  69. package/sdk/types.ts +4 -4
  70. package/tests/sdk/analytics.test.ts +10 -10
  71. package/tests/sdk/utils.ts +2 -1
  72. package/.idea/flagsmith-nodejs-client.iml +0 -12
  73. package/.idea/modules.xml +0 -8
  74. package/.idea/vcs.xml +0 -6
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrganisationModel = void 0;
4
+ var OrganisationModel = /** @class */ (function () {
5
+ function OrganisationModel(id, name, featureAnalytics, stopServingFlags, persistTraitData) {
6
+ this.id = id;
7
+ this.name = name;
8
+ this.featureAnalytics = featureAnalytics;
9
+ this.stopServingFlags = stopServingFlags;
10
+ this.persistTraitData = persistTraitData;
11
+ }
12
+ Object.defineProperty(OrganisationModel.prototype, "uniqueSlug", {
13
+ get: function () {
14
+ return this.id.toString() + '-' + this.name;
15
+ },
16
+ enumerable: false,
17
+ configurable: true
18
+ });
19
+ return OrganisationModel;
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
+ var 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,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProjectModel = void 0;
4
+ var ProjectModel = /** @class */ (function () {
5
+ function ProjectModel(id, name, hideDisabledFlags, organization) {
6
+ this.segments = [];
7
+ this.id = id;
8
+ this.name = name;
9
+ this.hideDisabledFlags = hideDisabledFlags;
10
+ this.organisation = organization;
11
+ }
12
+ return ProjectModel;
13
+ }());
14
+ 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
+ var util_1 = require("../organisations/util");
5
+ var util_2 = require("../segments/util");
6
+ var models_1 = require("./models");
7
+ function buildProjectModel(projectJSON) {
8
+ var segments = projectJSON['segments']
9
+ ? projectJSON['segments'].map(function (s) { return (0, util_2.buildSegmentModel)(s); })
10
+ : [];
11
+ var 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;
@@ -0,0 +1,26 @@
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
+ };
@@ -0,0 +1,31 @@
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
+ };
@@ -0,0 +1,6 @@
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;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.evaluateIdentityInSegment = exports.getIdentitySegments = void 0;
4
+ var hashing_1 = require("../utils/hashing");
5
+ var constants_1 = require("./constants");
6
+ function getIdentitySegments(environment, identity, overrideTraits) {
7
+ return environment.project.segments.filter(function (segment) {
8
+ return evaluateIdentityInSegment(identity, segment, overrideTraits);
9
+ });
10
+ }
11
+ exports.getIdentitySegments = getIdentitySegments;
12
+ function evaluateIdentityInSegment(identity, segment, overrideTraits) {
13
+ return (segment.rules.length > 0 &&
14
+ segment.rules.filter(function (rule) {
15
+ return traitsMatchSegmentRule(overrideTraits || identity.identityTraits, rule, segment.id, identity.compositeKey);
16
+ }).length === segment.rules.length);
17
+ }
18
+ exports.evaluateIdentityInSegment = evaluateIdentityInSegment;
19
+ function traitsMatchSegmentRule(identityTraits, rule, segmentId, identityId) {
20
+ var matchesConditions = rule.conditions.length > 0
21
+ ? rule.matchingFunction()(rule.conditions.map(function (condition) {
22
+ return traitsMatchSegmentCondition(identityTraits, condition, segmentId, identityId);
23
+ }))
24
+ : true;
25
+ return (matchesConditions &&
26
+ rule.rules.filter(function (rule) {
27
+ return traitsMatchSegmentRule(identityTraits, rule, segmentId, identityId);
28
+ }).length === rule.rules.length);
29
+ }
30
+ function traitsMatchSegmentCondition(identityTraits, condition, segmentId, identityId) {
31
+ if (condition.operator == constants_1.PERCENTAGE_SPLIT) {
32
+ return (0, hashing_1.getHashedPercentateForObjIds)([segmentId, identityId]) <= parseFloat(condition.value);
33
+ }
34
+ var traits = identityTraits.filter(function (t) { return t.traitKey === condition.property_; });
35
+ var trait = traits.length > 0 ? traits[0] : undefined;
36
+ return trait ? condition.matchesTraitValue(trait.traitValue) : false;
37
+ }
@@ -0,0 +1,37 @@
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 const semverMatchingFunction: {
8
+ [x: string]: ((thisValue: any, otherValue: any) => any) | ((thisValue: any, otherValue: any) => boolean);
9
+ };
10
+ export declare const getMatchingFunctions: (semver: boolean) => {
11
+ [x: string]: (thisValue: any, otherValue: any) => any;
12
+ };
13
+ export declare class SegmentConditionModel {
14
+ EXCEPTION_OPERATOR_METHODS: {
15
+ [key: string]: string;
16
+ };
17
+ operator: string;
18
+ value: string;
19
+ property_: string | undefined;
20
+ constructor(operator: string, value: string, property?: string);
21
+ matchesTraitValue(traitValue: any): any;
22
+ }
23
+ export declare class SegmentRuleModel {
24
+ type: string;
25
+ rules: SegmentRuleModel[];
26
+ conditions: SegmentConditionModel[];
27
+ constructor(type: string);
28
+ static none(iterable: Array<any>): boolean;
29
+ matchingFunction(): CallableFunction;
30
+ }
31
+ export declare class SegmentModel {
32
+ id: number;
33
+ name: string;
34
+ rules: SegmentRuleModel[];
35
+ featureStates: FeatureStateModel[];
36
+ constructor(id: number, name: string);
37
+ }
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _a, _b;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.SegmentModel = exports.SegmentRuleModel = exports.SegmentConditionModel = exports.getMatchingFunctions = exports.semverMatchingFunction = exports.matchingFunctions = exports.any = exports.all = void 0;
19
+ var semver_1 = __importDefault(require("semver"));
20
+ var utils_1 = require("../utils");
21
+ var constants_1 = require("./constants");
22
+ var util_1 = require("./util");
23
+ var all = function (iterable) { return iterable.filter(function (e) { return !!e; }).length === iterable.length; };
24
+ exports.all = all;
25
+ var any = function (iterable) { return iterable.filter(function (e) { return !!e; }).length > 0; };
26
+ exports.any = any;
27
+ exports.matchingFunctions = (_a = {},
28
+ _a[constants_1.CONDITION_OPERATORS.EQUAL] = function (thisValue, otherValue) { return thisValue == otherValue; },
29
+ _a[constants_1.CONDITION_OPERATORS.GREATER_THAN] = function (thisValue, otherValue) { return otherValue > thisValue; },
30
+ _a[constants_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE] = function (thisValue, otherValue) {
31
+ return otherValue >= thisValue;
32
+ },
33
+ _a[constants_1.CONDITION_OPERATORS.LESS_THAN] = function (thisValue, otherValue) { return thisValue > otherValue; },
34
+ _a[constants_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE] = function (thisValue, otherValue) {
35
+ return thisValue >= otherValue;
36
+ },
37
+ _a[constants_1.CONDITION_OPERATORS.NOT_EQUAL] = function (thisValue, otherValue) { return thisValue != otherValue; },
38
+ _a[constants_1.CONDITION_OPERATORS.CONTAINS] = function (thisValue, otherValue) {
39
+ return otherValue.includes(thisValue);
40
+ },
41
+ _a);
42
+ exports.semverMatchingFunction = __assign(__assign({}, exports.matchingFunctions), (_b = {}, _b[constants_1.CONDITION_OPERATORS.EQUAL] = function (thisValue, otherValue) { return semver_1.default.eq(thisValue, otherValue); }, _b[constants_1.CONDITION_OPERATORS.GREATER_THAN] = function (thisValue, otherValue) { return semver_1.default.gt(otherValue, thisValue); }, _b[constants_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE] = function (thisValue, otherValue) {
43
+ return semver_1.default.gte(otherValue, thisValue);
44
+ }, _b[constants_1.CONDITION_OPERATORS.LESS_THAN] = function (thisValue, otherValue) { return semver_1.default.gt(thisValue, otherValue); }, _b[constants_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE] = function (thisValue, otherValue) {
45
+ return semver_1.default.gte(thisValue, otherValue);
46
+ }, _b));
47
+ var getMatchingFunctions = function (semver) { return (semver ? exports.semverMatchingFunction : exports.matchingFunctions); };
48
+ exports.getMatchingFunctions = getMatchingFunctions;
49
+ var SegmentConditionModel = /** @class */ (function () {
50
+ function SegmentConditionModel(operator, value, property) {
51
+ var _a;
52
+ this.EXCEPTION_OPERATOR_METHODS = (_a = {},
53
+ _a[constants_1.NOT_CONTAINS] = 'evaluateNotContains',
54
+ _a[constants_1.REGEX] = 'evaluateRegex',
55
+ _a);
56
+ this.operator = operator;
57
+ this.value = value;
58
+ this.property_ = property;
59
+ }
60
+ SegmentConditionModel.prototype.matchesTraitValue = function (traitValue) {
61
+ var _this = this;
62
+ var evaluators = {
63
+ evaluateNotContains: function (traitValue) {
64
+ return !traitValue.includes(_this.value);
65
+ },
66
+ evaluateRegex: function (traitValue) {
67
+ return !!traitValue.match(new RegExp(_this.value));
68
+ }
69
+ };
70
+ // TODO: move this logic to the evaluator module
71
+ if (this.EXCEPTION_OPERATOR_METHODS[this.operator]) {
72
+ var evaluatorFunction = evaluators[this.EXCEPTION_OPERATOR_METHODS[this.operator]];
73
+ return evaluatorFunction(traitValue);
74
+ }
75
+ var defaultFunction = function (x, y) { return false; };
76
+ var matchingFunctionSet = (0, exports.getMatchingFunctions)((0, util_1.isSemver)(this.value));
77
+ var matchingFunction = matchingFunctionSet[this.operator] || defaultFunction;
78
+ var traitType = (0, util_1.isSemver)(this.value) ? 'semver' : typeof traitValue;
79
+ var castToTypeOfTraitValue = (0, utils_1.getCastingFunction)(traitType);
80
+ return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
81
+ };
82
+ return SegmentConditionModel;
83
+ }());
84
+ exports.SegmentConditionModel = SegmentConditionModel;
85
+ var SegmentRuleModel = /** @class */ (function () {
86
+ function SegmentRuleModel(type) {
87
+ this.rules = [];
88
+ this.conditions = [];
89
+ this.type = type;
90
+ }
91
+ SegmentRuleModel.none = function (iterable) {
92
+ return iterable.filter(function (e) { return !!e; }).length === 0;
93
+ };
94
+ SegmentRuleModel.prototype.matchingFunction = function () {
95
+ var _a;
96
+ return (_a = {},
97
+ _a[constants_1.ANY_RULE] = exports.any,
98
+ _a[constants_1.ALL_RULE] = exports.all,
99
+ _a[constants_1.NONE_RULE] = SegmentRuleModel.none,
100
+ _a)[this.type];
101
+ };
102
+ return SegmentRuleModel;
103
+ }());
104
+ exports.SegmentRuleModel = SegmentRuleModel;
105
+ var SegmentModel = /** @class */ (function () {
106
+ function SegmentModel(id, name) {
107
+ this.rules = [];
108
+ this.featureStates = [];
109
+ this.id = id;
110
+ this.name = name;
111
+ }
112
+ return SegmentModel;
113
+ }());
114
+ exports.SegmentModel = SegmentModel;
@@ -0,0 +1,6 @@
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;
5
+ export declare function isSemver(value: any): boolean;
6
+ export declare function removeSemverSuffix(value: string): string;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removeSemverSuffix = exports.isSemver = exports.buildSegmentModel = exports.buildSegmentRuleModel = exports.buildSegmentConditionModel = void 0;
4
+ var util_1 = require("../features/util");
5
+ var 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
+ var ruleModel = new models_1.SegmentRuleModel(ruleModelJSON.type);
12
+ ruleModel.rules = ruleModelJSON.rules.map(function (r) { return buildSegmentRuleModel(r); });
13
+ ruleModel.conditions = ruleModelJSON.conditions.map(function (c) { return buildSegmentConditionModel(c); });
14
+ return ruleModel;
15
+ }
16
+ exports.buildSegmentRuleModel = buildSegmentRuleModel;
17
+ function buildSegmentModel(segmentModelJSON) {
18
+ var model = new models_1.SegmentModel(segmentModelJSON.id, segmentModelJSON.name);
19
+ model.featureStates = segmentModelJSON['feature_states'].map(function (fs) {
20
+ return (0, util_1.buildFeatureStateModel)(fs);
21
+ });
22
+ model.rules = segmentModelJSON['rules'].map(function (r) { return buildSegmentRuleModel(r); });
23
+ return model;
24
+ }
25
+ exports.buildSegmentModel = buildSegmentModel;
26
+ function isSemver(value) {
27
+ return typeof value == 'string' && value.endsWith(':semver');
28
+ }
29
+ exports.isSemver = isSemver;
30
+ function removeSemverSuffix(value) {
31
+ return value.replace(':semver', '');
32
+ }
33
+ exports.removeSemverSuffix = removeSemverSuffix;
@@ -0,0 +1,3 @@
1
+ import { FeatureStateModel } from '../features/models';
2
+ export declare class IdentityFeaturesList extends Array<FeatureStateModel> {
3
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.IdentityFeaturesList = void 0;
19
+ var IdentityFeaturesList = /** @class */ (function (_super) {
20
+ __extends(IdentityFeaturesList, _super);
21
+ function IdentityFeaturesList() {
22
+ return _super !== null && _super.apply(this, arguments) || this;
23
+ }
24
+ return IdentityFeaturesList;
25
+ }(Array));
26
+ exports.IdentityFeaturesList = IdentityFeaturesList;
@@ -0,0 +1,2 @@
1
+ export declare class FeatureStateNotFound extends Error {
2
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.FeatureStateNotFound = void 0;
19
+ var FeatureStateNotFound = /** @class */ (function (_super) {
20
+ __extends(FeatureStateNotFound, _super);
21
+ function FeatureStateNotFound() {
22
+ return _super !== null && _super.apply(this, arguments) || this;
23
+ }
24
+ return FeatureStateNotFound;
25
+ }(Error));
26
+ exports.FeatureStateNotFound = FeatureStateNotFound;
@@ -0,0 +1,9 @@
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;
@@ -0,0 +1,60 @@
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
+ var md5_1 = __importDefault(require("md5"));
8
+ var big_integer_1 = __importDefault(require("big-integer"));
9
+ var makeRepeated = function (arr, repeats) {
10
+ return Array.from({ length: repeats }, function () { return arr; }).flat();
11
+ };
12
+ // https://stackoverflow.com/questions/12532871/how-to-convert-a-very-large-hex-number-to-decimal-in-javascript
13
+ function h2d(s) {
14
+ function add(x, y) {
15
+ var c = 0, r = [];
16
+ var x = x.split('').map(Number);
17
+ var y = y.split('').map(Number);
18
+ while (x.length || y.length) {
19
+ var s = (x.pop() || 0) + (y.pop() || 0) + c;
20
+ r.unshift(s < 10 ? s : s - 10);
21
+ c = s < 10 ? 0 : 1;
22
+ }
23
+ if (c)
24
+ r.unshift(c);
25
+ return r.join('');
26
+ }
27
+ var dec = '0';
28
+ s.split('').forEach(function (chr) {
29
+ var n = parseInt(chr, 16);
30
+ for (var t = 8; t; t >>= 1) {
31
+ dec = add(dec, dec);
32
+ if (n & t)
33
+ dec = add(dec, '1');
34
+ }
35
+ });
36
+ return dec;
37
+ }
38
+ /**
39
+ * Given a list of object ids, get a floating point number between 0 and 1 based on
40
+ * the hash of those ids. This should give the same value every time for any list of ids.
41
+ *
42
+ * @param {Array<any>} objectIds list of object ids to calculate the has for
43
+ * @param {} iterations=1 num times to include each id in the generated string to hash
44
+ * @returns number number between 0 (inclusive) and 100 (exclusive)
45
+ */
46
+ function getHashedPercentateForObjIds(objectIds, iterations) {
47
+ if (iterations === void 0) { iterations = 1; }
48
+ var toHash = makeRepeated(objectIds, iterations).join(',');
49
+ var hashedValue = (0, md5_1.default)(toHash);
50
+ var hashedInt = (0, big_integer_1.default)(h2d(hashedValue));
51
+ var value = (hashedInt.mod(9999).toJSNumber() / 9998) * 100;
52
+ // we ignore this for it's nearly impossible use case to catch
53
+ /* istanbul ignore next */
54
+ if (value === 100) {
55
+ /* istanbul ignore next */
56
+ return getHashedPercentateForObjIds(objectIds, iterations + 1);
57
+ }
58
+ return value;
59
+ }
60
+ exports.getHashedPercentateForObjIds = getHashedPercentateForObjIds;
@@ -0,0 +1 @@
1
+ export declare function getCastingFunction(traitType: 'boolean' | 'string' | 'number' | 'semver' | any): CallableFunction;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCastingFunction = void 0;
4
+ var util_1 = require("../segments/util");
5
+ function getCastingFunction(traitType) {
6
+ switch (traitType) {
7
+ case 'boolean':
8
+ return function (x) { return !['False', 'false'].includes(x); };
9
+ case 'number':
10
+ return function (x) { return parseFloat(x); };
11
+ case 'semver':
12
+ return function (x) { return (0, util_1.removeSemverSuffix)(x); };
13
+ default:
14
+ return function (x) { return String(x); };
15
+ }
16
+ }
17
+ exports.getCastingFunction = getCastingFunction;
@@ -0,0 +1 @@
1
+ export { Flagsmith } from './sdk';
package/build/index.js ADDED
@@ -0,0 +1,11 @@
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
+ var 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;
@@ -0,0 +1,28 @@
1
+ export declare class AnalyticsProcessor {
2
+ private analyticsEndpoint;
3
+ private environmentKey;
4
+ private lastFlushed;
5
+ analyticsData: {
6
+ [key: string]: any;
7
+ };
8
+ private timeout;
9
+ /**
10
+ * AnalyticsProcessor is used to track how often individual Flags are evaluated within
11
+ * the Flagsmith SDK. Docs: https://docs.flagsmith.com/advanced-use/flag-analytics.
12
+ *
13
+ * @param data.environmentKey environment key obtained from the Flagsmith UI
14
+ * @param data.baseApiUrl base api url to override when using self hosted version
15
+ * @param data.timeout used to tell requests to stop waiting for a response after a
16
+ given number of seconds
17
+ */
18
+ constructor(data: {
19
+ environmentKey: string;
20
+ baseApiUrl: string;
21
+ timeout?: number;
22
+ });
23
+ /**
24
+ * Sends all the collected data to the api asynchronously and resets the timer
25
+ */
26
+ flush(): Promise<void>;
27
+ trackFeature(featureName: string): void;
28
+ }