UNPKG

x-ng2-http-interceptor

Version:
1 lines 14.9 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/http"),require("@angular/core"),require("rxjs/Observable"),require("rxjs/add/observable/empty"),require("rxjs/add/observable/of"),require("rxjs/add/observable/throw"),require("rxjs/add/operator/catch"),require("rxjs/add/operator/mergeMap")):"function"==typeof define&&define.amd?define(["exports","@angular/http","@angular/core","rxjs/Observable","rxjs/add/observable/empty","rxjs/add/observable/of","rxjs/add/observable/throw","rxjs/add/operator/catch","rxjs/add/operator/mergeMap"],e):e((t.ng=t.ng||{},t.ng["x-ng2-http-interceptor"]=t.ng["x-ng2-http-interceptor"]||{}),t.ng.http,t.ng.core,t.Rx)}(this,function(t,e,r,o){"use strict";function n(t,e){return new g(t,e)}var i=function(){function t(){}return t.from=function(e){var r={};return t.assign(r,e),r.sharedData={},r},t.assign=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];if(!t)throw TypeError("Cannot convert undefined or null to object");for(var o=function(e){e&&Object.keys(e).forEach(function(r){return t[r]=e[r]})},n=0,i=e;n<i.length;n++){var u=i[n];o(u)}return t},t}(),u=function(){function t(t){i.assign(this,t)}return Object.defineProperty(t.prototype,"url",{get:function(){return this._url},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"options",{get:function(){return this._options},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"response",{get:function(){return this._response},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"sharedData",{get:function(){return this._sharedData},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"shortCircuitTriggeredBy",{get:function(){return+this._shortCircuitTriggeredBy},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"forceReturnResponse",{get:function(){return!!this._forceReturnResponse},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"forceRequestCompletion",{get:function(){return!!this._forceRequestCompletion},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"responseGeneratedByShortCircuitHandler",{get:function(){return!!this._responseGeneratedByShortCircuitHandler},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"err",{get:function(){return this._err},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"errEncounteredAt",{get:function(){return+this._errEncounteredAt},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"errEncounteredInRequestCycle",{get:function(){return!!this._errEncounteredInRequestCycle},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"responseGeneratedByErrHandler",{get:function(){return!!this._err&&!!this._response},enumerable:!0,configurable:!0}),t.prototype.isShortCircuited=function(){return void 0!==this._shortCircuitTriggeredBy&&null!==this._shortCircuitTriggeredBy&&!!this._shortCircuitTriggeredBy},t.prototype.circuitShortedByMe=function(t){return this.isShortCircuited()&&this._shortCircuitTriggeredBy===t},t.prototype.errThrownByMe=function(t){return!!this._errEncounteredAt&&this._errEncounteredAt===t},t}(),s=function(){function t(t){i.assign(this,t)}return Object.defineProperty(t.prototype,"url",{get:function(){return this._url},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"options",{get:function(){return this._options},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"sharedData",{get:function(){return this._sharedData},enumerable:!0,configurable:!0}),t}(),p=function(){function t(){}return t.new=function(e){var r=new t;return i.assign(r,e),r},t.prototype.url=function(t){return this._url=t,this},t.prototype.options=function(t){return this._options=t,this},t.prototype.sharedData=function(t){return this._sharedData=t,this},t.prototype.shortCircuitAtCurrentStep=function(t){return this._shortCircuitAtCurrentStep=t,this},t.prototype.alsoForceRequestCompletion=function(t){return this._alsoForceRequestCompletion=t,this},t.prototype.build=function(){return this._sharedData=this._sharedData||{},new s(this)},t}(),c=function(t,e){function r(){this.constructor=t}for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o]);t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)},a=function(t){function e(){t.call(this)}return c(e,t),e.new=function(t){var r=new e;return i.assign(r,t),r},e.prototype.url=function(e){return t.prototype.url.call(this,e),this},e.prototype.options=function(e){return t.prototype.options.call(this,e),this},e.prototype.sharedData=function(e){return t.prototype.sharedData.call(this,e),this},e.prototype.shortCircuitAtCurrentStep=function(e){return t.prototype.shortCircuitAtCurrentStep.call(this,e),this},e.prototype.alsoForceRequestCompletion=function(e){return t.prototype.alsoForceRequestCompletion.call(this,e),this},e.prototype.alreadyShortCircuited=function(t){return this._alreadyShortCircuited=t,this},e.prototype.shortCircuitTriggeredBy=function(t){return this._shortCircuitTriggeredBy=t,this},e.prototype.err=function(t){return this._err=t,this},e.prototype.errEncounteredAt=function(t){return this._errEncounteredAt=t,this},e.prototype.getUrl=function(){return this._url},e.prototype.getOptions=function(){return this._options},e.prototype.getSharedData=function(){return this._sharedData},e.prototype.getShortCircuitAtCurrentStep=function(){return this._shortCircuitAtCurrentStep},e.prototype.getAlsoForceRequestCompletion=function(){return this._alsoForceRequestCompletion},e.prototype.getAlreadyShortCircuited=function(){return this._alreadyShortCircuited},e.prototype.getShortCircuitTriggeredBy=function(){return this._shortCircuitTriggeredBy},e.prototype.getErr=function(){return this._err},e.prototype.getErrEncounteredAt=function(){return this._errEncounteredAt},e}(p),h=function(t,e){function r(){this.constructor=t}for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o]);t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)},f=function(t){function e(){t.call(this,null)}return h(e,t),e.prototype.getShortCircuitAtCurrentStep=function(){return this._shortCircuitAtCurrentStep},e.prototype.getAlsoForceRequestCompletion=function(){return this._alsoForceRequestCompletion},e.prototype.getAlreadyShortCircuited=function(){return this._alreadyShortCircuited},e.prototype.getShortCircuitTriggeredBy=function(){return this._shortCircuitTriggeredBy},e.prototype.getErr=function(){return this._err},e.prototype.getErrEncounteredAt=function(){return this._errEncounteredAt},e}(s),l=function(){function t(){}return t.new=function(r){var o=new t;return r instanceof e.Response?o._response=r:r&&i.assign(o,r),o},t.prototype.response=function(t){return this._response=t,this},t.prototype.sharedData=function(t){return this._sharedData=t,this},t.prototype.forceReturnResponse=function(t){return this._forceReturnResponse=t,this},t.prototype.forceRequestCompletion=function(t){return this._forceRequestCompletion=t,this},t.prototype.err=function(t){return this._err=t,this},t.prototype.build=function(){return this._sharedData=this._sharedData||{},new u(this)},t}(),d=function(t,e){function r(){this.constructor=t}for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o]);t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)},y=function(t){function r(){t.call(this)}return d(r,t),r.newInternal=function(t,o){var n=new r;if(o instanceof e.Response)n._response=o;else if(o instanceof u)i.assign(n,o);else{var s=new f;i.assign(s,o),i.assign(n,o),s.getShortCircuitAtCurrentStep()&&n.shortCircuitTriggeredBy(t-1).forceRequestCompletion(s.getAlsoForceRequestCompletion())}return n},r.prototype.url=function(t){return this._url=t,this},r.prototype.options=function(t){return this._options=t,this},r.prototype.response=function(e){return e&&t.prototype.response.call(this,e),this},r.prototype.sharedData=function(e){return t.prototype.sharedData.call(this,e),this},r.prototype.shortCircuitTriggeredBy=function(t){return this._shortCircuitTriggeredBy=t,this},r.prototype.forceReturnResponse=function(e){return t.prototype.forceReturnResponse.call(this,e),this},r.prototype.forceRequestCompletion=function(e){return t.prototype.forceRequestCompletion.call(this,e),this},r.prototype.responseGeneratedByShortCircuitHandler=function(t){return this._responseGeneratedByShortCircuitHandler=t,this},r.prototype.err=function(e){return t.prototype.err.call(this,e),this},r.prototype.errEncounteredAt=function(t){return this._errEncounteredAt=t,this},r.prototype.errEncounteredInRequestCycle=function(t){return this._errEncounteredInRequestCycle=t,this},r}(l),b=function(t,e){function r(){this.constructor=t}for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o]);t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)},g=function(t){function r(e,r){t.call(this,e,r),this.interceptors=[]}return b(r,t),r.prototype.request=function(t,r){var o;o=r?this.representsInterceptorFlavor(r)?r:i.from(r):{},o.headers=o.headers||new e.Headers;var n=a.new().url(t).options(o).sharedData(o.sharedData||{}).build();return this.httpRequest(n)},r.prototype.get=function(t,r){return r=r||{},r.method=e.RequestMethod.Get,r.url=r.url||t,this.request(t,r)},r.prototype.post=function(t,r,o){return o=o||{},o.method=e.RequestMethod.Post,o.url=o.url||t,o.body=o.body||r,this.request(t,o)},r.prototype.put=function(t,r,o){return o=o||{},o.method=e.RequestMethod.Put,o.url=o.url||t,o.body=o.body||r,this.request(t,o)},r.prototype.delete=function(t,r){return r=r||{},r.method=e.RequestMethod.Delete,r.url=r.url||t,this.request(t,r)},r.prototype.patch=function(t,r,o){return o=o||{},o.method=e.RequestMethod.Patch,o.url=o.url||t,o.body=o.body||r,this.request(t,o)},r.prototype.head=function(t,r){return r=r||{},r.method=e.RequestMethod.Head,r.url=r.url||t,this.request(t,r)},r.prototype.options=function(t,r){return r=r||{},r.method=e.RequestMethod.Options,r.url=r.url||t,this.request(t,r)},r.prototype.addInterceptor=function(t){this.interceptors.push(t)},Object.defineProperty(r.prototype,"realResponseObservableTransformer",{set:function(t){this._realResponseObservableTransformer=t},enumerable:!0,configurable:!0}),r.prototype.httpRequest=function(e){var r=this;return this.runBeforeInterceptors(e).flatMap(function(e,n){var i=e,u=a.new(i);if(u.getErr()||u.getAlreadyShortCircuited()){var s=y.newInternal(r.interceptors.length,i).build();return o.Observable.of(s)}if(u.getShortCircuitAtCurrentStep()){var s=y.newInternal(r.interceptors.length,i).build();return o.Observable.of(s)}var p=t.prototype.request.call(r,e.url,e.options);return r._realResponseObservableTransformer&&(p=r._realResponseObservableTransformer.transform(p,e,new r.HttpDirect,r)),p.map(function(t){return y.newInternal(r.interceptors.length,i).response(t).build()}).catch(function(t){var e=y.newInternal(r.interceptors.length,i).err(t).errEncounteredAt(r.interceptors.length).errEncounteredInRequestCycle(!0);return o.Observable.of(e.build())})}).flatMap(function(t,e){return r.runAfterInterceptors(t)}).flatMap(function(t,e){return t.response?o.Observable.of(t.response):t.err?o.Observable.throw(t.err):t.isShortCircuited()?o.Observable.throw(new Error("Short circuit was triggered, but no short circuit handlers generated any resonse")):o.Observable.throw(new Error("Response is empty"))})},r.prototype.runBeforeInterceptors=function(t){for(var e=o.Observable.of(t),r=function(t){var r=n.interceptors[t];e=e.flatMap(function(e,n){var i=a.new(e);if(i.getErr()||i.getAlreadyShortCircuited())return o.Observable.of(e);if(i.getShortCircuitAtCurrentStep()){var u=a.new(e).shortCircuitAtCurrentStep(!1).shortCircuitTriggeredBy(t-1).alreadyShortCircuited(!0);return o.Observable.of(u.build())}if(void 0!==r.beforeRequest){var s=r.beforeRequest(e,t),p=void 0;return p=s?s instanceof o.Observable?s:o.Observable.of(s):o.Observable.of(e),p.catch(function(r,n){var i=a.new(e).err(r).errEncounteredAt(t);return o.Observable.of(i.build())})}return o.Observable.of(e)})},n=this,i=0;i<this.interceptors.length;i++)r(i);return e},r.prototype.runAfterInterceptors=function(t){var e,r=o.Observable.of(t);e=t.err?t.errEncounteredAt:t.isShortCircuited()?t.shortCircuitTriggeredBy:this.interceptors.length-1,e===this.interceptors.length&&(e=this.interceptors.length-1);for(var n=function(t){var e=i.interceptors[t];r=r.flatMap(function(r,n){if(r.forceRequestCompletion)return void 0!==e.onForceCompleteOrForceReturn&&e.onForceCompleteOrForceReturn(r,t),0===t?o.Observable.empty():o.Observable.of(r);if(r.forceReturnResponse)return void 0!==e.onForceCompleteOrForceReturn&&e.onForceCompleteOrForceReturn(r,t),o.Observable.of(r);var i;r.err?void 0!==e.onErr&&(i=e.onErr(r,t)):r.isShortCircuited()?void 0!==e.onShortCircuit&&(i=e.onShortCircuit(r,t)):void 0!==e.onResponse&&(i=e.onResponse(r,t));var s;return s=i?i instanceof u?o.Observable.of(i):i:o.Observable.of(r),s.catch(function(e,n){var i=y.newInternal(t,r).err(e).errEncounteredAt(t).errEncounteredInRequestCycle(!1);return o.Observable.of(i.build())})})},i=this,s=e;s>=0;s--)n(s);return r},r.prototype.representsInterceptorFlavor=function(t){return void 0!==t.sharedData&&null!==t.sharedData},Object.defineProperty(r.prototype,"HttpDirect",{get:function(){var t=this;return function(){function e(){}return e.prototype.request=function(e,r){return t.requestNative(e,r)},e.prototype.get=function(e,r){return t.getNative(e,r)},e.prototype.post=function(e,r,o){return t.postNative(e,r,o)},e.prototype.put=function(e,r,o){return t.putNative(e,r,o)},e.prototype.delete=function(e,r){return t.deleteNative(e,r)},e.prototype.patch=function(e,r,o){return t.patchNative(e,r,o)},e.prototype.head=function(e,r){return t.headNative(e,r)},e.prototype.options=function(e,r){return t.optionsNative(e,r)},e}()},enumerable:!0,configurable:!0}),r.prototype.requestNative=function(e,r){return t.prototype.request.call(this,e,r)},r.prototype.getNative=function(e,r){return t.prototype.get.call(this,e,r)},r.prototype.postNative=function(e,r,o){return t.prototype.post.call(this,e,r,o)},r.prototype.putNative=function(e,r,o){return t.prototype.put.call(this,e,r,o)},r.prototype.deleteNative=function(e,r){return t.prototype.delete.call(this,e,r)},r.prototype.patchNative=function(e,r,o){return t.prototype.patch.call(this,e,r,o)},r.prototype.headNative=function(e,r){return t.prototype.head.call(this,e,r)},r.prototype.optionsNative=function(e,r){return t.prototype.options.call(this,e,r)},r}(e.Http),v=function(){function t(){}return t.decorators=[{type:r.NgModule,args:[{providers:[{provide:g,useFactory:n,deps:[e.ConnectionBackend,e.RequestOptions]}]}]}],t.ctorParameters=function(){return[]},t}();t.InterceptorModule=v,t.InterceptorRequestBuilder=p,t.InterceptorRequest=s,t.InterceptorResponseWrapperBuilder=l,t.InterceptorResponseWrapper=u,t.InterceptorService=g,t.InterceptorUtils=i,Object.defineProperty(t,"__esModule",{value:!0})});