@yveskaufmann/retry
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -23,2 +23,6 @@ /** | ||
/** | ||
* The max number of milliseconds between two retries. Default is `Unlimited` | ||
*/ | ||
maxDelay?: number; | ||
/** | ||
* Defines the condition on which a operation should be retried a implementation | ||
@@ -35,2 +39,12 @@ * should return true if a retry is desired. | ||
/** | ||
* Will be invoked after each failed attempt. | ||
* An atempt is to be considered failed, if the `retryWhen` classify a returned error/return-values as retryable. | ||
* @param attempt The number of the attempt | ||
* @param result The returned value from the retryable operation, that is considered be a reason to perform a retry | ||
* @param err The cause of the failed attempt | ||
* @returns | ||
*/ | ||
onFailedAttempt?: (attempt: number, result: T, err: Error) => void; | ||
/** | ||
* Calculates the delay between retries in milliseconds. | ||
@@ -110,3 +124,3 @@ * | ||
*/ | ||
static do<T>({ operation, retryWhen: retryCondition, maxRetries, delay, throwMaxAttemptError, nameOfOperation, }: { | ||
static do<T>({ operation, retryWhen: retryCondition, maxRetries, delay, throwMaxAttemptError, nameOfOperation, maxDelay, onFailedAttempt, }: { | ||
operation: () => Promise<T>; | ||
@@ -113,0 +127,0 @@ } & RetryOptions<T>): Promise<T>; |
@@ -85,3 +85,3 @@ "use strict"; | ||
*/ | ||
static do({ operation, retryWhen: retryCondition, maxRetries, delay, throwMaxAttemptError, nameOfOperation, }) { | ||
static do({ operation, retryWhen: retryCondition, maxRetries, delay, throwMaxAttemptError, nameOfOperation, maxDelay, onFailedAttempt, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -107,6 +107,12 @@ maxRetries = maxRetries !== null && maxRetries !== void 0 ? maxRetries : 2; | ||
} | ||
if (typeof onFailedAttempt === 'function') { | ||
onFailedAttempt(attempts + 1, result, previousError); | ||
} | ||
attemptsLeft = attempts++ < maxRetries; | ||
shouldRetry = shouldRetry && attemptsLeft; | ||
if (shouldRetry) { | ||
const delayDuration = delay(attempts, previousError); | ||
let delayDuration = delay(attempts, previousError); | ||
if (typeof maxDelay === 'number' && maxDelay > 0) { | ||
delayDuration = Math.min(maxDelay, delayDuration); | ||
} | ||
yield (0, wait_1.wait)(delayDuration); | ||
@@ -113,0 +119,0 @@ } |
{ | ||
"name": "@yveskaufmann/retry", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Utility for retrying promise based operation on certain situations.", | ||
@@ -33,15 +33,15 @@ "main": "dist/index.js", | ||
"@types/jest": "^27.4.1", | ||
"@types/node": "^16.11.26", | ||
"@typescript-eslint/eslint-plugin": "^5.17.0", | ||
"@typescript-eslint/parser": "^5.17.0", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-plugin-jest": "^24.4.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"eslint-plugin-unicorn": "^35.0.0", | ||
"@types/node": "^18.13.0", | ||
"@typescript-eslint/eslint-plugin": "~5.52.0", | ||
"@typescript-eslint/parser": "~5.52.0", | ||
"eslint": "~8.34.0", | ||
"eslint-config-prettier": "~8.6.0", | ||
"eslint-plugin-import": "~2.27.5", | ||
"eslint-plugin-jest": "~27.2.1", | ||
"eslint-plugin-prettier": "~4.2.1", | ||
"eslint-plugin-unicorn": "~45.0.2", | ||
"jest": "^27.5.1", | ||
"ts-jest": "^27.1.4", | ||
"typescript": "^4.6.3" | ||
"typescript": "^4.9.5" | ||
} | ||
} |
@@ -68,8 +68,12 @@ # @yveskaufmann/retry - retry-utility | ||
- [RetryOptions](#retryoptions) | ||
- [Retry](#retry) | ||
- [Retry#Delays](#retrydelays) | ||
- [Retry#Conditions](#retryconditions) | ||
- [Retryable](#retryable) | ||
- [MaxRetryAttemptsReached](#maxretryattemptsreached) | ||
- [@yveskaufmann/retry - retry-utility](#yveskaufmannretry---retry-utility) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [API Reference](#api-reference) | ||
- [RetryOptions](#retryoptions) | ||
- [Retry](#retry) | ||
- [RetryDelays](#retrydelays) | ||
- [RetryConditions](#retryconditions) | ||
- [Retryable](#retryable) | ||
- [MaxRetryAttemptsReached](#maxretryattemptsreached) | ||
@@ -106,2 +110,7 @@ ### RetryOptions | ||
/** | ||
* The max number of milliseconds between two retries. Default is `Unlimited` | ||
*/ | ||
maxDelay?: number; | ||
/** | ||
* Defines the condition on which a operation should be | ||
@@ -133,2 +142,13 @@ * retried, a implementation should return true | ||
delay?: (attempts: number, error?: Error) => number; | ||
/** | ||
* Will be invoked after each failed attempt. | ||
* An atempt is to be considered failed, if the `retryWhen` classify a returned error/return-values as retryable. | ||
* @param attempt The number of the attempt | ||
* @param result The returned value from the retryable operation, that is considered be a reason to perform a retry | ||
* @param err The cause of the failed attempt | ||
* @returns | ||
*/ | ||
onFailedAttempt?: (attempt: number, result: T, err: Error) => void; | ||
} | ||
@@ -135,0 +155,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48449
18
382
317