flagsmith-nodejs 2.5.1 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -364,10 +364,9 @@ var Flagsmith = /** @class */ (function () {
364
364
  return [4 /*yield*/, (0, utils_1.retryFetch)(url, {
365
365
  agent: this.agent,
366
366
  method: method,
367
- timeout: this.requestTimeoutMs || undefined,
368
367
  body: JSON.stringify(body),
369
368
  headers: headers
370
- }, this.retries)];
369
+ }, this.retries, this.requestTimeoutMs || undefined)];
371
370
  case 1:
372
371
  data = _e.sent();
373
372
  if (data.status !== 200) {
@@ -63,11 +63,8 @@ var retryFetch = function (url, fetchOptions, retries, timeout // set an overall
63
63
  ) {
64
64
  if (retries === void 0) { retries = 3; }
65
65
  return new Promise(function (resolve, reject) {
66
- // check for timeout
67
- if (timeout)
68
- setTimeout(function () { return reject('error: timeout'); }, timeout);
69
- var wrapper = function (n) {
70
- (0, node_fetch_1.default)(url, fetchOptions)
66
+ var retryWrapper = function (n) {
67
+ requestWrapper()
71
68
  .then(function (res) { return resolve(res); })
72
69
  .catch(function (err) { return __awaiter(void 0, void 0, void 0, function () {
73
70
  return __generator(this, function (_a) {
@@ -77,7 +74,7 @@ var retryFetch = function (url, fetchOptions, retries, timeout // set an overall
77
74
  return [4 /*yield*/, (0, exports.delay)(1000)];
78
75
  case 1:
79
76
  _a.sent();
80
- wrapper(--n);
77
+ retryWrapper(--n);
81
78
  return [3 /*break*/, 3];
82
79
  case 2:
83
80
  reject(err);
@@ -87,7 +84,16 @@ var retryFetch = function (url, fetchOptions, retries, timeout // set an overall
87
84
  });
88
85
  }); });
89
86
  };
90
- wrapper(retries);
87
+ var requestWrapper = function () {
88
+ return new Promise(function (resolve, reject) {
89
+ if (timeout)
90
+ setTimeout(function () { return reject('error: timeout'); }, timeout);
91
+ return (0, node_fetch_1.default)(url, fetchOptions)
92
+ .then(function (res) { return resolve(res); })
93
+ .catch(function (err) { return reject(err); });
94
+ });
95
+ };
96
+ retryWrapper(retries);
91
97
  });
92
98
  };
93
99
  exports.retryFetch = retryFetch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "2.5.1",
3
+ "version": "2.5.2",
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/index.ts CHANGED
@@ -283,11 +283,11 @@ export class Flagsmith {
283
283
  {
284
284
  agent: this.agent,
285
285
  method: method,
286
- timeout: this.requestTimeoutMs || undefined,
287
286
  body: JSON.stringify(body),
288
287
  headers: headers
289
288
  },
290
- this.retries
289
+ this.retries,
290
+ this.requestTimeoutMs || undefined,
291
291
  );
292
292
 
293
293
  if (data.status !== 200) {
package/sdk/utils.ts CHANGED
@@ -23,22 +23,28 @@ export const retryFetch = (
23
23
  timeout?: number // set an overall timeout for this function
24
24
  ): Promise<Response> => {
25
25
  return new Promise((resolve, reject) => {
26
- // check for timeout
27
- if (timeout) setTimeout(() => reject('error: timeout'), timeout);
28
-
29
- const wrapper = (n: number) => {
30
- fetch(url, fetchOptions)
26
+ const retryWrapper = (n: number) => {
27
+ requestWrapper()
31
28
  .then(res => resolve(res))
32
29
  .catch(async err => {
33
30
  if (n > 0) {
34
31
  await delay(1000);
35
- wrapper(--n);
32
+ retryWrapper(--n);
36
33
  } else {
37
34
  reject(err);
38
35
  }
39
36
  });
40
37
  };
41
38
 
42
- wrapper(retries);
39
+ const requestWrapper = (): Promise<Response> => {
40
+ return new Promise((resolve, reject) => {
41
+ if (timeout) setTimeout(() => reject('error: timeout'), timeout);
42
+ return fetch(url, fetchOptions)
43
+ .then(res => resolve(res))
44
+ .catch(err => reject(err))
45
+ })
46
+ }
47
+
48
+ retryWrapper(retries);
43
49
  });
44
50
  };
@@ -42,7 +42,6 @@ describe('getHashedPercentageForObjIds', () => {
42
42
  let objectIdPairs = Array.from(Array(testSample).keys()).flatMap(d => Array.from(Array(testSample).keys()).map(e => [d, e].flat()))
43
43
 
44
44
  // When
45
- console.log(objectIdPairs);
46
45
  let values = objectIdPairs.map((objIds) => getHashedPercentateForObjIds(objIds));
47
46
 
48
47
  // Then
@@ -172,6 +172,27 @@ test('test_default_flag_used_after_multiple_API_errors', async () => {
172
172
  expect(flag.value).toBe(defaultFlag.value);
173
173
  });
174
174
 
175
+ test('default flag handler used when timeout occurs', async () => {
176
+ // @ts-ignore
177
+ fetch.mockReturnValue(Promise.resolve(sleep(10000)));
178
+
179
+ const defaultFlag = new DefaultFlag('some-default-value', true);
180
+
181
+ const defaultFlagHandler = (featureName: string) => defaultFlag;
182
+
183
+ const flg = new Flagsmith({
184
+ environmentKey: 'key',
185
+ defaultFlagHandler: defaultFlagHandler,
186
+ requestTimeoutSeconds: 0.1,
187
+ });
188
+
189
+ const flags = await flg.getEnvironmentFlags();
190
+ const flag = flags.getFlag('some_feature');
191
+ expect(flag.isDefault).toBe(true);
192
+ expect(flag.enabled).toBe(defaultFlag.enabled);
193
+ expect(flag.value).toBe(defaultFlag.value);
194
+ })
195
+
175
196
  test('test_throws_when_no_identity_flags_returned_due_to_error', async () => {
176
197
  // @ts-ignore
177
198
  fetch.mockReturnValue(Promise.resolve(new Response('bad data')));
@@ -245,7 +266,6 @@ test('getIdentitySegments throws error if identifier is empty string', () => {
245
266
  })
246
267
 
247
268
 
248
-
249
269
  async function wipeFeatureStateUUIDs (enviromentModel: EnvironmentModel) {
250
270
  // TODO: this has been pulled out of tests above as a helper function.
251
271
  // I'm not entirely sure why it's necessary, however, we should look to remove.
@@ -264,3 +284,9 @@ async function wipeFeatureStateUUIDs (enviromentModel: EnvironmentModel) {
264
284
  })
265
285
  })
266
286
  }
287
+
288
+ function sleep(ms: number) {
289
+ return new Promise((resolve) => {
290
+ setTimeout(resolve, ms);
291
+ });
292
+ }