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.54 kB
JavaScript
var t=function(){return t=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},t.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var e=function(){function e(n){void 0===n&&(n={}),this.name="CircuitBreakerPlugin",this.version="1.0.0",this._state=e.STATES.CLOSED;this._options=t(t({},{failureThreshold:5,openTimeout:3e4,halfOpenMax:1}),n),this._state=e.STATES.CLOSED,this._failureCount=0,this._halfOpenCount=0,this._nextAttempt=Date.now()}return e.prototype.initialize=function(t){var n=this;this._manager=t;var r=t.axiosInstance;this._requestInterceptorId=r.interceptors.request.use((function(t){if(n._state===e.STATES.OPEN){if(!(Date.now()>=n._nextAttempt))return Promise.reject(new Error("Circuit is open: failing fast."));n._transitionToHalfOpen()}if(n._state===e.STATES.HALF_OPEN){if(n._halfOpenCount>=n._options.halfOpenMax)return Promise.reject(new Error("Circuit is half-open: too many test requests."));n._halfOpenCount++}return t})),this._responseInterceptorId=r.interceptors.response.use((function(t){return n._state===e.STATES.HALF_OPEN?n._reset():n._state===e.STATES.CLOSED&&(n._failureCount=0),t}),(function(t){return n._failureCount++,(n._state===e.STATES.HALF_OPEN||n._failureCount>=n._options.failureThreshold)&&n._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}();export{e as CircuitBreakerPlugin};