index.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. exports.DEFAULT_OPTIONS = exports.exponentialDelay = exports.retryAfter = exports.isNetworkOrIdempotentRequestError = exports.isIdempotentRequestError = exports.isSafeRequestError = exports.isRetryableError = exports.isNetworkError = exports.namespace = void 0;
  16. const is_retry_allowed_1 = __importDefault(require("is-retry-allowed"));
  17. exports.namespace = 'axios-retry';
  18. function isNetworkError(error) {
  19. const CODE_EXCLUDE_LIST = ['ERR_CANCELED', 'ECONNABORTED'];
  20. if (error.response) {
  21. return false;
  22. }
  23. if (!error.code) {
  24. return false;
  25. }
  26. // Prevents retrying timed out & cancelled requests
  27. if (CODE_EXCLUDE_LIST.includes(error.code)) {
  28. return false;
  29. }
  30. // Prevents retrying unsafe errors
  31. return (0, is_retry_allowed_1.default)(error);
  32. }
  33. exports.isNetworkError = isNetworkError;
  34. const SAFE_HTTP_METHODS = ['get', 'head', 'options'];
  35. const IDEMPOTENT_HTTP_METHODS = SAFE_HTTP_METHODS.concat(['put', 'delete']);
  36. function isRetryableError(error) {
  37. return (error.code !== 'ECONNABORTED' &&
  38. (!error.response ||
  39. error.response.status === 429 ||
  40. (error.response.status >= 500 && error.response.status <= 599)));
  41. }
  42. exports.isRetryableError = isRetryableError;
  43. function isSafeRequestError(error) {
  44. var _a;
  45. if (!((_a = error.config) === null || _a === void 0 ? void 0 : _a.method)) {
  46. // Cannot determine if the request can be retried
  47. return false;
  48. }
  49. return isRetryableError(error) && SAFE_HTTP_METHODS.indexOf(error.config.method) !== -1;
  50. }
  51. exports.isSafeRequestError = isSafeRequestError;
  52. function isIdempotentRequestError(error) {
  53. var _a;
  54. if (!((_a = error.config) === null || _a === void 0 ? void 0 : _a.method)) {
  55. // Cannot determine if the request can be retried
  56. return false;
  57. }
  58. return isRetryableError(error) && IDEMPOTENT_HTTP_METHODS.indexOf(error.config.method) !== -1;
  59. }
  60. exports.isIdempotentRequestError = isIdempotentRequestError;
  61. function isNetworkOrIdempotentRequestError(error) {
  62. return isNetworkError(error) || isIdempotentRequestError(error);
  63. }
  64. exports.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError;
  65. function retryAfter(error = undefined) {
  66. var _a;
  67. const retryAfterHeader = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.headers['retry-after'];
  68. if (!retryAfterHeader) {
  69. return 0;
  70. }
  71. // if the retry after header is a number, convert it to milliseconds
  72. let retryAfterMs = (Number(retryAfterHeader) || 0) * 1000;
  73. // If the retry after header is a date, get the number of milliseconds until that date
  74. if (retryAfterMs === 0) {
  75. retryAfterMs = (new Date(retryAfterHeader).valueOf() || 0) - Date.now();
  76. }
  77. return Math.max(0, retryAfterMs);
  78. }
  79. exports.retryAfter = retryAfter;
  80. function noDelay(_retryNumber = 0, error = undefined) {
  81. return Math.max(0, retryAfter(error));
  82. }
  83. function exponentialDelay(retryNumber = 0, error = undefined, delayFactor = 100) {
  84. const calculatedDelay = Math.pow(2, retryNumber) * delayFactor;
  85. const delay = Math.max(calculatedDelay, retryAfter(error));
  86. const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay
  87. return delay + randomSum;
  88. }
  89. exports.exponentialDelay = exponentialDelay;
  90. exports.DEFAULT_OPTIONS = {
  91. retries: 3,
  92. retryCondition: isNetworkOrIdempotentRequestError,
  93. retryDelay: noDelay,
  94. shouldResetTimeout: false,
  95. onRetry: () => { },
  96. onMaxRetryTimesExceeded: () => { },
  97. validateResponse: null
  98. };
  99. function getRequestOptions(config, defaultOptions) {
  100. return Object.assign(Object.assign(Object.assign({}, exports.DEFAULT_OPTIONS), defaultOptions), config[exports.namespace]);
  101. }
  102. function setCurrentState(config, defaultOptions) {
  103. const currentState = getRequestOptions(config, defaultOptions || {});
  104. currentState.retryCount = currentState.retryCount || 0;
  105. currentState.lastRequestTime = currentState.lastRequestTime || Date.now();
  106. config[exports.namespace] = currentState;
  107. return currentState;
  108. }
  109. function fixConfig(axiosInstance, config) {
  110. // @ts-ignore
  111. if (axiosInstance.defaults.agent === config.agent) {
  112. // @ts-ignore
  113. delete config.agent;
  114. }
  115. if (axiosInstance.defaults.httpAgent === config.httpAgent) {
  116. delete config.httpAgent;
  117. }
  118. if (axiosInstance.defaults.httpsAgent === config.httpsAgent) {
  119. delete config.httpsAgent;
  120. }
  121. }
  122. function shouldRetry(currentState, error) {
  123. return __awaiter(this, void 0, void 0, function* () {
  124. const { retries, retryCondition } = currentState;
  125. const shouldRetryOrPromise = (currentState.retryCount || 0) < retries && retryCondition(error);
  126. // This could be a promise
  127. if (typeof shouldRetryOrPromise === 'object') {
  128. try {
  129. const shouldRetryPromiseResult = yield shouldRetryOrPromise;
  130. // keep return true unless shouldRetryPromiseResult return false for compatibility
  131. return shouldRetryPromiseResult !== false;
  132. }
  133. catch (_err) {
  134. return false;
  135. }
  136. }
  137. return shouldRetryOrPromise;
  138. });
  139. }
  140. function handleRetry(axiosInstance, currentState, error, config) {
  141. var _a;
  142. return __awaiter(this, void 0, void 0, function* () {
  143. currentState.retryCount += 1;
  144. const { retryDelay, shouldResetTimeout, onRetry } = currentState;
  145. const delay = retryDelay(currentState.retryCount, error);
  146. // Axios fails merging this configuration to the default configuration because it has an issue
  147. // with circular structures: https://github.com/mzabriskie/axios/issues/370
  148. fixConfig(axiosInstance, config);
  149. if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) {
  150. const lastRequestDuration = Date.now() - currentState.lastRequestTime;
  151. const timeout = config.timeout - lastRequestDuration - delay;
  152. if (timeout <= 0) {
  153. return Promise.reject(error);
  154. }
  155. config.timeout = timeout;
  156. }
  157. config.transformRequest = [(data) => data];
  158. yield onRetry(currentState.retryCount, error, config);
  159. if ((_a = config.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
  160. return Promise.resolve(axiosInstance(config));
  161. }
  162. return new Promise((resolve) => {
  163. var _a;
  164. const abortListener = () => {
  165. clearTimeout(timeout);
  166. resolve(axiosInstance(config));
  167. };
  168. const timeout = setTimeout(() => {
  169. var _a;
  170. resolve(axiosInstance(config));
  171. if ((_a = config.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener) {
  172. config.signal.removeEventListener('abort', abortListener);
  173. }
  174. }, delay);
  175. if ((_a = config.signal) === null || _a === void 0 ? void 0 : _a.addEventListener) {
  176. config.signal.addEventListener('abort', abortListener, { once: true });
  177. }
  178. });
  179. });
  180. }
  181. function handleMaxRetryTimesExceeded(currentState, error) {
  182. return __awaiter(this, void 0, void 0, function* () {
  183. if (currentState.retryCount >= currentState.retries)
  184. yield currentState.onMaxRetryTimesExceeded(error, currentState.retryCount);
  185. });
  186. }
  187. const axiosRetry = (axiosInstance, defaultOptions) => {
  188. const requestInterceptorId = axiosInstance.interceptors.request.use((config) => {
  189. var _a;
  190. setCurrentState(config, defaultOptions);
  191. if ((_a = config[exports.namespace]) === null || _a === void 0 ? void 0 : _a.validateResponse) {
  192. // by setting this, all HTTP responses will be go through the error interceptor first
  193. config.validateStatus = () => false;
  194. }
  195. return config;
  196. });
  197. const responseInterceptorId = axiosInstance.interceptors.response.use(null, (error) => __awaiter(void 0, void 0, void 0, function* () {
  198. var _a;
  199. const { config } = error;
  200. // If we have no information to retry the request
  201. if (!config) {
  202. return Promise.reject(error);
  203. }
  204. const currentState = setCurrentState(config, defaultOptions);
  205. if (error.response && ((_a = currentState.validateResponse) === null || _a === void 0 ? void 0 : _a.call(currentState, error.response))) {
  206. // no issue with response
  207. return error.response;
  208. }
  209. if (yield shouldRetry(currentState, error)) {
  210. return handleRetry(axiosInstance, currentState, error, config);
  211. }
  212. yield handleMaxRetryTimesExceeded(currentState, error);
  213. return Promise.reject(error);
  214. }));
  215. return { requestInterceptorId, responseInterceptorId };
  216. };
  217. // Compatibility with CommonJS
  218. axiosRetry.isNetworkError = isNetworkError;
  219. axiosRetry.isSafeRequestError = isSafeRequestError;
  220. axiosRetry.isIdempotentRequestError = isIdempotentRequestError;
  221. axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError;
  222. axiosRetry.exponentialDelay = exponentialDelay;
  223. axiosRetry.isRetryableError = isRetryableError;
  224. exports.default = axiosRetry;