1 | import Dispatcher from "./dispatcher";
|
---|
2 |
|
---|
3 | export default RetryHandler;
|
---|
4 |
|
---|
5 | declare class RetryHandler implements Dispatcher.DispatchHandlers {
|
---|
6 | constructor(
|
---|
7 | options: Dispatcher.DispatchOptions & {
|
---|
8 | retryOptions?: RetryHandler.RetryOptions;
|
---|
9 | },
|
---|
10 | retryHandlers: RetryHandler.RetryHandlers
|
---|
11 | );
|
---|
12 | }
|
---|
13 |
|
---|
14 | declare namespace RetryHandler {
|
---|
15 | export type RetryState = { counter: number; };
|
---|
16 |
|
---|
17 | export type RetryContext = {
|
---|
18 | state: RetryState;
|
---|
19 | opts: Dispatcher.DispatchOptions & {
|
---|
20 | retryOptions?: RetryHandler.RetryOptions;
|
---|
21 | };
|
---|
22 | }
|
---|
23 |
|
---|
24 | export type OnRetryCallback = (result?: Error | null) => void;
|
---|
25 |
|
---|
26 | export type RetryCallback = (
|
---|
27 | err: Error,
|
---|
28 | context: {
|
---|
29 | state: RetryState;
|
---|
30 | opts: Dispatcher.DispatchOptions & {
|
---|
31 | retryOptions?: RetryHandler.RetryOptions;
|
---|
32 | };
|
---|
33 | },
|
---|
34 | callback: OnRetryCallback
|
---|
35 | ) => number | null;
|
---|
36 |
|
---|
37 | export interface RetryOptions {
|
---|
38 | /**
|
---|
39 | * Callback to be invoked on every retry iteration.
|
---|
40 | * It receives the error, current state of the retry object and the options object
|
---|
41 | * passed when instantiating the retry handler.
|
---|
42 | *
|
---|
43 | * @type {RetryCallback}
|
---|
44 | * @memberof RetryOptions
|
---|
45 | */
|
---|
46 | retry?: RetryCallback;
|
---|
47 | /**
|
---|
48 | * Maximum number of retries to allow.
|
---|
49 | *
|
---|
50 | * @type {number}
|
---|
51 | * @memberof RetryOptions
|
---|
52 | * @default 5
|
---|
53 | */
|
---|
54 | maxRetries?: number;
|
---|
55 | /**
|
---|
56 | * Max number of milliseconds allow between retries
|
---|
57 | *
|
---|
58 | * @type {number}
|
---|
59 | * @memberof RetryOptions
|
---|
60 | * @default 30000
|
---|
61 | */
|
---|
62 | maxTimeout?: number;
|
---|
63 | /**
|
---|
64 | * Initial number of milliseconds to wait before retrying for the first time.
|
---|
65 | *
|
---|
66 | * @type {number}
|
---|
67 | * @memberof RetryOptions
|
---|
68 | * @default 500
|
---|
69 | */
|
---|
70 | minTimeout?: number;
|
---|
71 | /**
|
---|
72 | * Factior to multiply the timeout factor between retries.
|
---|
73 | *
|
---|
74 | * @type {number}
|
---|
75 | * @memberof RetryOptions
|
---|
76 | * @default 2
|
---|
77 | */
|
---|
78 | timeoutFactor?: number;
|
---|
79 | /**
|
---|
80 | * It enables to automatically infer timeout between retries based on the `Retry-After` header.
|
---|
81 | *
|
---|
82 | * @type {boolean}
|
---|
83 | * @memberof RetryOptions
|
---|
84 | * @default true
|
---|
85 | */
|
---|
86 | retryAfter?: boolean;
|
---|
87 | /**
|
---|
88 | * HTTP methods to retry.
|
---|
89 | *
|
---|
90 | * @type {Dispatcher.HttpMethod[]}
|
---|
91 | * @memberof RetryOptions
|
---|
92 | * @default ['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE'],
|
---|
93 | */
|
---|
94 | methods?: Dispatcher.HttpMethod[];
|
---|
95 | /**
|
---|
96 | * Error codes to be retried. e.g. `ECONNRESET`, `ENOTFOUND`, `ETIMEDOUT`, `ECONNREFUSED`, etc.
|
---|
97 | *
|
---|
98 | * @type {string[]}
|
---|
99 | * @default ['ECONNRESET','ECONNREFUSED','ENOTFOUND','ENETDOWN','ENETUNREACH','EHOSTDOWN','EHOSTUNREACH','EPIPE']
|
---|
100 | */
|
---|
101 | errorCodes?: string[];
|
---|
102 | /**
|
---|
103 | * HTTP status codes to be retried.
|
---|
104 | *
|
---|
105 | * @type {number[]}
|
---|
106 | * @memberof RetryOptions
|
---|
107 | * @default [500, 502, 503, 504, 429],
|
---|
108 | */
|
---|
109 | statusCodes?: number[];
|
---|
110 | }
|
---|
111 |
|
---|
112 | export interface RetryHandlers {
|
---|
113 | dispatch: Dispatcher["dispatch"];
|
---|
114 | handler: Dispatcher.DispatchHandlers;
|
---|
115 | }
|
---|
116 | }
|
---|