@mimik/request-retry 4.0.5 → 4.0.7

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/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  **Example**
5
5
  ```js
6
6
  import { rpRetry } from '@mimik/request-retry';
7
- or
7
+ // or
8
8
  import rp from '@mimik/request-retry';
9
9
  ```
10
10
  <a name="module_request-retry..rpRetry"></a>
@@ -16,8 +16,8 @@ Make a request with retries.
16
16
  **Category**: async
17
17
  **Throws**:
18
18
 
19
- - <code>Error</code> An error produced by `getRichError`, wrapping an error from
20
- [request-promise](https://www.npmjs.com/package/request-promise) or a `TimeoutError`.
19
+ - <code>Error</code> An error produced by `getRichError`, wrapping an
20
+ [Axios](https://www.npmjs.com/package/axios) error or a `TimeoutError`.
21
21
 
22
22
  The following properties may be added to the `options` object under the `retry` property:
23
23
  - retries `{int}`: Maximum number of retries, independent of the `retryStrategy`. Default: `2`.
@@ -27,9 +27,9 @@ The following properties may be added to the `options` object under the `retry`
27
27
  - delayStrategy `{function}`: A function that returns the delay (in milliseconds) to wait before the next retry.
28
28
  Signature: `(nbRetry, err, options, correlationId)`. Must return a number `> 0` and `< 30000`; otherwise `delay` is used.
29
29
  - logLevel `{object}`: Controls logging behavior:
30
- - *response*: Log level for responses. Default: `silly` (invalid values fall back to `silly`).
31
- - *error*: Log level for errors. Default: `silly` (invalid values fall back to `silly`).
32
- - *request*: Log level for requests. Default: `silly` (invalid values fall back to `silly`).
30
+ - *response*: Log level for responses. Invalid values fall back to `silly`.
31
+ - *error*: Log level for errors. Invalid values fall back to `silly`.
32
+ - *request*: Log level for requests. Invalid values fall back to `silly`.
33
33
  - *responseDetails*: Detail level when displaying the response: `count`, `type`, or `full`. Default: `type`
34
34
  (invalid values fall back to `type`).
35
35
  - *responseName*: Label associated with the response. Default: `response`
@@ -55,8 +55,8 @@ defaultRetry = (...args) => {
55
55
  ```
56
56
 
57
57
  If `logLevel` is empty, requests and responses are logged at `info`, the response is logged with full details
58
- under the `response` property, and request errors generate a `warn`. If `logLevel` is provided but incomplete,
59
- the provided keys override the defaults.
58
+ under the `response` property, and request errors generate a `warn`. If logLevel is provided but incomplete,
59
+ only the provided keys are active; missing keys disable that logging.
60
60
 
61
61
  If not already set, the `User-Agent` header is set to:
62
62
  `mimik-{serverType}/{serverVersion}/{serverId} {architecture} {node}`.
@@ -71,5 +71,5 @@ To ease migration from `request-promise`, the following adjustments are applied
71
71
 
72
72
  | Param | Type | Description |
73
73
  | --- | --- | --- |
74
- | options | <code>object</code> | Options for the request (similar to [axios](https://www.npmjs.com/package/axios)). The `validateStatus` option is disabled: an error is thrown for status codes outside **[200, 300)**. An additional option, `resolveWithFullResponse`, controls the return shape: when `true`, the full Axios response object is returned; when `false` or omitted, only `response.data` is returned. |
74
+ | options | <code>object</code> | Options for the request (similar to [axios](https://www.npmjs.com/package/axios)). The `validateStatus` option is disabled: an error is thrown for status codes outside **[200, 300)**. An additional option, `resolveWithFullResponse`, controls the return shape: when `true`, the full Axios response object is returned; when `false` or omitted, only `response.data` is returned. |
75
75
 
package/index.js CHANGED
@@ -25,10 +25,8 @@ const DEFAULT_LOGLEVEL_RESPONSE = 'info';
25
25
  const DEFAULT_LOGLEVEL_ERROR = 'warn';
26
26
  const DEFAULT_LOGLEVEL_REQUEST = 'info';
27
27
 
28
- const MILLI_SECONDS = 0;
29
- const MILLI_NANO_SECONDS = 1;
30
28
  const MILLI = 1000;
31
- const NANO = 1e6;
29
+ const NANO_TO_MS = 1000000;
32
30
 
33
31
  Promise.config({ cancellation: true });
34
32
 
@@ -36,7 +34,7 @@ Promise.config({ cancellation: true });
36
34
  * @module request-retry
37
35
  * @example
38
36
  * import { rpRetry } from '@mimik/request-retry';
39
- * or
37
+ * // or
40
38
  * import rp from '@mimik/request-retry';
41
39
  */
42
40
 
@@ -48,13 +46,13 @@ Promise.config({ cancellation: true });
48
46
  * @requires @mimik/response-helper
49
47
  * @category async
50
48
  * @param {object} options - Options for the request (similar to [axios](https://www.npmjs.com/package/axios)).
51
- * The `validateStatus` option is disabled: an error is thrown for status codes outside **[200, 300)**.
52
- * An additional option, `resolveWithFullResponse`, controls the return shape:
53
- * when `true`, the full Axios response object is returned; when `false` or omitted, only `response.data` is returned.
49
+ * The `validateStatus` option is disabled: an error is thrown for status codes outside **[200, 300)**.
50
+ * An additional option, `resolveWithFullResponse`, controls the return shape:
51
+ * when `true`, the full Axios response object is returned; when `false` or omitted, only `response.data` is returned.
54
52
  * @return {Promise}
55
53
  * @fulfil {object} - The Axios response when `resolveWithFullResponse` is `true`; otherwise `response.data`.
56
- * @throws {Error} An error produced by `getRichError`, wrapping an error from
57
- * [request-promise](https://www.npmjs.com/package/request-promise) or a `TimeoutError`.
54
+ * @throws {Error} An error produced by `getRichError`, wrapping an
55
+ * [Axios](https://www.npmjs.com/package/axios) error or a `TimeoutError`.
58
56
  *
59
57
  * The following properties may be added to the `options` object under the `retry` property:
60
58
  * - retries `{int}`: Maximum number of retries, independent of the `retryStrategy`. Default: `2`.
@@ -64,9 +62,9 @@ Promise.config({ cancellation: true });
64
62
  * - delayStrategy `{function}`: A function that returns the delay (in milliseconds) to wait before the next retry.
65
63
  * Signature: `(nbRetry, err, options, correlationId)`. Must return a number `> 0` and `< 30000`; otherwise `delay` is used.
66
64
  * - logLevel `{object}`: Controls logging behavior:
67
- * - *response*: Log level for responses. Default: `silly` (invalid values fall back to `silly`).
68
- * - *error*: Log level for errors. Default: `silly` (invalid values fall back to `silly`).
69
- * - *request*: Log level for requests. Default: `silly` (invalid values fall back to `silly`).
65
+ * - *response*: Log level for responses. Invalid values fall back to `silly`.
66
+ * - *error*: Log level for errors. Invalid values fall back to `silly`.
67
+ * - *request*: Log level for requests. Invalid values fall back to `silly`.
70
68
  * - *responseDetails*: Detail level when displaying the response: `count`, `type`, or `full`. Default: `type`
71
69
  * (invalid values fall back to `type`).
72
70
  * - *responseName*: Label associated with the response. Default: `response`
@@ -92,8 +90,8 @@ Promise.config({ cancellation: true });
92
90
  * ```
93
91
  *
94
92
  * If `logLevel` is empty, requests and responses are logged at `info`, the response is logged with full details
95
- * under the `response` property, and request errors generate a `warn`. If `logLevel` is provided but incomplete,
96
- * the provided keys override the defaults.
93
+ * under the `response` property, and request errors generate a `warn`. If logLevel is provided but incomplete,
94
+ * only the provided keys are active; missing keys disable that logging.
97
95
  *
98
96
  * If not already set, the `User-Agent` header is set to:
99
97
  * `mimik-{serverType}/{serverVersion}/{serverId} {architecture} {node}`.
@@ -104,7 +102,7 @@ Promise.config({ cancellation: true });
104
102
  * - `qs` is mapped to Axios `params`.
105
103
  */
106
104
  export const rpRetry = (origOptions) => {
107
- const startHrTime = process.hrtime();
105
+ const startHrTime = process.hrtime.bigint();
108
106
  const options = origOptions;
109
107
  const { metrics } = options;
110
108
  const enteredUrl = options.uri || options.url;
@@ -118,8 +116,7 @@ export const rpRetry = (origOptions) => {
118
116
  let mainTimeout;
119
117
  const measure = (statusCode) => {
120
118
  if (metrics && metrics.HTTPRequestDuration) {
121
- const elapsedHrTime = process.hrtime(startHrTime);
122
- const elapsedTimeInMs = elapsedHrTime[MILLI_SECONDS] * MILLI + elapsedHrTime[MILLI_NANO_SECONDS] / NANO;
119
+ const elapsedTimeInMs = Number(process.hrtime.bigint() - startHrTime) / NANO_TO_MS;
123
120
 
124
121
  metrics.HTTPRequestDuration
125
122
  .labels('rpRetry', options.method, metrics.url || enteredUrl, enteredUrl.includes('?'), statusCode)
@@ -13,7 +13,7 @@ const rp = (origOptions) => {
13
13
  delete options.body;
14
14
  }
15
15
  else if (typeof options.json === 'object') {
16
- options.data = options.body;
16
+ options.data = options.json;
17
17
  delete options.json;
18
18
  }
19
19
  else delete options.json;
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-console */
2
- import './testEnv';
2
+ import './testEnv.js';
3
3
  import { rpRetry } from '../index.js';
4
4
 
5
5
  const DELAY = 1000;
@@ -30,11 +30,11 @@ let message;
30
30
 
31
31
  app.use(bodyParser.json());
32
32
  app.get(`${config.base}${config.path}`, (req, res) => {
33
- if (nbRequest === 0) message = 'Recieved first request';
33
+ if (nbRequest === 0) message = 'Received first request';
34
34
  else {
35
35
  const timeLap = Date.now() - time;
36
36
 
37
- message = `Received ${nbRequest} retry with timelap ${timeLap}`;
37
+ message = `Received ${nbRequest} retry with timelapse ${timeLap}`;
38
38
  }
39
39
  console.log('----->', message);
40
40
  time = Date.now();
@@ -57,7 +57,7 @@ app.post(`${config.base}${config.path}`, (req, res) => {
57
57
  });
58
58
 
59
59
  app.post('/logs/:id', (req, res) => {
60
- console.log('--->', 'Recieved a log');
60
+ console.log('--->', 'Received a log');
61
61
  console.log('---> id:', req.params.id);
62
62
  console.log('---> body:', req.body);
63
63
  res.statusCode = config.post.success;
@@ -1,5 +1,5 @@
1
1
  /* eslint no-process-env: "off" */
2
- import process from 'process';
2
+ import process from 'node:process';
3
3
 
4
4
  /**
5
5
  * The following environment variables are set for the test:
@@ -10,7 +10,7 @@ import process from 'process';
10
10
  * | SUMO_LOGIC_COLLECTOR_CODE | code to use to log on sumologic | null
11
11
  * | NO_STACK | flag to have a stack associated with the log | yes
12
12
  * | LOG_LEVEL | log level to log | error
13
- * | CONSOLE_LEVEL | log level to diplay | debug
13
+ * | CONSOLE_LEVEL | log level to display | debug
14
14
  * | LOG_MODE | log mode | none
15
15
  */
16
16
  /*
@@ -1,8 +1,5 @@
1
1
  /* eslint-disable no-console */
2
- import {
3
- clearTimeout,
4
- setTimeout,
5
- } from 'timers';
2
+ import { clearTimeout, setTimeout } from 'node:timers';
6
3
 
7
4
  const MAX_INCR = 100000000;
8
5
  const MAIN_TIMEOUT = 1; // in millisecond
@@ -26,12 +26,12 @@ const options = {
26
26
  * delayStrategy: 'joe', // => setDelay returning retryDelay
27
27
  * delayStrategy: function badDelay() { return 'joe' }, // => no change then bad delay strategy => retryDelay
28
28
  * delayStrategy: function userDelay(...args) { return (Math.pow(2, args[0]) * 100) + Math.floor(Math.random() * 50) }, // OK
29
- * retryStrategy: 'joe', // => unknowRetry returning false
29
+ * retryStrategy: 'joe', // => unknownRetry returning false
30
30
  * retryStrategy: function badRetry() { return 'joe' }, // => no change then bad retry strategy => false
31
31
  * retryStrategy: function userRetry(...args) { // OK
32
32
  * const { statusCode } = args[1]; // err
33
33
  *
34
- * return statusCode && (Math.floor(statusCode/100) !== 5 || statusCode !== 429); // retry if there is no statusCode or if there is 500 range statucCode or 429
34
+ * return statusCode && (Math.floor(statusCode/100) !== 5 || statusCode !== 429); // retry if there is no statusCode or if there is 500 range statusCode or 429
35
35
  * },
36
36
  * retryStrategy: function invalidRetry(...args) { // defaultRetry
37
37
  * const { statusCode } = args[34]; // null value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mimik/request-retry",
3
- "version": "4.0.5",
3
+ "version": "4.0.7",
4
4
  "description": "Request retry wrapping axios",
5
5
  "main": "./index.js",
6
6
  "type": "module",
@@ -29,27 +29,27 @@
29
29
  "url": "https://bitbucket.org/mimiktech/request-retry"
30
30
  },
31
31
  "dependencies": {
32
- "@mimik/request-helper": "^2.0.2",
33
- "@mimik/response-helper": "^4.0.6",
34
- "@mimik/sumologic-winston-logger": "^2.1.6",
35
- "axios": "1.12.2",
32
+ "@mimik/request-helper": "^2.0.3",
33
+ "@mimik/response-helper": "^4.0.9",
34
+ "@mimik/sumologic-winston-logger": "^2.1.10",
35
+ "axios": "1.13.5",
36
36
  "bluebird": "3.7.2"
37
37
  },
38
38
  "devDependencies": {
39
- "@eslint/js": "9.35.0",
39
+ "@eslint/js": "9.39.2",
40
40
  "@mimik/eslint-plugin-document-env": "^2.0.8",
41
- "@stylistic/eslint-plugin": "5.3.1",
41
+ "@stylistic/eslint-plugin": "5.8.0",
42
42
  "acorn": "8.15.0",
43
- "body-parser": "2.2.0",
43
+ "body-parser": "2.2.2",
44
44
  "c8": "10.1.3",
45
- "chai": "6.0.1",
46
- "eslint": "9.35.0",
45
+ "chai": "6.2.2",
46
+ "eslint": "9.39.2",
47
47
  "eslint-plugin-import": "2.32.0",
48
- "express": "5.1.0",
48
+ "express": "5.2.1",
49
49
  "husky": "9.1.7",
50
- "jsdoc-to-markdown": "9.1.2",
51
- "mocha": "11.7.2",
52
- "mochawesome": "7.1.3",
53
- "sinon": "21.0.0"
50
+ "jsdoc-to-markdown": "9.1.3",
51
+ "mocha": "11.7.5",
52
+ "mochawesome": "7.1.4",
53
+ "sinon": "21.0.1"
54
54
  }
55
55
  }
package/test/retryMock.js CHANGED
@@ -30,11 +30,11 @@ let message;
30
30
 
31
31
  app.use(bodyParser.json());
32
32
  app.get(`${config.base}${config.path}`, (req, res) => {
33
- if (nbRequest === 0) message = 'Recieved first request';
33
+ if (nbRequest === 0) message = 'Received first request';
34
34
  else {
35
35
  const timeLap = Date.now() - time;
36
36
 
37
- message = `Received ${nbRequest} retry with timelap ${timeLap}`;
37
+ message = `Received ${nbRequest} retry with timelapse ${timeLap}`;
38
38
  }
39
39
  console.log('----->', message);
40
40
  time = Date.now();
@@ -139,7 +139,7 @@ describe('request-retry Unit Tests', () => {
139
139
  expect(callArgs[CALL_ARG_MESSAGE]).to.equal('out of scope timeout, reset to min');
140
140
  });
141
141
  });
142
- it('should generate warning: out of scope timeout: 150 becoming max', () => {
142
+ it('should generate warning: out of scope timeout: 150 becoming max', () => {
143
143
  options.retry.timeout = OUT_OF_SCOPE_RETRY_TIMEOUT;
144
144
  return rpRetry(options).then(() => {
145
145
  assert.calledOnce(loggerSpyWarn);
@@ -163,7 +163,7 @@ describe('request-retry Unit Tests', () => {
163
163
  assert.notCalled(loggerSpyWarn);
164
164
  });
165
165
  });
166
- it('should generate warning: invalid retryStrategy: "notANumber" becoming default function', () => {
166
+ it('should generate warning: invalid retryStrategy: "notAFunction" becoming default function', () => {
167
167
  options.retry.retryStrategy = 'notAFunction';
168
168
  return rpRetry(options).then(() => {
169
169
  assert.calledOnce(loggerSpyWarn);
@@ -240,7 +240,7 @@ describe('request-retry Unit Tests', () => {
240
240
  expect(callArgs[CALL_ARG_MESSAGE]).to.equal('invalid logLevel.responseName, reset to default');
241
241
  });
242
242
  });
243
- it('should generate warning: invalid logLevel.responseName (" ""), reset to default', () => {
243
+ it('should generate warning: invalid logLevel.responseName (" "), reset to default', () => {
244
244
  options.retry.logLevel = {
245
245
  responseName: ' ',
246
246
  };
package/test/testEnv.js CHANGED
@@ -10,7 +10,7 @@ import process from 'node:process';
10
10
  * | SUMO_LOGIC_COLLECTOR_CODE | code to use to log on sumologic | null
11
11
  * | NO_STACK | flag to have a stack associated with the log | yes
12
12
  * | LOG_LEVEL | log level to log | error
13
- * | CONSOLE_LEVEL | log level to diplay | debug
13
+ * | CONSOLE_LEVEL | log level to display | debug
14
14
  * | LOG_MODE | log mode | none
15
15
  */
16
16