-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 12.20 and move to ESM
- Loading branch information
1 parent
e7f9f61
commit 8c8a86c
Showing
9 changed files
with
198 additions
and
229 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,89 @@ | ||
import {OperationOptions} from 'retry'; | ||
|
||
declare class AbortErrorClass extends Error { | ||
export class AbortError extends Error { | ||
readonly name: 'AbortError'; | ||
readonly originalError: Error; | ||
|
||
/** | ||
Abort retrying and reject the promise. | ||
@param message - Error message or custom error. | ||
@param message - An error message or a custom error. | ||
*/ | ||
constructor(message: string | Error); | ||
} | ||
|
||
declare namespace pRetry { | ||
interface FailedAttemptError extends Error { | ||
readonly attemptNumber: number; | ||
readonly retriesLeft: number; | ||
} | ||
|
||
interface Options extends OperationOptions { | ||
/** | ||
Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively. | ||
export interface FailedAttemptError extends Error { | ||
readonly attemptNumber: number; | ||
readonly retriesLeft: number; | ||
} | ||
|
||
The `onFailedAttempt` function can return a promise. For example, to add a [delay](https://github.com/sindresorhus/delay): | ||
export interface Options extends OperationOptions { | ||
/** | ||
Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively. | ||
``` | ||
import pRetry = require('p-retry'); | ||
import delay = require('delay'); | ||
The `onFailedAttempt` function can return a promise. For example, to add a [delay](https://github.com/sindresorhus/delay): | ||
const run = async () => { ... }; | ||
``` | ||
import pRetry from 'p-retry'; | ||
import delay from 'delay'; | ||
(async () => { | ||
const result = await pRetry(run, { | ||
onFailedAttempt: async error => { | ||
console.log('Waiting for 1 second before retrying'); | ||
await delay(1000); | ||
} | ||
}); | ||
})(); | ||
``` | ||
const run = async () => { ... }; | ||
If the `onFailedAttempt` function throws, all retries will be aborted and the original promise will reject with the thrown error. | ||
*/ | ||
readonly onFailedAttempt?: (error: FailedAttemptError) => void | Promise<void>; | ||
} | ||
const result = await pRetry(run, { | ||
onFailedAttempt: async error => { | ||
console.log('Waiting for 1 second before retrying'); | ||
await delay(1000); | ||
} | ||
}); | ||
``` | ||
type AbortError = AbortErrorClass; | ||
If the `onFailedAttempt` function throws, all retries will be aborted and the original promise will reject with the thrown error. | ||
*/ | ||
readonly onFailedAttempt?: (error: FailedAttemptError) => void | Promise<void>; | ||
} | ||
|
||
declare const pRetry: { | ||
/** | ||
Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the max retries are reached, it then rejects with the last rejection reason. | ||
Does not retry on most `TypeErrors`, with the exception of network errors. This is done on a best case basis as different browsers have different [messages](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful) to indicate this. | ||
See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080) | ||
/** | ||
Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the max retries are reached, it then rejects with the last rejection reason. | ||
@param input - Receives the number of attempts as the first argument and is expected to return a `Promise` or any value. | ||
@param options - Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module. | ||
@example | ||
``` | ||
import pRetry = require('p-retry'); | ||
import fetch from 'node-fetch'; | ||
Does not retry on most `TypeErrors`, with the exception of network errors. This is done on a best case basis as different browsers have different [messages](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful) to indicate this. | ||
See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080) | ||
const run = async () => { | ||
const response = await fetch('https://sindresorhus.com/unicorn'); | ||
@param input - Receives the number of attempts as the first argument and is expected to return a `Promise` or any value. | ||
@param options - Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module. | ||
// Abort retrying if the resource doesn't exist | ||
if (response.status === 404) { | ||
throw new pRetry.AbortError(response.statusText); | ||
} | ||
@example | ||
``` | ||
import pRetry from 'p-retry'; | ||
import fetch from 'node-fetch'; | ||
return response.blob(); | ||
}; | ||
(async () => { | ||
console.log(await pRetry(run, {retries: 5})); | ||
// With the `onFailedAttempt` option: | ||
const result = await pRetry(run, { | ||
onFailedAttempt: error => { | ||
console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`); | ||
// 1st request => Attempt 1 failed. There are 4 retries left. | ||
// 2nd request => Attempt 2 failed. There are 3 retries left. | ||
// … | ||
}, | ||
retries: 5 | ||
}); | ||
console.log(result); | ||
})(); | ||
``` | ||
*/ | ||
<T>( | ||
input: (attemptCount: number) => PromiseLike<T> | T, | ||
options?: pRetry.Options | ||
): Promise<T>; | ||
const run = async () => { | ||
const response = await fetch('https://sindresorhus.com/unicorn'); | ||
AbortError: typeof AbortErrorClass; | ||
// Abort retrying if the resource doesn't exist | ||
if (response.status === 404) { | ||
throw new pRetry.AbortError(response.statusText); | ||
} | ||
// TODO: remove this in the next major version | ||
default: typeof pRetry; | ||
return response.blob(); | ||
}; | ||
export = pRetry; | ||
console.log(await pRetry(run, {retries: 5})); | ||
// With the `onFailedAttempt` option: | ||
const result = await pRetry(run, { | ||
onFailedAttempt: error => { | ||
console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`); | ||
// 1st request => Attempt 1 failed. There are 4 retries left. | ||
// 2nd request => Attempt 2 failed. There are 3 retries left. | ||
// … | ||
}, | ||
retries: 5 | ||
}); | ||
console.log(result); | ||
``` | ||
*/ | ||
export default function pRetry<T>( | ||
input: (attemptCount: number) => PromiseLike<T> | T, | ||
options?: Options | ||
): Promise<T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
import {expectType} from 'tsd'; | ||
import pRetry = require('.'); | ||
import {AbortError, FailedAttemptError} from '.'; | ||
import pRetry, {AbortError, FailedAttemptError} from './index.js'; | ||
|
||
expectType<Promise<number>>( | ||
pRetry(count => { | ||
pRetry(async count => { | ||
expectType<number>(count); | ||
return Promise.resolve(1); | ||
}) | ||
}), | ||
); | ||
expectType<Promise<void>>( | ||
pRetry(() => {}, { | ||
pRetry(() => undefined, { | ||
onFailedAttempt: error => { | ||
expectType<FailedAttemptError>(error); | ||
expectType<number>(error.attemptNumber); | ||
expectType<number>(error.retriesLeft); | ||
} | ||
}) | ||
}, | ||
}), | ||
); | ||
expectType<Promise<string>>( | ||
pRetry(() => 'foo', { | ||
retries: 5 | ||
}) | ||
retries: 5, | ||
}), | ||
); | ||
|
||
const abortError = new AbortError('foo'); | ||
new AbortError(new Error('foo')); | ||
new AbortError(new Error('foo')); // eslint-disable-line no-new | ||
|
||
abortError instanceof AbortError; | ||
expectType<AbortError>(abortError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.