index.d.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import * as axios from 'axios'
  2. interface IAxiosRetry {
  3. (
  4. axios: axios.AxiosStatic | axios.AxiosInstance,
  5. axiosRetryConfig?: IAxiosRetry.IAxiosRetryConfig
  6. ): void;
  7. isNetworkError(error: Error): boolean;
  8. isRetryableError(error: Error): boolean;
  9. isSafeRequestError(error: Error): boolean;
  10. isIdempotentRequestError(error: Error): boolean;
  11. isNetworkOrIdempotentRequestError(error: Error): boolean;
  12. exponentialDelay(retryNumber: number): number;
  13. }
  14. export function isNetworkError(error: Error): boolean;
  15. export function isRetryableError(error: Error): boolean;
  16. export function isSafeRequestError(error: Error): boolean;
  17. export function isIdempotentRequestError(error: Error): boolean;
  18. export function isNetworkOrIdempotentRequestError(error: Error): boolean;
  19. export function exponentialDelay(retryNumber: number): number;
  20. declare namespace IAxiosRetry {
  21. export interface IAxiosRetryConfig {
  22. /**
  23. * The number of times to retry before failing
  24. * default: 3
  25. *
  26. * @type {number}
  27. */
  28. retries?: number,
  29. /**
  30. * Defines if the timeout should be reset between retries
  31. * default: false
  32. *
  33. * @type {boolean}
  34. */
  35. shouldResetTimeout?: boolean,
  36. /**
  37. * A callback to further control if a request should be retried. By default, it retries if the result did not have a response.
  38. * default: error => !error.response
  39. *
  40. * @type {Function}
  41. */
  42. retryCondition?: (error: axios.AxiosError) => boolean | Promise<boolean>,
  43. /**
  44. * A callback to further control the delay between retry requests. By default there is no delay.
  45. *
  46. * @type {Function}
  47. */
  48. retryDelay?: (retryCount: number, error: axios.AxiosError) => number
  49. /**
  50. * A callback to get notified when a retry occurs, the number of times it has occurre, and the error
  51. *
  52. * @type {Function}
  53. */
  54. onRetry?: (retryCount: number, error: axios.AxiosError, requestConfig: axios.AxiosRequestConfig) => void
  55. }
  56. }
  57. declare const axiosRetry: IAxiosRetry;
  58. export type IAxiosRetryConfig = IAxiosRetry.IAxiosRetryConfig;
  59. export default axiosRetry;
  60. declare module 'axios' {
  61. export interface AxiosRequestConfig {
  62. 'axios-retry'?: IAxiosRetryConfig;
  63. }
  64. }