@common.js/p-retry 6.0.0 → 6.2.0

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
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [p-retry](https://www.npmjs.com/package/p-retry) package exported as CommonJS modules.
4
4
 
5
- Exported from [p-retry@6.0.0](https://www.npmjs.com/package/p-retry/v/6.0.0) using https://github.com/etienne-martin/common.js.
5
+ Exported from [p-retry@6.2.0](https://www.npmjs.com/package/p-retry/v/6.2.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -41,6 +41,28 @@ export type Options = {
41
41
  */
42
42
  readonly onFailedAttempt?: (error: FailedAttemptError) => void | Promise<void>;
43
43
 
44
+ /**
45
+ Decide if a retry should occur based on the error. Returning true triggers a retry, false aborts with the error.
46
+
47
+ It is not called for `TypeError` (except network errors) and `AbortError`.
48
+
49
+ @param error - The error thrown by the input function.
50
+
51
+ @example
52
+ ```
53
+ import pRetry from 'p-retry';
54
+
55
+ const run = async () => { … };
56
+
57
+ const result = await pRetry(run, {
58
+ shouldRetry: error => !(error instanceof CustomError);
59
+ });
60
+ ```
61
+
62
+ In the example above, the operation will be retried unless the error is an instance of `CustomError`.
63
+ */
64
+ readonly shouldRetry?: (error: FailedAttemptError) => boolean | Promise<boolean>;
65
+
44
66
  /**
45
67
  You can abort retrying using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
46
68
 
package/index.js CHANGED
@@ -17,6 +17,7 @@ _export(exports, {
17
17
  }
18
18
  });
19
19
  var _retry = /*#__PURE__*/ _interopRequireDefault(require("retry"));
20
+ var _isNetworkError = /*#__PURE__*/ _interopRequireDefault(require("is-network-error"));
20
21
  function _assertThisInitialized(self) {
21
22
  if (self === void 0) {
22
23
  throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
@@ -309,13 +310,6 @@ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
309
310
  };
310
311
  }
311
312
  };
312
- var networkErrorMsgs = new Set([
313
- "Failed to fetch",
314
- "NetworkError when attempting to fetch resource.",
315
- "The Internet connection appears to be offline.",
316
- "Network request failed",
317
- "fetch failed"
318
- ]);
319
313
  var AbortError = /*#__PURE__*/ function(Error1) {
320
314
  "use strict";
321
315
  _inherits(AbortError, Error1);
@@ -344,9 +338,6 @@ var decorateErrorWithCounts = function(error, attemptNumber, options) {
344
338
  error.retriesLeft = retriesLeft;
345
339
  return error;
346
340
  };
347
- var isNetworkError = function(errorMessage) {
348
- return networkErrorMsgs.has(errorMessage);
349
- };
350
341
  function pRetry(input, options) {
351
342
  return _pRetry.apply(this, arguments);
352
343
  }
@@ -358,7 +349,10 @@ function _pRetry() {
358
349
  new Promise(function(resolve, reject) {
359
350
  options = _objectSpread({
360
351
  onFailedAttempt: function onFailedAttempt() {},
361
- retries: 10
352
+ retries: 10,
353
+ shouldRetry: function() {
354
+ return true;
355
+ }
362
356
  }, options);
363
357
  var operation = _retry.default.operation(options);
364
358
  var abortHandler = function() {
@@ -386,7 +380,7 @@ function _pRetry() {
386
380
  0,
387
381
  2,
388
382
  ,
389
- 7
383
+ 8
390
384
  ]);
391
385
  return [
392
386
  4,
@@ -398,7 +392,7 @@ function _pRetry() {
398
392
  resolve(result);
399
393
  return [
400
394
  3,
401
- 7
395
+ 8
402
396
  ];
403
397
  case 2:
404
398
  error = _state.sent();
@@ -406,9 +400,9 @@ function _pRetry() {
406
400
  case 3:
407
401
  _state.trys.push([
408
402
  3,
409
- 5,
403
+ 6,
410
404
  ,
411
- 6
405
+ 7
412
406
  ]);
413
407
  if (!_instanceof(error, Error)) {
414
408
  throw new TypeError('Non-error was thrown: "'.concat(error, '". You should only throw errors.'));
@@ -416,37 +410,47 @@ function _pRetry() {
416
410
  if (_instanceof(error, AbortError)) {
417
411
  throw error.originalError;
418
412
  }
419
- if (_instanceof(error, TypeError) && !isNetworkError(error.message)) {
413
+ if (_instanceof(error, TypeError) && !(0, _isNetworkError.default)(error)) {
420
414
  throw error;
421
415
  }
416
+ decorateErrorWithCounts(error, attemptNumber, options);
422
417
  return [
423
418
  4,
424
- options.onFailedAttempt(decorateErrorWithCounts(error, attemptNumber, options))
419
+ options.shouldRetry(error)
425
420
  ];
426
421
  case 4:
422
+ if (!_state.sent()) {
423
+ operation.stop();
424
+ reject(error);
425
+ }
426
+ return [
427
+ 4,
428
+ options.onFailedAttempt(error)
429
+ ];
430
+ case 5:
427
431
  _state.sent();
428
432
  if (!operation.retry(error)) {
429
433
  throw operation.mainError();
430
434
  }
431
435
  return [
432
436
  3,
433
- 6
437
+ 7
434
438
  ];
435
- case 5:
439
+ case 6:
436
440
  finalError = _state.sent();
437
441
  decorateErrorWithCounts(finalError, attemptNumber, options);
438
442
  cleanUp();
439
443
  reject(finalError);
440
444
  return [
441
445
  3,
442
- 6
446
+ 7
443
447
  ];
444
- case 6:
448
+ case 7:
445
449
  return [
446
450
  3,
447
- 7
451
+ 8
448
452
  ];
449
- case 7:
453
+ case 8:
450
454
  return [
451
455
  2
452
456
  ];
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@common.js/p-retry",
3
- "version": "6.0.0",
3
+ "version": "6.2.0",
4
4
  "description": "p-retry package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
8
8
  "type": "commonjs",
9
+ "types": "./index.d.ts",
10
+ "sideEffects": false,
9
11
  "engines": {
10
12
  "node": ">=16.17"
11
13
  },
@@ -18,6 +20,7 @@
18
20
  ],
19
21
  "dependencies": {
20
22
  "@types/retry": "0.12.2",
23
+ "@common.js/is-network-error": "^1.0.0",
21
24
  "retry": "^0.13.1"
22
25
  },
23
26
  "devDependencies": {