@common.js/p-retry 6.1.0 → 6.2.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/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.1.0](https://www.npmjs.com/package/p-retry/v/6.1.0) using https://github.com/etienne-martin/common.js.
5
+ Exported from [p-retry@6.2.1](https://www.npmjs.com/package/p-retry/v/6.2.1) 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
@@ -310,6 +310,7 @@ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
310
310
  };
311
311
  }
312
312
  };
313
+ var _options, _options1, _options2;
313
314
  var AbortError = /*#__PURE__*/ function(Error1) {
314
315
  "use strict";
315
316
  _inherits(AbortError, Error1);
@@ -347,10 +348,15 @@ function _pRetry() {
347
348
  return [
348
349
  2,
349
350
  new Promise(function(resolve, reject) {
350
- options = _objectSpread({
351
- onFailedAttempt: function onFailedAttempt() {},
352
- retries: 10
353
- }, options);
351
+ options = _objectSpread({}, options);
352
+ var _onFailedAttempt;
353
+ (_onFailedAttempt = (_options = options).onFailedAttempt) !== null && _onFailedAttempt !== void 0 ? _onFailedAttempt : _options.onFailedAttempt = function() {};
354
+ var _shouldRetry;
355
+ (_shouldRetry = (_options1 = options).shouldRetry) !== null && _shouldRetry !== void 0 ? _shouldRetry : _options1.shouldRetry = function() {
356
+ return true;
357
+ };
358
+ var _retries;
359
+ (_retries = (_options2 = options).retries) !== null && _retries !== void 0 ? _retries : _options2.retries = 10;
354
360
  var operation = _retry.default.operation(options);
355
361
  var abortHandler = function() {
356
362
  var ref;
@@ -377,7 +383,7 @@ function _pRetry() {
377
383
  0,
378
384
  2,
379
385
  ,
380
- 7
386
+ 8
381
387
  ]);
382
388
  return [
383
389
  4,
@@ -389,7 +395,7 @@ function _pRetry() {
389
395
  resolve(result);
390
396
  return [
391
397
  3,
392
- 7
398
+ 8
393
399
  ];
394
400
  case 2:
395
401
  error = _state.sent();
@@ -397,9 +403,9 @@ function _pRetry() {
397
403
  case 3:
398
404
  _state.trys.push([
399
405
  3,
400
- 5,
406
+ 6,
401
407
  ,
402
- 6
408
+ 7
403
409
  ]);
404
410
  if (!_instanceof(error, Error)) {
405
411
  throw new TypeError('Non-error was thrown: "'.concat(error, '". You should only throw errors.'));
@@ -410,34 +416,44 @@ function _pRetry() {
410
416
  if (_instanceof(error, TypeError) && !(0, _isNetworkError.default)(error)) {
411
417
  throw error;
412
418
  }
419
+ decorateErrorWithCounts(error, attemptNumber, options);
413
420
  return [
414
421
  4,
415
- options.onFailedAttempt(decorateErrorWithCounts(error, attemptNumber, options))
422
+ options.shouldRetry(error)
416
423
  ];
417
424
  case 4:
425
+ if (!_state.sent()) {
426
+ operation.stop();
427
+ reject(error);
428
+ }
429
+ return [
430
+ 4,
431
+ options.onFailedAttempt(error)
432
+ ];
433
+ case 5:
418
434
  _state.sent();
419
435
  if (!operation.retry(error)) {
420
436
  throw operation.mainError();
421
437
  }
422
438
  return [
423
439
  3,
424
- 6
440
+ 7
425
441
  ];
426
- case 5:
442
+ case 6:
427
443
  finalError = _state.sent();
428
444
  decorateErrorWithCounts(finalError, attemptNumber, options);
429
445
  cleanUp();
430
446
  reject(finalError);
431
447
  return [
432
448
  3,
433
- 6
449
+ 7
434
450
  ];
435
- case 6:
451
+ case 7:
436
452
  return [
437
453
  3,
438
- 7
454
+ 8
439
455
  ];
440
- case 7:
456
+ case 8:
441
457
  return [
442
458
  2
443
459
  ];
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@common.js/p-retry",
3
- "version": "6.1.0",
3
+ "version": "6.2.1",
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
  },