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 { ...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 | async function shouldRetry(retries, retryCondition, currentState, error) {
|
130 | const shouldRetryOrPromise = currentState.retryCount < retries && retryCondition(error);
|
131 |
|
132 |
|
133 | if (typeof shouldRetryOrPromise === 'object') {
|
134 | try {
|
135 | await shouldRetryOrPromise;
|
136 | return true;
|
137 | } catch (_err) {
|
138 | return false;
|
139 | }
|
140 | }
|
141 | return shouldRetryOrPromise;
|
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 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | export default function axiosRetry(axios, defaultOptions) {
|
197 | axios.interceptors.request.use((config) => {
|
198 | const currentState = getCurrentState(config);
|
199 | currentState.lastRequestTime = Date.now();
|
200 | return config;
|
201 | });
|
202 |
|
203 | axios.interceptors.response.use(null, async (error) => {
|
204 | const { config } = error;
|
205 |
|
206 |
|
207 | if (!config) {
|
208 | return Promise.reject(error);
|
209 | }
|
210 |
|
211 | const {
|
212 | retries = 3,
|
213 | retryCondition = isNetworkOrIdempotentRequestError,
|
214 | retryDelay = noDelay,
|
215 | shouldResetTimeout = false
|
216 | } = getRequestOptions(config, defaultOptions);
|
217 |
|
218 | const currentState = getCurrentState(config);
|
219 |
|
220 | if (await shouldRetry(retries, retryCondition, currentState, error)) {
|
221 | currentState.retryCount += 1;
|
222 | const delay = retryDelay(currentState.retryCount, error);
|
223 |
|
224 |
|
225 |
|
226 | fixConfig(axios, config);
|
227 |
|
228 | if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) {
|
229 | const lastRequestDuration = Date.now() - currentState.lastRequestTime;
|
230 |
|
231 | config.timeout = Math.max(config.timeout - lastRequestDuration - delay, 1);
|
232 | }
|
233 |
|
234 | config.transformRequest = [(data) => data];
|
235 |
|
236 | return new Promise((resolve) => setTimeout(() => resolve(axios(config)), delay));
|
237 | }
|
238 |
|
239 | return Promise.reject(error);
|
240 | });
|
241 | }
|
242 |
|
243 |
|
244 | axiosRetry.isNetworkError = isNetworkError;
|
245 | axiosRetry.isSafeRequestError = isSafeRequestError;
|
246 | axiosRetry.isIdempotentRequestError = isIdempotentRequestError;
|
247 | axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError;
|
248 | axiosRetry.exponentialDelay = exponentialDelay;
|
249 | axiosRetry.isRetryableError = isRetryableError;
|