1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.isNetworkError = isNetworkError;
|
7 | exports.isRetryableError = isRetryableError;
|
8 | exports.isSafeRequestError = isSafeRequestError;
|
9 | exports.isIdempotentRequestError = isIdempotentRequestError;
|
10 | exports.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError;
|
11 | exports.exponentialDelay = exponentialDelay;
|
12 | exports.default = axiosRetry;
|
13 |
|
14 | var _isRetryAllowed = require('is-retry-allowed');
|
15 |
|
16 | var _isRetryAllowed2 = _interopRequireDefault(_isRetryAllowed);
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | var namespace = 'axios-retry';
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function isNetworkError(error) {
|
27 | return !error.response && Boolean(error.code) &&
|
28 | error.code !== 'ECONNABORTED' &&
|
29 | (0, _isRetryAllowed2.default)(error);
|
30 | }
|
31 |
|
32 | var SAFE_HTTP_METHODS = ['get', 'head', 'options'];
|
33 | var IDEMPOTENT_HTTP_METHODS = SAFE_HTTP_METHODS.concat(['put', 'delete']);
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function isRetryableError(error) {
|
40 | return error.code !== 'ECONNABORTED' && (!error.response || error.response.status >= 500 && error.response.status <= 599);
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function isSafeRequestError(error) {
|
48 | if (!error.config) {
|
49 |
|
50 | return false;
|
51 | }
|
52 |
|
53 | return isRetryableError(error) && SAFE_HTTP_METHODS.indexOf(error.config.method) !== -1;
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function isIdempotentRequestError(error) {
|
61 | if (!error.config) {
|
62 |
|
63 | return false;
|
64 | }
|
65 |
|
66 | return isRetryableError(error) && IDEMPOTENT_HTTP_METHODS.indexOf(error.config.method) !== -1;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function isNetworkOrIdempotentRequestError(error) {
|
74 | return isNetworkError(error) || isIdempotentRequestError(error);
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | function noDelay() {
|
81 | return 0;
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function exponentialDelay() {
|
89 | var retryNumber = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
90 |
|
91 | var delay = Math.pow(2, retryNumber) * 100;
|
92 | var randomSum = delay * 0.2 * Math.random();
|
93 | return delay + randomSum;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function getCurrentState(config) {
|
102 | var currentState = config[namespace] || {};
|
103 | currentState.retryCount = currentState.retryCount || 0;
|
104 | config[namespace] = currentState;
|
105 | return currentState;
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | function getRequestOptions(config, defaultOptions) {
|
115 | return Object.assign({}, defaultOptions, config[namespace]);
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | function fixConfig(axios, config) {
|
123 | if (axios.defaults.agent === config.agent) {
|
124 | delete config.agent;
|
125 | }
|
126 | if (axios.defaults.httpAgent === config.httpAgent) {
|
127 | delete config.httpAgent;
|
128 | }
|
129 | if (axios.defaults.httpsAgent === config.httpsAgent) {
|
130 | delete config.httpsAgent;
|
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 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | function axiosRetry(axios, defaultOptions) {
|
187 | axios.interceptors.request.use(function (config) {
|
188 | var currentState = getCurrentState(config);
|
189 | currentState.lastRequestTime = Date.now();
|
190 | return config;
|
191 | });
|
192 |
|
193 | axios.interceptors.response.use(null, function (error) {
|
194 | var config = error.config;
|
195 |
|
196 |
|
197 | if (!config) {
|
198 | return Promise.reject(error);
|
199 | }
|
200 |
|
201 | var _getRequestOptions = getRequestOptions(config, defaultOptions),
|
202 | _getRequestOptions$re = _getRequestOptions.retries,
|
203 | retries = _getRequestOptions$re === undefined ? 3 : _getRequestOptions$re,
|
204 | _getRequestOptions$re2 = _getRequestOptions.retryCondition,
|
205 | retryCondition = _getRequestOptions$re2 === undefined ? isNetworkOrIdempotentRequestError : _getRequestOptions$re2,
|
206 | _getRequestOptions$re3 = _getRequestOptions.retryDelay,
|
207 | retryDelay = _getRequestOptions$re3 === undefined ? noDelay : _getRequestOptions$re3,
|
208 | _getRequestOptions$sh = _getRequestOptions.shouldResetTimeout,
|
209 | shouldResetTimeout = _getRequestOptions$sh === undefined ? false : _getRequestOptions$sh;
|
210 |
|
211 | var currentState = getCurrentState(config);
|
212 |
|
213 | var shouldRetry = retryCondition(error) && currentState.retryCount < retries;
|
214 |
|
215 | if (shouldRetry) {
|
216 | currentState.retryCount += 1;
|
217 | var delay = retryDelay(currentState.retryCount, error);
|
218 |
|
219 |
|
220 |
|
221 | fixConfig(axios, config);
|
222 |
|
223 | if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) {
|
224 | var lastRequestDuration = Date.now() - currentState.lastRequestTime;
|
225 |
|
226 | config.timeout = Math.max(config.timeout - lastRequestDuration - delay, 1);
|
227 | }
|
228 |
|
229 | config.transformRequest = [function (data) {
|
230 | return data;
|
231 | }];
|
232 |
|
233 | return new Promise(function (resolve) {
|
234 | return setTimeout(function () {
|
235 | return resolve(axios(config));
|
236 | }, delay);
|
237 | });
|
238 | }
|
239 |
|
240 | return Promise.reject(error);
|
241 | });
|
242 | }
|
243 |
|
244 |
|
245 | axiosRetry.isNetworkError = isNetworkError;
|
246 | axiosRetry.isSafeRequestError = isSafeRequestError;
|
247 | axiosRetry.isIdempotentRequestError = isIdempotentRequestError;
|
248 | axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError;
|
249 | axiosRetry.exponentialDelay = exponentialDelay;
|
250 | axiosRetry.isRetryableError = isRetryableError;
|
251 |
|
\ | No newline at end of file |