flagsmith-nodejs 2.0.0-beta.5 → 2.0.0-beta.6

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 (31) hide show
  1. package/build/flagsmith-engine/environments/integrations/models.js +7 -4
  2. package/build/flagsmith-engine/environments/models.js +12 -23
  3. package/build/flagsmith-engine/environments/util.js +9 -7
  4. package/build/flagsmith-engine/features/models.js +64 -47
  5. package/build/flagsmith-engine/features/util.js +5 -5
  6. package/build/flagsmith-engine/identities/models.js +96 -31
  7. package/build/flagsmith-engine/identities/traits/models.js +4 -5
  8. package/build/flagsmith-engine/identities/util.js +33 -9
  9. package/build/flagsmith-engine/index.js +74 -22
  10. package/build/flagsmith-engine/organisations/models.js +11 -11
  11. package/build/flagsmith-engine/organisations/util.js +1 -1
  12. package/build/flagsmith-engine/projects/models.js +5 -8
  13. package/build/flagsmith-engine/projects/util.js +6 -6
  14. package/build/flagsmith-engine/segments/evaluators.js +17 -9
  15. package/build/flagsmith-engine/segments/models.js +67 -58
  16. package/build/flagsmith-engine/segments/util.js +10 -8
  17. package/build/flagsmith-engine/utils/collections.js +90 -9
  18. package/build/flagsmith-engine/utils/errors.js +22 -2
  19. package/build/flagsmith-engine/utils/hashing/index.js +11 -8
  20. package/build/flagsmith-engine/utils/index.js +3 -3
  21. package/build/index.js +1 -1
  22. package/build/sdk/analytics.js +72 -29
  23. package/build/sdk/errors.js +29 -4
  24. package/build/sdk/index.js +263 -140
  25. package/build/sdk/models.js +95 -47
  26. package/build/sdk/polling_manager.js +58 -16
  27. package/build/sdk/utils.js +67 -18
  28. package/example/package-lock.json +1 -996
  29. package/example/server/api/index.js +1 -1
  30. package/package.json +2 -1
  31. package/tsconfig.json +2 -1
@@ -1,85 +1,132 @@
1
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
+ var __values = (this && this.__values) || function(o) {
18
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
19
+ if (m) return m.call(o);
20
+ if (o && typeof o.length === "number") return {
21
+ next: function () {
22
+ if (o && i >= o.length) o = void 0;
23
+ return { value: o && o[i++], done: !o };
24
+ }
25
+ };
26
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
27
+ };
2
28
  Object.defineProperty(exports, "__esModule", { value: true });
3
29
  exports.Flags = exports.Flag = exports.DefaultFlag = exports.BaseFlag = void 0;
4
- class BaseFlag {
5
- enabled;
6
- value;
7
- isDefault;
8
- constructor(value, enabled, isDefault) {
30
+ var BaseFlag = /** @class */ (function () {
31
+ function BaseFlag(value, enabled, isDefault) {
9
32
  this.value = value;
10
33
  this.enabled = enabled;
11
34
  this.isDefault = isDefault;
12
35
  }
13
- }
36
+ return BaseFlag;
37
+ }());
14
38
  exports.BaseFlag = BaseFlag;
15
- class DefaultFlag extends BaseFlag {
16
- constructor(value, enabled) {
17
- super(value, enabled, true);
39
+ var DefaultFlag = /** @class */ (function (_super) {
40
+ __extends(DefaultFlag, _super);
41
+ function DefaultFlag(value, enabled) {
42
+ return _super.call(this, value, enabled, true) || this;
18
43
  }
19
- }
44
+ return DefaultFlag;
45
+ }(BaseFlag));
20
46
  exports.DefaultFlag = DefaultFlag;
21
- class Flag extends BaseFlag {
22
- featureId;
23
- featureName;
24
- constructor(params) {
25
- super(params.value, params.enabled, !!params.isDefault);
26
- this.featureId = params.featureId;
27
- this.featureName = params.featureName;
47
+ var Flag = /** @class */ (function (_super) {
48
+ __extends(Flag, _super);
49
+ function Flag(params) {
50
+ var _this = _super.call(this, params.value, params.enabled, !!params.isDefault) || this;
51
+ _this.featureId = params.featureId;
52
+ _this.featureName = params.featureName;
53
+ return _this;
28
54
  }
29
- static fromFeatureStateModel(fsm, identityId) {
55
+ Flag.fromFeatureStateModel = function (fsm, identityId) {
30
56
  return new Flag({
31
57
  value: fsm.getValue(identityId),
32
58
  enabled: fsm.enabled,
33
59
  featureId: fsm.feature.id,
34
60
  featureName: fsm.feature.name
35
61
  });
36
- }
37
- static fromAPIFlag(flagData) {
62
+ };
63
+ Flag.fromAPIFlag = function (flagData) {
38
64
  return new Flag({
39
65
  enabled: flagData['enabled'],
40
66
  value: flagData['feature_state_value'] || flagData['value'],
41
67
  featureId: flagData['feature']['id'],
42
68
  featureName: flagData['feature']['name']
43
69
  });
44
- }
45
- }
70
+ };
71
+ return Flag;
72
+ }(BaseFlag));
46
73
  exports.Flag = Flag;
47
- class Flags {
48
- flags = {};
49
- defaultFlagHandler;
50
- analyticsProcessor;
51
- constructor(data) {
74
+ var Flags = /** @class */ (function () {
75
+ function Flags(data) {
76
+ this.flags = {};
52
77
  this.flags = data.flags;
53
78
  this.defaultFlagHandler = data.defaultFlagHandler;
54
79
  this.analyticsProcessor = data.analyticsProcessor;
55
80
  }
56
- static fromFeatureStateModels(data) {
57
- const flags = {};
58
- for (const fs of data.featureStates) {
59
- flags[fs.feature.name] = Flag.fromFeatureStateModel(fs, data.identityID);
81
+ Flags.fromFeatureStateModels = function (data) {
82
+ var e_1, _a;
83
+ var flags = {};
84
+ try {
85
+ for (var _b = __values(data.featureStates), _c = _b.next(); !_c.done; _c = _b.next()) {
86
+ var fs = _c.value;
87
+ flags[fs.feature.name] = Flag.fromFeatureStateModel(fs, data.identityID);
88
+ }
89
+ }
90
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
91
+ finally {
92
+ try {
93
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
94
+ }
95
+ finally { if (e_1) throw e_1.error; }
60
96
  }
61
97
  return new Flags({
62
98
  flags: flags,
63
99
  defaultFlagHandler: data.defaultFlagHandler,
64
100
  analyticsProcessor: data.analyticsProcessor
65
101
  });
66
- }
67
- static fromAPIFlags(data) {
68
- const flags = {};
69
- for (const flagData of data.apiFlags) {
70
- flags[flagData['feature']['name']] = Flag.fromAPIFlag(flagData);
102
+ };
103
+ Flags.fromAPIFlags = function (data) {
104
+ var e_2, _a;
105
+ var flags = {};
106
+ try {
107
+ for (var _b = __values(data.apiFlags), _c = _b.next(); !_c.done; _c = _b.next()) {
108
+ var flagData = _c.value;
109
+ flags[flagData['feature']['name']] = Flag.fromAPIFlag(flagData);
110
+ }
111
+ }
112
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
113
+ finally {
114
+ try {
115
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
116
+ }
117
+ finally { if (e_2) throw e_2.error; }
71
118
  }
72
119
  return new Flags({
73
120
  flags: flags,
74
121
  defaultFlagHandler: data.defaultFlagHandler,
75
122
  analyticsProcessor: data.analyticsProcessor
76
123
  });
77
- }
78
- allFlags() {
124
+ };
125
+ Flags.prototype.allFlags = function () {
79
126
  return Object.values(this.flags);
80
- }
81
- getFlag(featureName) {
82
- const flag = this.flags[featureName];
127
+ };
128
+ Flags.prototype.getFlag = function (featureName) {
129
+ var flag = this.flags[featureName];
83
130
  if (!flag) {
84
131
  if (this.defaultFlagHandler) {
85
132
  return this.defaultFlagHandler(featureName);
@@ -90,12 +137,13 @@ class Flags {
90
137
  this.analyticsProcessor.trackFeature(flag.featureId);
91
138
  }
92
139
  return flag;
93
- }
94
- getFeatureValue(featureName) {
140
+ };
141
+ Flags.prototype.getFeatureValue = function (featureName) {
95
142
  return this.getFlag(featureName).value;
96
- }
97
- isFeatureEnabled(featureName) {
143
+ };
144
+ Flags.prototype.isFeatureEnabled = function (featureName) {
98
145
  return this.getFlag(featureName).enabled;
99
- }
100
- }
146
+ };
147
+ return Flags;
148
+ }());
101
149
  exports.Flags = Flags;
@@ -1,31 +1,73 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
2
38
  Object.defineProperty(exports, "__esModule", { value: true });
3
39
  exports.EnvironmentDataPollingManager = void 0;
4
- class EnvironmentDataPollingManager {
5
- interval;
6
- main;
7
- refreshIntervalSeconds;
8
- constructor(main, refreshIntervalSeconds) {
40
+ var EnvironmentDataPollingManager = /** @class */ (function () {
41
+ function EnvironmentDataPollingManager(main, refreshIntervalSeconds) {
9
42
  this.main = main;
10
43
  this.refreshIntervalSeconds = refreshIntervalSeconds;
11
44
  }
12
- start() {
13
- const updateEnvironment = () => {
14
- if (this.interval)
15
- clearInterval(this.interval);
16
- this.interval = setInterval(async () => {
17
- await this.main.updateEnvironment();
18
- }, this.refreshIntervalSeconds * 1000);
45
+ EnvironmentDataPollingManager.prototype.start = function () {
46
+ var _this = this;
47
+ var updateEnvironment = function () {
48
+ if (_this.interval)
49
+ clearInterval(_this.interval);
50
+ _this.interval = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
51
+ return __generator(this, function (_a) {
52
+ switch (_a.label) {
53
+ case 0: return [4 /*yield*/, this.main.updateEnvironment()];
54
+ case 1:
55
+ _a.sent();
56
+ return [2 /*return*/];
57
+ }
58
+ });
59
+ }); }, _this.refreshIntervalSeconds * 1000);
19
60
  };
20
61
  // todo: this call should be awaited for getIdentityFlags/getEnvironmentFlags when enableLocalEvaluation is true
21
62
  this.main.updateEnvironment();
22
63
  updateEnvironment();
23
- }
24
- stop() {
64
+ };
65
+ EnvironmentDataPollingManager.prototype.stop = function () {
25
66
  if (!this.interval) {
26
67
  return;
27
68
  }
28
69
  clearInterval(this.interval);
29
- }
30
- }
70
+ };
71
+ return EnvironmentDataPollingManager;
72
+ }());
31
73
  exports.EnvironmentDataPollingManager = EnvironmentDataPollingManager;
@@ -1,43 +1,92 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
2
38
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
40
  };
5
41
  Object.defineProperty(exports, "__esModule", { value: true });
6
42
  exports.retryFetch = exports.delay = exports.generateIdentitiesData = void 0;
7
- const node_fetch_1 = __importDefault(require("node-fetch"));
43
+ var node_fetch_1 = __importDefault(require("node-fetch"));
8
44
  // @ts-ignore
9
45
  if (typeof node_fetch_1.default.default !== 'undefined')
10
46
  node_fetch_1.default = node_fetch_1.default.default;
11
47
  function generateIdentitiesData(identifier, traits) {
12
- const traitsGenerated = Object.entries(traits || {}).map(trait => ({
48
+ var traitsGenerated = Object.entries(traits || {}).map(function (trait) { return ({
13
49
  trait_key: trait[0],
14
50
  trait_value: trait[1]
15
- }));
51
+ }); });
16
52
  return {
17
53
  identifier: identifier,
18
54
  traits: traitsGenerated
19
55
  };
20
56
  }
21
57
  exports.generateIdentitiesData = generateIdentitiesData;
22
- const delay = (ms) => new Promise(resolve => setTimeout(() => resolve(undefined), ms));
58
+ var delay = function (ms) {
59
+ return new Promise(function (resolve) { return setTimeout(function () { return resolve(undefined); }, ms); });
60
+ };
23
61
  exports.delay = delay;
24
- const retryFetch = (url, fetchOptions = {}, retries = 3, retryDelay = 1000, timeout) => {
25
- return new Promise((resolve, reject) => {
62
+ var retryFetch = function (url, fetchOptions, retries, retryDelay, timeout) {
63
+ if (fetchOptions === void 0) { fetchOptions = {}; }
64
+ if (retries === void 0) { retries = 3; }
65
+ if (retryDelay === void 0) { retryDelay = 1000; }
66
+ return new Promise(function (resolve, reject) {
26
67
  // check for timeout
27
68
  if (timeout)
28
- setTimeout(() => reject('error: timeout'), timeout);
29
- const wrapper = (n) => {
69
+ setTimeout(function () { return reject('error: timeout'); }, timeout);
70
+ var wrapper = function (n) {
30
71
  (0, node_fetch_1.default)(url, fetchOptions)
31
- .then(res => resolve(res))
32
- .catch(async (err) => {
33
- if (n > 0) {
34
- await (0, exports.delay)(retryDelay);
35
- wrapper(--n);
36
- }
37
- else {
38
- reject(err);
39
- }
40
- });
72
+ .then(function (res) { return resolve(res); })
73
+ .catch(function (err) { return __awaiter(void 0, void 0, void 0, function () {
74
+ return __generator(this, function (_a) {
75
+ switch (_a.label) {
76
+ case 0:
77
+ if (!(n > 0)) return [3 /*break*/, 2];
78
+ return [4 /*yield*/, (0, exports.delay)(retryDelay)];
79
+ case 1:
80
+ _a.sent();
81
+ wrapper(--n);
82
+ return [3 /*break*/, 3];
83
+ case 2:
84
+ reject(err);
85
+ _a.label = 3;
86
+ case 3: return [2 /*return*/];
87
+ }
88
+ });
89
+ }); });
41
90
  };
42
91
  wrapper(retries);
43
92
  });