1 | import isRetryAllowed from 'is-retry-allowed';
|
2 |
|
3 | const namespace = 'axios-retry';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export function isNetworkError(error) {
|
10 | return (
|
11 | !error.response &&
|
12 | Boolean(error.code) &&
|
13 | error.code !== 'ECONNABORTED' &&
|
14 | isRetryAllowed(error)
|
15 | );
|
16 | }
|
17 |
|
18 | const SAFE_HTTP_METHODS = ['get', 'head', 'options'];
|
19 | const IDEMPOTENT_HTTP_METHODS = SAFE_HTTP_METHODS.concat(['put', 'delete']);
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | export function isRetryableError(error) {
|
26 | return (
|
27 | error.code !== 'ECONNABORTED' &&
|
28 | (!error.response || (error.response.status >= 500 && error.response.status <= 599))
|
29 | );
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export function isSafeRequestError(error) {
|
37 | if (!error.config) {
|
38 |
|
39 | return false;
|
40 | }
|
41 |
|
42 | return isRetryableError(error) && SAFE_HTTP_METHODS.indexOf(error.config.method) !== -1;
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export function isIdempotentRequestError(error) {
|
50 | if (!error.config) {
|
51 |
|
52 | return false;
|
53 | }
|
54 |
|
55 | return isRetryableError(error) && IDEMPOTENT_HTTP_METHODS.indexOf(error.config.method) !== -1;
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export function isNetworkOrIdempotentRequestError(error) {
|
63 | return isNetworkError(error) || isIdempotentRequestError(error);
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function noDelay() {
|
70 | return 0;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | export function exponentialDelay(retryNumber = 0) {
|
78 | const delay = Math.pow(2, retryNumber) * 100;
|
79 | const randomSum = delay * 0.2 * Math.random();
|
80 | return delay + randomSum;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function getCurrentState(config) {
|
89 | const currentState = config[namespace] || {};
|
90 | currentState.retryCount = currentState.retryCount || 0;
|
91 | config[namespace] = currentState;
|
92 | return currentState;
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function getRequestOptions(config, defaultOptions) {
|
102 | return Object.assign({}, defaultOptions, config[namespace]);
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | function fixConfig(axios, config) {
|
110 | if (axios.defaults.agent === config.agent) {
|
111 | delete config.agent;
|
112 | }
|
113 | if (axios.defaults.httpAgent === config.httpAgent) {
|
114 | delete config.httpAgent;
|
115 | }
|
116 | if (axios.defaults.httpsAgent === config.httpsAgent) {
|
117 | delete config.httpsAgent;
|
118 | }
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | export default function axiosRetry(axios, defaultOptions) {
|
174 | axios.interceptors.request.use(config => {
|
175 | const currentState = getCurrentState(config);
|
176 | currentState.lastRequestTime = Date.now();
|
177 | return config;
|
178 | });
|
179 |
|
180 | axios.interceptors.response.use(null, error => {
|
181 | const config = error.config;
|
182 |
|
183 |
|
184 | if (!config) {
|
185 | return Promise.reject(error);
|
186 | }
|
187 |
|
188 | const {
|
189 | retries = 3,
|
190 | retryCondition = isNetworkOrIdempotentRequestError,
|
191 | retryDelay = noDelay,
|
192 | shouldResetTimeout = false
|
193 | } = getRequestOptions(config, defaultOptions);
|
194 |
|
195 | const currentState = getCurrentState(config);
|
196 |
|
197 | const shouldRetry = retryCondition(error) && currentState.retryCount < retries;
|
198 |
|
199 | if (shouldRetry) {
|
200 | currentState.retryCount += 1;
|
201 | const delay = retryDelay(currentState.retryCount, error);
|
202 |
|
203 |
|
204 |
|
205 | fixConfig(axios, config);
|
206 |
|
207 | if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) {
|
208 | const lastRequestDuration = Date.now() - currentState.lastRequestTime;
|
209 |
|
210 | config.timeout = Math.max(config.timeout - lastRequestDuration - delay, 1);
|
211 | }
|
212 |
|
213 | config.transformRequest = [data => data];
|
214 |
|
215 | return new Promise(resolve => setTimeout(() => resolve(axios(config)), delay));
|
216 | }
|
217 |
|
218 | return Promise.reject(error);
|
219 | });
|
220 | }
|
221 |
|
222 |
|
223 | axiosRetry.isNetworkError = isNetworkError;
|
224 | axiosRetry.isSafeRequestError = isSafeRequestError;
|
225 | axiosRetry.isIdempotentRequestError = isIdempotentRequestError;
|
226 | axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError;
|
227 | axiosRetry.exponentialDelay = exponentialDelay;
|
228 | axiosRetry.isRetryableError = isRetryableError;
|