index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.linearDelay = 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. /**
  91. * Linear delay
  92. * @param {number | undefined} delayFactor - delay factor in milliseconds (default: 100)
  93. * @returns {function} (retryNumber: number, error: AxiosError | undefined) => number
  94. */
  95. function linearDelay(delayFactor = 100) {
  96. return (retryNumber = 0, error = undefined) => {
  97. const delay = retryNumber * delayFactor;
  98. return Math.max(delay, retryAfter(error));
  99. };
  100. }
  101. exports.linearDelay = linearDelay;
  102. exports.DEFAULT_OPTIONS = {
  103. retries: 3,
  104. retryCondition: isNetworkOrIdempotentRequestError,
  105. retryDelay: noDelay,
  106. shouldResetTimeout: false,
  107. onRetry: () => { },
  108. onMaxRetryTimesExceeded: () => { },
  109. validateResponse: null
  110. };
  111. function getRequestOptions(config, defaultOptions) {
  112. return Object.assign(Object.assign(Object.assign({}, exports.DEFAULT_OPTIONS), defaultOptions), config[exports.namespace]);
  113. }
  114. function setCurrentState(config, defaultOptions, resetLastRequestTime = false) {
  115. const currentState = getRequestOptions(config, defaultOptions || {});
  116. currentState.retryCount = currentState.retryCount || 0;
  117. if (!currentState.lastRequestTime || resetLastRequestTime) {
  118. currentState.lastRequestTime = Date.now();
  119. }
  120. config[exports.namespace] = currentState;
  121. return currentState;
  122. }
  123. function fixConfig(axiosInstance, config) {
  124. // @ts-ignore
  125. if (axiosInstance.defaults.agent === config.agent) {
  126. // @ts-ignore
  127. delete config.agent;
  128. }
  129. if (axiosInstance.defaults.httpAgent === config.httpAgent) {
  130. delete config.httpAgent;
  131. }
  132. if (axiosInstance.defaults.httpsAgent === config.httpsAgent) {
  133. delete config.httpsAgent;
  134. }
  135. }
  136. function shouldRetry(currentState, error) {
  137. return __awaiter(this, void 0, void 0, function* () {
  138. const { retries, retryCondition } = currentState;
  139. const shouldRetryOrPromise = (currentState.retryCount || 0) < retries && retryCondition(error);
  140. // This could be a promise
  141. if (typeof shouldRetryOrPromise === 'object') {
  142. try {
  143. const shouldRetryPromiseResult = yield shouldRetryOrPromise;
  144. // keep return true unless shouldRetryPromiseResult return false for compatibility
  145. return shouldRetryPromiseResult !== false;
  146. }
  147. catch (_err) {
  148. return false;
  149. }
  150. }
  151. return shouldRetryOrPromise;
  152. });
  153. }
  154. function handleRetry(axiosInstance, currentState, error, config) {
  155. var _a;
  156. return __awaiter(this, void 0, void 0, function* () {
  157. currentState.retryCount += 1;
  158. const { retryDelay, shouldResetTimeout, onRetry } = currentState;
  159. const delay = retryDelay(currentState.retryCount, error);
  160. // Axios fails merging this configuration to the default configuration because it has an issue
  161. // with circular structures: https://github.com/mzabriskie/axios/issues/370
  162. fixConfig(axiosInstance, config);
  163. if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) {
  164. const lastRequestDuration = Date.now() - currentState.lastRequestTime;
  165. const timeout = config.timeout - lastRequestDuration - delay;
  166. if (timeout <= 0) {
  167. return Promise.reject(error);
  168. }
  169. config.timeout = timeout;
  170. }
  171. config.transformRequest = [(data) => data];
  172. yield onRetry(currentState.retryCount, error, config);
  173. if ((_a = config.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
  174. return Promise.resolve(axiosInstance(config));
  175. }
  176. return new Promise((resolve) => {
  177. var _a;
  178. const abortListener = () => {
  179. clearTimeout(timeout);
  180. resolve(axiosInstance(config));
  181. };
  182. const timeout = setTimeout(() => {
  183. var _a;
  184. resolve(axiosInstance(config));
  185. if ((_a = config.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener) {
  186. config.signal.removeEventListener('abort', abortListener);
  187. }
  188. }, delay);
  189. if ((_a = config.signal) === null || _a === void 0 ? void 0 : _a.addEventListener) {
  190. config.signal.addEventListener('abort', abortListener, { once: true });
  191. }
  192. });
  193. });
  194. }
  195. function handleMaxRetryTimesExceeded(currentState, error) {
  196. return __awaiter(this, void 0, void 0, function* () {
  197. if (currentState.retryCount >= currentState.retries)
  198. yield currentState.onMaxRetryTimesExceeded(error, currentState.retryCount);
  199. });
  200. }
  201. const axiosRetry = (axiosInstance, defaultOptions) => {
  202. const requestInterceptorId = axiosInstance.interceptors.request.use((config) => {
  203. var _a;
  204. setCurrentState(config, defaultOptions, true);
  205. if ((_a = config[exports.namespace]) === null || _a === void 0 ? void 0 : _a.validateResponse) {
  206. // by setting this, all HTTP responses will be go through the error interceptor first
  207. config.validateStatus = () => false;
  208. }
  209. return config;
  210. });
  211. const responseInterceptorId = axiosInstance.interceptors.response.use(null, (error) => __awaiter(void 0, void 0, void 0, function* () {
  212. var _a;
  213. const { config } = error;
  214. // If we have no information to retry the request
  215. if (!config) {
  216. return Promise.reject(error);
  217. }
  218. const currentState = setCurrentState(config, defaultOptions);
  219. if (error.response && ((_a = currentState.validateResponse) === null || _a === void 0 ? void 0 : _a.call(currentState, error.response))) {
  220. // no issue with response
  221. return error.response;
  222. }
  223. if (yield shouldRetry(currentState, error)) {
  224. return handleRetry(axiosInstance, currentState, error, config);
  225. }
  226. yield handleMaxRetryTimesExceeded(currentState, error);
  227. return Promise.reject(error);
  228. }));
  229. return { requestInterceptorId, responseInterceptorId };
  230. };
  231. // Compatibility with CommonJS
  232. axiosRetry.isNetworkError = isNetworkError;
  233. axiosRetry.isSafeRequestError = isSafeRequestError;
  234. axiosRetry.isIdempotentRequestError = isIdempotentRequestError;
  235. axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError;
  236. axiosRetry.exponentialDelay = exponentialDelay;
  237. axiosRetry.linearDelay = linearDelay;
  238. axiosRetry.isRetryableError = isRetryableError;
  239. exports.default = axiosRetry;