UNPKG

ng-http-interceptor

Version:
1 lines 10.7 kB
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("rxjs/Observable"),require("rxjs/add/observable/of"),require("rxjs/add/observable/empty"),require("rxjs/add/operator/switchMap"),require("rxjs/add/operator/mergeMap"),require("@angular/core"),require("@angular/http")):"function"==typeof define&&define.amd?define(["exports","rxjs/Observable","rxjs/add/observable/of","rxjs/add/observable/empty","rxjs/add/operator/switchMap","rxjs/add/operator/mergeMap","@angular/core","@angular/http"],factory):factory(global.httpInterceptor=global.httpInterceptor||{},global.Rx,global.Rx.Observable,global.Rx.Observable,global.Rx.Observable.prototype,global.Rx.Observable.prototype,global.ng.core,global.ng.http)}(this,function(exports,rxjs_Observable,rxjs_add_observable_of,rxjs_add_observable_empty,rxjs_add_operator_switchMap,rxjs_add_operator_mergeMap,_angular_core,_angular_http){"use strict";function __extends(d,b){function __(){this.constructor=d}extendStatics(d,b),d.prototype=null===b?Object.create(b):(__.prototype=b.prototype,new __)}function identityFactory_(ref){return ref}function identityFactory(provide,obj){return{provide:provide,useFactory:identityFactory_,deps:[obj]}}function safeProxyHandler_(handler){var safeHandler={};return SAFE_PROXY_TRAPS.filter(function(trap){return"function"==typeof handler[trap]}).forEach(function(trap){return safeHandler[trap]=handler[trap].bind(handler)}),safeHandler}function safeProxy(obj,handler){return new Proxy(obj,safeProxyHandler_(handler))}function _proxyFactory(http,interceptor){return safeProxy(_proxyTarget,new InterceptableHttpProxyService(http,interceptor))}function proxyFactory(backend,options,interceptor){return _proxyFactory(new _angular_http.Http(backend,options),interceptor)}function getHttpOptionsIdx(method){switch(method){case"post":case"put":case"patch":return 2;default:return 1}}function getHttpOptions(data,method,alwaysOriginal){return void 0===alwaysOriginal&&(alwaysOriginal=!1),alwaysOriginal?data[getHttpOptionsIdx(method)]:data[getHttpOptionsIdx(method)]||new _angular_http.RequestOptions}function getHttpOptionsAndIdx(data,method,alwaysOriginal){return void 0===alwaysOriginal&&(alwaysOriginal=!1),{options:getHttpOptions(data,method,alwaysOriginal),idx:getHttpOptionsIdx(method)}}function getHttpHeadersOrInit(data,method){var _a=getHttpOptionsAndIdx(data,method),options=_a.options,idx=_a.idx,headers=options.headers;return options.headers||(headers=new _angular_http.Headers,options.headers=headers),data[idx]=options,headers}var extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)b.hasOwnProperty(p)&&(d[p]=b[p])},InterceptableStoreFactory=function(){function InterceptableStoreFactory(){}return InterceptableStoreFactory.prototype.createStore=function(){return new InterceptableStore},InterceptableStoreFactory}();InterceptableStoreFactory.decorators=[{type:_angular_core.Injectable}],InterceptableStoreFactory.ctorParameters=function(){return[]};var DEFAULT_URL_STORE="/",InterceptableStore=function(){function InterceptableStore(){this.storeMatcher={},this.stores={},this.activeStore=DEFAULT_URL_STORE}return Object.defineProperty(InterceptableStore.prototype,"store",{get:function(){return this._getStoreSafely(this.activeStore)},enumerable:!0,configurable:!0}),InterceptableStore.prototype.addInterceptor=function(interceptor){return this.store.push(interceptor),this},InterceptableStore.prototype.removeInterceptor=function(interceptor){var idx=this.store.indexOf(interceptor);return-1===idx?this:(this.store.splice(idx,1),this)},InterceptableStore.prototype.clearInterceptors=function(interceptors){var _this=this;return void 0===interceptors&&(interceptors=[]),interceptors.length>0?interceptors.forEach(function(i){return _this.removeInterceptor(i)}):this.store.splice(0),this},InterceptableStore.prototype.setActiveStore=function(url){return void 0===url&&(url=DEFAULT_URL_STORE),this.activeStore=String(url),url instanceof RegExp&&(this.storeMatcher[this.activeStore]=url),this},InterceptableStore.prototype.getStore=function(key){return void 0===key&&(key=DEFAULT_URL_STORE),this._getStoreSafely(key)},InterceptableStore.prototype.getMatchedStores=function(url){var _this=this;void 0===url&&(url=DEFAULT_URL_STORE);var backedUrl="/"+url.replace("/","\\/")+"/";return Object.keys(this.stores).filter(function(k){return k===url||k===backedUrl||_this.storeMatcher[k]&&_this.storeMatcher[k].test(url)}).filter(function(k,i,arr){return k!==DEFAULT_URL_STORE&&arr.indexOf(k)===i}).map(function(k){return _this.getStore(k)}).reduce(function(stores,store){return stores.concat(store)},this.getStore(DEFAULT_URL_STORE))},InterceptableStore.prototype._getStoreSafely=function(key){return this.stores[key]||(this.stores[key]=[])},InterceptableStore}(),HttpInterceptorService=function(){function HttpInterceptorService(store){this.store=store,this._requestStore=this.store.createStore(),this._responseStore=this.store.createStore()}return HttpInterceptorService.wrapInObservable=function(res){return res instanceof rxjs_Observable.Observable?res:rxjs_Observable.Observable.of(res)},HttpInterceptorService.prototype.request=function(url){return void 0===url&&(url=DEFAULT_URL_STORE),this._requestStore.setActiveStore(url)},HttpInterceptorService.prototype.response=function(url){return void 0===url&&(url=DEFAULT_URL_STORE),this._responseStore.setActiveStore(url)},HttpInterceptorService.prototype._interceptRequest=function(url,method,data,context){return this._requestStore.getMatchedStores(url).reduce(function(o,i){return o.flatMap(function(d){return d?HttpInterceptorService.wrapInObservable(i(d,method,context)):rxjs_Observable.Observable.of(d)})},rxjs_Observable.Observable.of(data))},HttpInterceptorService.prototype._interceptResponse=function(url,method,response,context){return this._responseStore.getMatchedStores(url).reduce(function(o,i){return i(o,method,context)},response)},HttpInterceptorService}();HttpInterceptorService.decorators=[{type:_angular_core.Injectable}],HttpInterceptorService.ctorParameters=function(){return[{type:InterceptableStoreFactory}]};var SAFE_PROXY_TRAPS=["get","set","apply"],InterceptableHttpProxyService=function(){function InterceptableHttpProxyService(http,httpInterceptorService){this.http=http,this.httpInterceptorService=httpInterceptorService}return InterceptableHttpProxyService._extractUrl=function(url){var dirtyUrl=url[0];return"object"==typeof dirtyUrl&&"url"in dirtyUrl?dirtyUrl.url:dirtyUrl},InterceptableHttpProxyService.prototype.get=function(target,p,receiver){return InterceptableHttpProxyService._callStack.push(p),receiver},InterceptableHttpProxyService.prototype.apply=function(target,thisArg,argArray){var _this=this,method=InterceptableHttpProxyService._callStack.pop();if(!method)return rxjs_Observable.Observable.empty();var context=Object.create(null);return this.httpInterceptorService._interceptRequest(InterceptableHttpProxyService._extractUrl(argArray),method,argArray,context).switchMap(function(args){if(!args)return rxjs_Observable.Observable.empty();var response=_this.http[method].apply(_this.http,args);return _this.httpInterceptorService._interceptResponse(InterceptableHttpProxyService._extractUrl(args),method,response,context)})},InterceptableHttpProxyService}();InterceptableHttpProxyService._callStack=[],InterceptableHttpProxyService.decorators=[{type:_angular_core.Injectable}],InterceptableHttpProxyService.ctorParameters=function(){return[{type:_angular_http.Http},{type:HttpInterceptorService}]};var _proxyTarget=function(){return null};Object.keys(_angular_http.Http.prototype).forEach(function(method){return _proxyTarget[method]="Http."+method});var InterceptableHttpProxyProviders=[{provide:_angular_http.Http,useFactory:proxyFactory,deps:[_angular_http.XHRBackend,_angular_http.RequestOptions,HttpInterceptorService]},identityFactory(InterceptableHttpProxyService,_angular_http.Http)],InterceptableHttpProxyNoOverrideProviders=[{provide:InterceptableHttpProxyService,useFactory:_proxyFactory,deps:[_angular_http.Http,HttpInterceptorService]}],InterceptableHttp=function(_super){function InterceptableHttp(_backend,_defaultOptions){return _super.call(this,_backend,_defaultOptions)||this}return __extends(InterceptableHttp,_super),InterceptableHttp}(_angular_http.Http);InterceptableHttp.decorators=[{type:_angular_core.Injectable}],InterceptableHttp.ctorParameters=function(){return[{type:_angular_http.ConnectionBackend},{type:_angular_http.RequestOptions}]};var InterceptableHttpProviders=[identityFactory(InterceptableHttp,InterceptableHttpProxyService)],SharedProviders=[InterceptableStoreFactory,HttpInterceptorService].concat(InterceptableHttpProviders),HTTP_INTERCEPTOR_PROVIDER=SharedProviders.concat(InterceptableHttpProxyProviders),HTTP_INTERCEPTOR_NO_OVERRIDE_PROVIDER=SharedProviders.concat(InterceptableHttpProxyNoOverrideProviders),HttpInterceptorModule=function(){function HttpInterceptorModule(){}return HttpInterceptorModule.noOverrideHttp=function(){return{ngModule:HttpInterceptorNoOverrideModule}},HttpInterceptorModule}();HttpInterceptorModule.decorators=[{type:_angular_core.NgModule,args:[{imports:[_angular_http.HttpModule],providers:[HTTP_INTERCEPTOR_PROVIDER]}]}],HttpInterceptorModule.ctorParameters=function(){return[]};var HttpInterceptorNoOverrideModule=function(){function HttpInterceptorNoOverrideModule(){}return HttpInterceptorNoOverrideModule}();HttpInterceptorNoOverrideModule.decorators=[{type:_angular_core.NgModule,args:[{imports:[_angular_http.HttpModule],providers:[HTTP_INTERCEPTOR_NO_OVERRIDE_PROVIDER]}]}],HttpInterceptorNoOverrideModule.ctorParameters=function(){return[]},exports.HttpInterceptorModule=HttpInterceptorModule,exports.HttpInterceptorNoOverrideModule=HttpInterceptorNoOverrideModule,exports.InterceptableHttp=InterceptableHttp,exports.InterceptableHttpProviders=InterceptableHttpProviders,exports.HttpInterceptorService=HttpInterceptorService,exports.getHttpOptions=getHttpOptions,exports.getHttpOptionsIdx=getHttpOptionsIdx,exports.getHttpOptionsAndIdx=getHttpOptionsAndIdx,exports.getHttpHeadersOrInit=getHttpHeadersOrInit,exports.ɵm=InterceptableHttpProxyNoOverrideProviders,exports.ɵl=InterceptableHttpProxyProviders,exports.ɵh=InterceptableHttpProxyService,exports.ɵj=_proxyFactory,exports.ɵi=_proxyTarget,exports.ɵk=proxyFactory,exports.ɵc=InterceptableStoreFactory,exports.ɵb=HTTP_INTERCEPTOR_NO_OVERRIDE_PROVIDER,exports.ɵa=HTTP_INTERCEPTOR_PROVIDER,exports.ɵe=identityFactory,exports.ɵd=identityFactory_,exports.ɵg=safeProxy,exports.ɵf=safeProxyHandler_,Object.defineProperty(exports,"__esModule",{value:!0})});