index.d.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {OperationOptions} from 'retry';
  2. declare class AbortErrorClass extends Error {
  3. readonly name: 'AbortError';
  4. readonly originalError: Error;
  5. /**
  6. Abort retrying and reject the promise.
  7. @param message - Error message or custom error.
  8. */
  9. constructor(message: string | Error);
  10. }
  11. declare namespace pRetry {
  12. interface FailedAttemptError extends Error {
  13. readonly attemptNumber: number;
  14. readonly retriesLeft: number;
  15. }
  16. interface Options extends OperationOptions {
  17. /**
  18. 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.
  19. The `onFailedAttempt` function can return a promise. For example, to add a [delay](https://github.com/sindresorhus/delay):
  20. ```
  21. import pRetry = require('p-retry');
  22. import delay = require('delay');
  23. const run = async () => { ... };
  24. (async () => {
  25. const result = await pRetry(run, {
  26. onFailedAttempt: async error => {
  27. console.log('Waiting for 1 second before retrying');
  28. await delay(1000);
  29. }
  30. });
  31. })();
  32. ```
  33. If the `onFailedAttempt` function throws, all retries will be aborted and the original promise will reject with the thrown error.
  34. */
  35. readonly onFailedAttempt?: (error: FailedAttemptError) => void | Promise<void>;
  36. }
  37. type AbortError = AbortErrorClass;
  38. }
  39. declare const pRetry: {
  40. /**
  41. 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.
  42. 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.
  43. See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080)
  44. @param input - Receives the number of attempts as the first argument and is expected to return a `Promise` or any value.
  45. @param options - Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module.
  46. @example
  47. ```
  48. import pRetry = require('p-retry');
  49. import fetch from 'node-fetch';
  50. const run = async () => {
  51. const response = await fetch('https://sindresorhus.com/unicorn');
  52. // Abort retrying if the resource doesn't exist
  53. if (response.status === 404) {
  54. throw new pRetry.AbortError(response.statusText);
  55. }
  56. return response.blob();
  57. };
  58. (async () => {
  59. console.log(await pRetry(run, {retries: 5}));
  60. // With the `onFailedAttempt` option:
  61. const result = await pRetry(run, {
  62. onFailedAttempt: error => {
  63. console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`);
  64. // 1st request => Attempt 1 failed. There are 4 retries left.
  65. // 2nd request => Attempt 2 failed. There are 3 retries left.
  66. // …
  67. },
  68. retries: 5
  69. });
  70. console.log(result);
  71. })();
  72. ```
  73. */
  74. <T>(
  75. input: (attemptCount: number) => PromiseLike<T> | T,
  76. options?: pRetry.Options
  77. ): Promise<T>;
  78. AbortError: typeof AbortErrorClass;
  79. // TODO: remove this in the next major version
  80. default: typeof pRetry;
  81. };
  82. export = pRetry;