axios-retryer
Version:
axios-retryer is an advanced Axios request manager offering intelligent retry logic with token refresh, concurrency control, priority queuing, and a flexible plugin architecture, all built with TypeScript for robust HTTP client integrations.
2 lines (1 loc) • 2.55 kB
JavaScript
"use strict";var t=function(){return t=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var i in e=arguments[r])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},t.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var e=function(){function e(r){void 0===r&&(r={}),this.name="CircuitBreakerPlugin",this.version="1.0.0",this._state=e.STATES.CLOSED;this._options=t(t({},{failureThreshold:5,openTimeout:3e4,halfOpenMax:1}),r),this._state=e.STATES.CLOSED,this._failureCount=0,this._halfOpenCount=0,this._nextAttempt=Date.now()}return e.prototype.initialize=function(t){var r=this;this._manager=t;var n=t.axiosInstance;this._requestInterceptorId=n.interceptors.request.use((function(t){if(r._state===e.STATES.OPEN){if(!(Date.now()>=r._nextAttempt))return Promise.reject(new Error("Circuit is open: failing fast."));r._transitionToHalfOpen()}if(r._state===e.STATES.HALF_OPEN){if(r._halfOpenCount>=r._options.halfOpenMax)return Promise.reject(new Error("Circuit is half-open: too many test requests."));r._halfOpenCount++}return t})),this._responseInterceptorId=n.interceptors.response.use((function(t){return r._state===e.STATES.HALF_OPEN?r._reset():r._state===e.STATES.CLOSED&&(r._failureCount=0),t}),(function(t){return r._failureCount++,(r._state===e.STATES.HALF_OPEN||r._failureCount>=r._options.failureThreshold)&&r._trip(),Promise.reject(t)}))},e.prototype.onBeforeDestroyed=function(t){var e=t.axiosInstance;void 0!==this._requestInterceptorId&&e.interceptors.request.eject(this._requestInterceptorId),void 0!==this._responseInterceptorId&&e.interceptors.response.eject(this._responseInterceptorId)},e.prototype._trip=function(){this._state!==e.STATES.OPEN&&(this._state=e.STATES.OPEN,this._nextAttempt=Date.now()+this._options.openTimeout,this._manager&&"function"==typeof this._manager.getLogger&&this._manager.getLogger().error("".concat(this.name," tripped: entering OPEN state.")))},e.prototype._reset=function(){this._state=e.STATES.CLOSED,this._failureCount=0,this._halfOpenCount=0,this._manager&&"function"==typeof this._manager.getLogger&&this._manager.getLogger().debug("".concat(this.name," reset: entering CLOSED state."))},e.prototype._transitionToHalfOpen=function(){this._state=e.STATES.HALF_OPEN,this._halfOpenCount=0,this._manager&&"function"==typeof this._manager.getLogger&&this._manager.getLogger().debug("".concat(this.name," transitioning to HALF_OPEN state."))},e.STATES={CLOSED:"CLOSED",OPEN:"OPEN",HALF_OPEN:"HALF_OPEN"},e}();exports.CircuitBreakerPlugin=e;