flagsmith-nodejs 2.1.2 → 2.2.2-beta.1

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.
package/build/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { AnalyticsProcessor, FlagsmithAPIError, FlagsmithClientError, EnvironmentDataPollingManager, FlagsmithCache, DefaultFlag, Flags } from './sdk';
1
+ export { AnalyticsProcessor, FlagsmithAPIError, FlagsmithClientError, EnvironmentDataPollingManager, FlagsmithCache, DefaultFlag, Flags, default } from './sdk';
2
2
  export { EnvironmentModel, IntegrationModel, FeatureStateModel, IdentityModel, TraitModel, SegmentModel, OrganisationModel } from './flagsmith-engine';
package/build/index.js CHANGED
@@ -3,15 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.OrganisationModel = exports.SegmentModel = exports.TraitModel = exports.IdentityModel = exports.FeatureStateModel = exports.IntegrationModel = exports.EnvironmentModel = exports.Flags = exports.DefaultFlag = exports.EnvironmentDataPollingManager = exports.FlagsmithClientError = exports.FlagsmithAPIError = exports.AnalyticsProcessor = void 0;
7
- var sdk_1 = __importDefault(require("./sdk"));
8
- var sdk_2 = require("./sdk");
9
- Object.defineProperty(exports, "AnalyticsProcessor", { enumerable: true, get: function () { return sdk_2.AnalyticsProcessor; } });
10
- Object.defineProperty(exports, "FlagsmithAPIError", { enumerable: true, get: function () { return sdk_2.FlagsmithAPIError; } });
11
- Object.defineProperty(exports, "FlagsmithClientError", { enumerable: true, get: function () { return sdk_2.FlagsmithClientError; } });
12
- Object.defineProperty(exports, "EnvironmentDataPollingManager", { enumerable: true, get: function () { return sdk_2.EnvironmentDataPollingManager; } });
13
- Object.defineProperty(exports, "DefaultFlag", { enumerable: true, get: function () { return sdk_2.DefaultFlag; } });
14
- Object.defineProperty(exports, "Flags", { enumerable: true, get: function () { return sdk_2.Flags; } });
6
+ exports.OrganisationModel = exports.SegmentModel = exports.TraitModel = exports.IdentityModel = exports.FeatureStateModel = exports.IntegrationModel = exports.EnvironmentModel = exports.default = exports.Flags = exports.DefaultFlag = exports.EnvironmentDataPollingManager = exports.FlagsmithClientError = exports.FlagsmithAPIError = exports.AnalyticsProcessor = void 0;
7
+ var sdk_1 = require("./sdk");
8
+ Object.defineProperty(exports, "AnalyticsProcessor", { enumerable: true, get: function () { return sdk_1.AnalyticsProcessor; } });
9
+ Object.defineProperty(exports, "FlagsmithAPIError", { enumerable: true, get: function () { return sdk_1.FlagsmithAPIError; } });
10
+ Object.defineProperty(exports, "FlagsmithClientError", { enumerable: true, get: function () { return sdk_1.FlagsmithClientError; } });
11
+ Object.defineProperty(exports, "EnvironmentDataPollingManager", { enumerable: true, get: function () { return sdk_1.EnvironmentDataPollingManager; } });
12
+ Object.defineProperty(exports, "DefaultFlag", { enumerable: true, get: function () { return sdk_1.DefaultFlag; } });
13
+ Object.defineProperty(exports, "Flags", { enumerable: true, get: function () { return sdk_1.Flags; } });
14
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(sdk_1).default; } });
15
15
  var flagsmith_engine_1 = require("./flagsmith-engine");
16
16
  Object.defineProperty(exports, "EnvironmentModel", { enumerable: true, get: function () { return flagsmith_engine_1.EnvironmentModel; } });
17
17
  Object.defineProperty(exports, "IntegrationModel", { enumerable: true, get: function () { return flagsmith_engine_1.IntegrationModel; } });
@@ -20,4 +20,3 @@ Object.defineProperty(exports, "IdentityModel", { enumerable: true, get: functio
20
20
  Object.defineProperty(exports, "TraitModel", { enumerable: true, get: function () { return flagsmith_engine_1.TraitModel; } });
21
21
  Object.defineProperty(exports, "SegmentModel", { enumerable: true, get: function () { return flagsmith_engine_1.SegmentModel; } });
22
22
  Object.defineProperty(exports, "OrganisationModel", { enumerable: true, get: function () { return flagsmith_engine_1.OrganisationModel; } });
23
- module.exports = sdk_1.default;
@@ -5,20 +5,20 @@ export declare class AnalyticsProcessor {
5
5
  analyticsData: {
6
6
  [key: string]: any;
7
7
  };
8
- private timeout;
8
+ private requestTimeoutMs;
9
9
  /**
10
10
  * AnalyticsProcessor is used to track how often individual Flags are evaluated within
11
11
  * the Flagsmith SDK. Docs: https://docs.flagsmith.com/advanced-use/flag-analytics.
12
12
  *
13
13
  * @param data.environmentKey environment key obtained from the Flagsmith UI
14
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
15
+ * @param data.requestTimeoutMs used to tell requests to stop waiting for a response after a
16
+ given number of milliseconds
17
17
  */
18
18
  constructor(data: {
19
19
  environmentKey: string;
20
20
  baseApiUrl: string;
21
- timeout?: number;
21
+ requestTimeoutMs?: number;
22
22
  });
23
23
  /**
24
24
  * Sends all the collected data to the api asynchronously and resets the timer
@@ -51,16 +51,16 @@ var AnalyticsProcessor = /** @class */ (function () {
51
51
  *
52
52
  * @param data.environmentKey environment key obtained from the Flagsmith UI
53
53
  * @param data.baseApiUrl base api url to override when using self hosted version
54
- * @param data.timeout used to tell requests to stop waiting for a response after a
55
- given number of seconds
54
+ * @param data.requestTimeoutMs used to tell requests to stop waiting for a response after a
55
+ given number of milliseconds
56
56
  */
57
57
  function AnalyticsProcessor(data) {
58
- this.timeout = 3;
58
+ this.requestTimeoutMs = 3000;
59
59
  this.analyticsEndpoint = data.baseApiUrl + ANALYTICS_ENDPOINT;
60
60
  this.environmentKey = data.environmentKey;
61
61
  this.lastFlushed = Date.now();
62
62
  this.analyticsData = {};
63
- this.timeout = data.timeout || this.timeout;
63
+ this.requestTimeoutMs = data.requestTimeoutMs || this.requestTimeoutMs;
64
64
  }
65
65
  /**
66
66
  * Sends all the collected data to the api asynchronously and resets the timer
@@ -76,7 +76,7 @@ var AnalyticsProcessor = /** @class */ (function () {
76
76
  return [4 /*yield*/, (0, node_fetch_1.default)(this.analyticsEndpoint, {
77
77
  method: 'POST',
78
78
  body: JSON.stringify(this.analyticsData),
79
- timeout: this.timeout,
79
+ timeout: this.requestTimeoutMs,
80
80
  headers: {
81
81
  'Content-Type': 'application/json',
82
82
  'X-Environment-Key': this.environmentKey
@@ -15,6 +15,7 @@ export declare class Flagsmith {
15
15
  [key: string]: any;
16
16
  };
17
17
  requestTimeoutSeconds?: number;
18
+ requestTimeoutMs?: number;
18
19
  enableLocalEvaluation?: boolean;
19
20
  environmentRefreshIntervalSeconds: number;
20
21
  retries?: number;
@@ -113,6 +114,7 @@ export declare class Flagsmith {
113
114
  * You only need to call this if you wish to bypass environmentRefreshIntervalSeconds.
114
115
  */
115
116
  updateEnvironment(): Promise<void>;
117
+ close(): Promise<void>;
116
118
  private getJSONResponse;
117
119
  /**
118
120
  * This promise ensures that the environment is retrieved before attempting to locally evaluate.
@@ -125,6 +125,7 @@ var Flagsmith = /** @class */ (function () {
125
125
  this.apiUrl = data.apiUrl || this.apiUrl;
126
126
  this.customHeaders = data.customHeaders;
127
127
  this.requestTimeoutSeconds = data.requestTimeoutSeconds;
128
+ this.requestTimeoutMs = data.requestTimeoutSeconds ? data.requestTimeoutSeconds * 1000 : undefined;
128
129
  this.enableLocalEvaluation = data.enableLocalEvaluation;
129
130
  this.environmentRefreshIntervalSeconds =
130
131
  data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds;
@@ -154,7 +155,7 @@ var Flagsmith = /** @class */ (function () {
154
155
  ? new analytics_1.AnalyticsProcessor({
155
156
  environmentKey: this.environmentKey,
156
157
  baseApiUrl: this.apiUrl,
157
- timeout: this.requestTimeoutSeconds
158
+ requestTimeoutMs: this.requestTimeoutMs
158
159
  })
159
160
  : undefined;
160
161
  }
@@ -182,10 +183,10 @@ var Flagsmith = /** @class */ (function () {
182
183
  return [2 /*return*/, cachedItem];
183
184
  }
184
185
  if (this.enableLocalEvaluation) {
185
- return [2 /*return*/, new Promise(function (resolve) {
186
+ return [2 /*return*/, new Promise(function (resolve, reject) {
186
187
  return _this.environmentPromise.then(function () {
187
188
  resolve(_this.getEnvironmentFlagsFromDocument());
188
- });
189
+ }).catch(function (e) { return reject(e); });
189
190
  })];
190
191
  }
191
192
  if (this.environment) {
@@ -227,10 +228,10 @@ var Flagsmith = /** @class */ (function () {
227
228
  }
228
229
  traits = traits || {};
229
230
  if (this.enableLocalEvaluation) {
230
- return [2 /*return*/, new Promise(function (resolve) {
231
+ return [2 /*return*/, new Promise(function (resolve, reject) {
231
232
  return _this.environmentPromise.then(function () {
232
233
  resolve(_this.getIdentityFlagsFromDocument(identifier, traits || {}));
233
- });
234
+ }).catch(function (e) { return reject(e); });
234
235
  })];
235
236
  }
236
237
  return [2 /*return*/, this.getIdentityFlagsFromApi(identifier, traits)];
@@ -253,15 +254,15 @@ var Flagsmith = /** @class */ (function () {
253
254
  var _this = this;
254
255
  traits = traits || {};
255
256
  if (this.enableLocalEvaluation) {
256
- return this.environmentPromise.then(function () {
257
- return new Promise(function (resolve) {
257
+ return new Promise(function (resolve, reject) {
258
+ return _this.environmentPromise.then(function () {
258
259
  var identityModel = _this.buildIdentityModel(identifier, Object.keys(traits || {}).map(function (key) { return ({
259
260
  key: key,
260
261
  value: traits === null || traits === void 0 ? void 0 : traits[key]
261
262
  }); }));
262
263
  var segments = (0, evaluators_1.getIdentitySegments)(_this.environment, identityModel);
263
264
  return resolve(segments);
264
- });
265
+ }).catch(function (e) { return reject(e); });
265
266
  });
266
267
  }
267
268
  console.error('This function is only permitted with local evaluation.');
@@ -311,6 +312,15 @@ var Flagsmith = /** @class */ (function () {
311
312
  });
312
313
  });
313
314
  };
315
+ Flagsmith.prototype.close = function () {
316
+ var _a;
317
+ return __awaiter(this, void 0, void 0, function () {
318
+ return __generator(this, function (_b) {
319
+ (_a = this.environmentDataPollingManager) === null || _a === void 0 ? void 0 : _a.stop();
320
+ return [2 /*return*/];
321
+ });
322
+ });
323
+ };
314
324
  Flagsmith.prototype.getJSONResponse = function (url, method, body) {
315
325
  return __awaiter(this, void 0, void 0, function () {
316
326
  var headers, _a, _b, _c, k, v, data;
@@ -339,10 +349,10 @@ var Flagsmith = /** @class */ (function () {
339
349
  }
340
350
  return [4 /*yield*/, (0, utils_1.retryFetch)(url, {
341
351
  method: method,
342
- timeout: this.requestTimeoutSeconds || undefined,
352
+ timeout: this.requestTimeoutMs || undefined,
343
353
  body: JSON.stringify(body),
344
354
  headers: headers
345
- }, this.retries, (this.requestTimeoutSeconds || 10) * 1000)];
355
+ }, this.retries)];
346
356
  case 1:
347
357
  data = _e.sent();
348
358
  if (data.status !== 200) {
@@ -9,4 +9,4 @@ export declare function generateIdentitiesData(identifier: string, traits: {
9
9
  }[];
10
10
  };
11
11
  export declare const delay: (ms: number) => Promise<unknown>;
12
- export declare const retryFetch: (url: string, fetchOptions: any, retries: number | undefined, timeout: number) => Promise<Response>;
12
+ export declare const retryFetch: (url: string, fetchOptions: any, retries?: number, timeout?: number | undefined) => Promise<Response>;
@@ -59,7 +59,8 @@ var delay = function (ms) {
59
59
  return new Promise(function (resolve) { return setTimeout(function () { return resolve(undefined); }, ms); });
60
60
  };
61
61
  exports.delay = delay;
62
- var retryFetch = function (url, fetchOptions, retries, timeout) {
62
+ var retryFetch = function (url, fetchOptions, retries, timeout // set an overall timeout for this function
63
+ ) {
63
64
  if (retries === void 0) { retries = 3; }
64
65
  return new Promise(function (resolve, reject) {
65
66
  // check for timeout
package/index.ts CHANGED
@@ -1,5 +1,3 @@
1
- import Flagsmith from './sdk';
2
-
3
1
  export {
4
2
  AnalyticsProcessor,
5
3
  FlagsmithAPIError,
@@ -7,7 +5,8 @@ export {
7
5
  EnvironmentDataPollingManager,
8
6
  FlagsmithCache,
9
7
  DefaultFlag,
10
- Flags
8
+ Flags,
9
+ default
11
10
  } from './sdk';
12
11
 
13
12
  export {
@@ -19,5 +18,3 @@ export {
19
18
  SegmentModel,
20
19
  OrganisationModel
21
20
  } from './flagsmith-engine';
22
-
23
- module.exports = Flagsmith;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "2.1.2",
3
+ "version": "2.2.2-beta.1",
4
4
  "description": "Flagsmith lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.",
5
5
  "main": "build/index.js",
6
6
  "repository": {
package/sdk/analytics.ts CHANGED
@@ -10,22 +10,22 @@ export class AnalyticsProcessor {
10
10
  private environmentKey: string;
11
11
  private lastFlushed: number;
12
12
  analyticsData: { [key: string]: any };
13
- private timeout: number = 3;
13
+ private requestTimeoutMs: number = 3000;
14
14
  /**
15
15
  * AnalyticsProcessor is used to track how often individual Flags are evaluated within
16
16
  * the Flagsmith SDK. Docs: https://docs.flagsmith.com/advanced-use/flag-analytics.
17
17
  *
18
18
  * @param data.environmentKey environment key obtained from the Flagsmith UI
19
19
  * @param data.baseApiUrl base api url to override when using self hosted version
20
- * @param data.timeout used to tell requests to stop waiting for a response after a
21
- given number of seconds
20
+ * @param data.requestTimeoutMs used to tell requests to stop waiting for a response after a
21
+ given number of milliseconds
22
22
  */
23
- constructor(data: { environmentKey: string; baseApiUrl: string; timeout?: number }) {
23
+ constructor(data: { environmentKey: string; baseApiUrl: string; requestTimeoutMs?: number }) {
24
24
  this.analyticsEndpoint = data.baseApiUrl + ANALYTICS_ENDPOINT;
25
25
  this.environmentKey = data.environmentKey;
26
26
  this.lastFlushed = Date.now();
27
27
  this.analyticsData = {};
28
- this.timeout = data.timeout || this.timeout;
28
+ this.requestTimeoutMs = data.requestTimeoutMs || this.requestTimeoutMs;
29
29
  }
30
30
  /**
31
31
  * Sends all the collected data to the api asynchronously and resets the timer
@@ -38,7 +38,7 @@ export class AnalyticsProcessor {
38
38
  await fetch(this.analyticsEndpoint, {
39
39
  method: 'POST',
40
40
  body: JSON.stringify(this.analyticsData),
41
- timeout: this.timeout,
41
+ timeout: this.requestTimeoutMs,
42
42
  headers: {
43
43
  'Content-Type': 'application/json',
44
44
  'X-Environment-Key': this.environmentKey
package/sdk/index.ts CHANGED
@@ -28,6 +28,7 @@ export class Flagsmith {
28
28
  apiUrl: string = DEFAULT_API_URL;
29
29
  customHeaders?: { [key: string]: any };
30
30
  requestTimeoutSeconds?: number;
31
+ requestTimeoutMs?: number;
31
32
  enableLocalEvaluation?: boolean = false;
32
33
  environmentRefreshIntervalSeconds: number = 60;
33
34
  retries?: number;
@@ -92,6 +93,7 @@ export class Flagsmith {
92
93
  this.apiUrl = data.apiUrl || this.apiUrl;
93
94
  this.customHeaders = data.customHeaders;
94
95
  this.requestTimeoutSeconds = data.requestTimeoutSeconds;
96
+ this.requestTimeoutMs = data.requestTimeoutSeconds ? data.requestTimeoutSeconds * 1000 : undefined;
95
97
  this.enableLocalEvaluation = data.enableLocalEvaluation;
96
98
  this.environmentRefreshIntervalSeconds =
97
99
  data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds;
@@ -135,7 +137,7 @@ export class Flagsmith {
135
137
  ? new AnalyticsProcessor({
136
138
  environmentKey: this.environmentKey,
137
139
  baseApiUrl: this.apiUrl,
138
- timeout: this.requestTimeoutSeconds
140
+ requestTimeoutMs: this.requestTimeoutMs
139
141
  })
140
142
  : undefined;
141
143
  }
@@ -150,10 +152,10 @@ export class Flagsmith {
150
152
  return cachedItem;
151
153
  }
152
154
  if (this.enableLocalEvaluation) {
153
- return new Promise(resolve =>
155
+ return new Promise((resolve, reject) =>
154
156
  this.environmentPromise!.then(() => {
155
157
  resolve(this.getEnvironmentFlagsFromDocument());
156
- })
158
+ }).catch((e) => reject(e))
157
159
  );
158
160
  }
159
161
  if (this.environment) {
@@ -180,10 +182,10 @@ export class Flagsmith {
180
182
  }
181
183
  traits = traits || {};
182
184
  if (this.enableLocalEvaluation) {
183
- return new Promise(resolve =>
185
+ return new Promise((resolve, reject) =>
184
186
  this.environmentPromise!.then(() => {
185
187
  resolve(this.getIdentityFlagsFromDocument(identifier, traits || {}));
186
- })
188
+ }).catch(e => reject(e))
187
189
  );
188
190
  }
189
191
  return this.getIdentityFlagsFromApi(identifier, traits);
@@ -206,8 +208,8 @@ export class Flagsmith {
206
208
  ): Promise<SegmentModel[]> {
207
209
  traits = traits || {};
208
210
  if (this.enableLocalEvaluation) {
209
- return this.environmentPromise!.then(() => {
210
- return new Promise(resolve => {
211
+ return new Promise((resolve, reject) => {
212
+ return this.environmentPromise!.then(() => {
211
213
  const identityModel = this.buildIdentityModel(
212
214
  identifier,
213
215
  Object.keys(traits || {}).map(key => ({
@@ -218,7 +220,7 @@ export class Flagsmith {
218
220
 
219
221
  const segments = getIdentitySegments(this.environment, identityModel);
220
222
  return resolve(segments);
221
- });
223
+ }).catch((e) => reject(e));
222
224
  });
223
225
  }
224
226
  console.error('This function is only permitted with local evaluation.');
@@ -251,6 +253,10 @@ export class Flagsmith {
251
253
  }
252
254
  }
253
255
 
256
+ async close() {
257
+ this.environmentDataPollingManager?.stop();
258
+ }
259
+
254
260
  private async getJSONResponse(
255
261
  url: string,
256
262
  method: string,
@@ -271,12 +277,11 @@ export class Flagsmith {
271
277
  url,
272
278
  {
273
279
  method: method,
274
- timeout: this.requestTimeoutSeconds || undefined,
280
+ timeout: this.requestTimeoutMs || undefined,
275
281
  body: JSON.stringify(body),
276
282
  headers: headers
277
283
  },
278
- this.retries,
279
- (this.requestTimeoutSeconds || 10) * 1000
284
+ this.retries
280
285
  );
281
286
 
282
287
  if (data.status !== 200) {
package/sdk/utils.ts CHANGED
@@ -20,7 +20,7 @@ export const retryFetch = (
20
20
  url: string,
21
21
  fetchOptions: any,
22
22
  retries = 3,
23
- timeout: number
23
+ timeout?: number // set an overall timeout for this function
24
24
  ): Promise<Response> => {
25
25
  return new Promise((resolve, reject) => {
26
26
  // check for timeout
@@ -32,7 +32,7 @@ test('test_analytics_processor_flush_post_request_data_match_ananlytics_data', a
32
32
  body: '{"myFeature1":1,"myFeature2":1}',
33
33
  headers: { 'Content-Type': 'application/json', 'X-Environment-Key': 'test-key' },
34
34
  method: 'POST',
35
- timeout: 3
35
+ timeout: 3000
36
36
  });
37
37
  });
38
38