x-ng2-http-interceptor
Version:
Adds interception capability around angular http api
885 lines (871 loc) • 38.4 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(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')) :
typeof define === 'function' && 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'], factory) :
(factory((global.ng = global.ng || {}, global.ng['x-ng2-http-interceptor'] = global.ng['x-ng2-http-interceptor'] || {}),global.ng.http,global.ng.core,global.Rx));
}(this, (function (exports,_angular_http,_angular_core,rxjs_Observable) { 'use strict';
var InterceptorUtils = (function () {
/**
* Forcing the user to use static methods, as this is a utility class
*/
function InterceptorUtils() {
}
InterceptorUtils.from = function (options) {
var interceptorRequestOptionsArgs = {};
InterceptorUtils.assign(interceptorRequestOptionsArgs, options);
interceptorRequestOptionsArgs.sharedData = {};
return interceptorRequestOptionsArgs;
};
InterceptorUtils.assign = function (target) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
if (!target) {
throw TypeError('Cannot convert undefined or null to object');
}
var _loop_1 = function(source) {
if (source) {
Object.keys(source).forEach(function (key) { return target[key] = source[key]; });
}
};
for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
var source = args_1[_a];
_loop_1(source);
}
return target;
};
return InterceptorUtils;
}());
var InterceptorResponseWrapper = (function () {
function InterceptorResponseWrapper(builder) {
InterceptorUtils.assign(this, builder);
}
Object.defineProperty(InterceptorResponseWrapper.prototype, "url", {
get: function () {
return this._url;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "options", {
get: function () {
return this._options;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "response", {
get: function () {
return this._response;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "sharedData", {
get: function () {
return this._sharedData;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "shortCircuitTriggeredBy", {
get: function () {
return +this._shortCircuitTriggeredBy;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "forceReturnResponse", {
get: function () {
return !!this._forceReturnResponse;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "forceRequestCompletion", {
get: function () {
return !!this._forceRequestCompletion;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "responseGeneratedByShortCircuitHandler", {
get: function () {
return !!this._responseGeneratedByShortCircuitHandler;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "err", {
get: function () {
return this._err;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "errEncounteredAt", {
get: function () {
return +this._errEncounteredAt;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "errEncounteredInRequestCycle", {
get: function () {
return !!this._errEncounteredInRequestCycle;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorResponseWrapper.prototype, "responseGeneratedByErrHandler", {
get: function () {
return !!this._err && !!this._response;
},
enumerable: true,
configurable: true
});
InterceptorResponseWrapper.prototype.isShortCircuited = function () {
return this._shortCircuitTriggeredBy !== undefined && this._shortCircuitTriggeredBy !== null && !!this._shortCircuitTriggeredBy;
};
InterceptorResponseWrapper.prototype.circuitShortedByMe = function (currentStep) {
return this.isShortCircuited() && this._shortCircuitTriggeredBy === currentStep;
};
InterceptorResponseWrapper.prototype.errThrownByMe = function (currentStep) {
return !!this._errEncounteredAt && this._errEncounteredAt === currentStep;
};
return InterceptorResponseWrapper;
}());
var InterceptorRequest = (function () {
function InterceptorRequest(builder) {
InterceptorUtils.assign(this, builder);
}
Object.defineProperty(InterceptorRequest.prototype, "url", {
get: function () {
return this._url;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorRequest.prototype, "options", {
get: function () {
return this._options;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InterceptorRequest.prototype, "sharedData", {
get: function () {
return this._sharedData;
},
enumerable: true,
configurable: true
});
return InterceptorRequest;
}());
/**
* Utility builder for creating a new instance of InterceptorRequest
* Use InterceptorRequestBuilder.new() to instantiate the builder
*/
var InterceptorRequestBuilder = (function () {
/**
* Use InterceptorRequestBuilder.new() to instantiate the builder
*/
function InterceptorRequestBuilder() {
}
InterceptorRequestBuilder.new = function (request) {
var builder = new InterceptorRequestBuilder();
InterceptorUtils.assign(builder, request);
return builder;
};
InterceptorRequestBuilder.prototype.url = function (url) {
this._url = url;
return this;
};
InterceptorRequestBuilder.prototype.options = function (options) {
this._options = options;
return this;
};
InterceptorRequestBuilder.prototype.sharedData = function (sharedData) {
this._sharedData = sharedData;
return this;
};
InterceptorRequestBuilder.prototype.shortCircuitAtCurrentStep = function (shortCircuitAtCurrentStep) {
this._shortCircuitAtCurrentStep = shortCircuitAtCurrentStep;
return this;
};
InterceptorRequestBuilder.prototype.alsoForceRequestCompletion = function (alsoForceRequestCompletion) {
this._alsoForceRequestCompletion = alsoForceRequestCompletion;
return this;
};
InterceptorRequestBuilder.prototype.build = function () {
this._sharedData = this._sharedData || {};
return new InterceptorRequest(this);
};
return InterceptorRequestBuilder;
}());
var __extends$1 = (undefined && undefined.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
* !!INTERNAL USE ONLY!!
* Utility builder for creating a new instance of _InterceptorRequest with additional ability to set internal properties aswell
* Use InterceptorRequestBuilderInternal.new() to instantiate the builder
*/
var InterceptorRequestBuilderInternal = (function (_super) {
__extends$1(InterceptorRequestBuilderInternal, _super);
/**
* Use InterceptorRequestBuilderInternal.new() to instantiate the builder
*/
function InterceptorRequestBuilderInternal() {
_super.call(this);
}
InterceptorRequestBuilderInternal.new = function (request) {
var builder = new InterceptorRequestBuilderInternal();
InterceptorUtils.assign(builder, request);
return builder;
};
InterceptorRequestBuilderInternal.prototype.url = function (url) {
_super.prototype.url.call(this, url);
return this;
};
InterceptorRequestBuilderInternal.prototype.options = function (options) {
_super.prototype.options.call(this, options);
return this;
};
InterceptorRequestBuilderInternal.prototype.sharedData = function (sharedData) {
_super.prototype.sharedData.call(this, sharedData);
return this;
};
InterceptorRequestBuilderInternal.prototype.shortCircuitAtCurrentStep = function (shortCircuitAtCurrentStep) {
_super.prototype.shortCircuitAtCurrentStep.call(this, shortCircuitAtCurrentStep);
return this;
};
InterceptorRequestBuilderInternal.prototype.alsoForceRequestCompletion = function (alsoForceRequestCompletion) {
_super.prototype.alsoForceRequestCompletion.call(this, alsoForceRequestCompletion);
return this;
};
InterceptorRequestBuilderInternal.prototype.alreadyShortCircuited = function (alreadyShortCircuited) {
this._alreadyShortCircuited = alreadyShortCircuited;
return this;
};
InterceptorRequestBuilderInternal.prototype.shortCircuitTriggeredBy = function (shortCircuitTriggeredBy) {
this._shortCircuitTriggeredBy = shortCircuitTriggeredBy;
return this;
};
InterceptorRequestBuilderInternal.prototype.err = function (err) {
this._err = err;
return this;
};
InterceptorRequestBuilderInternal.prototype.errEncounteredAt = function (errEncounteredAt) {
this._errEncounteredAt = errEncounteredAt;
return this;
};
InterceptorRequestBuilderInternal.prototype.getUrl = function () {
return this._url;
};
InterceptorRequestBuilderInternal.prototype.getOptions = function () {
return this._options;
};
InterceptorRequestBuilderInternal.prototype.getSharedData = function () {
return this._sharedData;
};
InterceptorRequestBuilderInternal.prototype.getShortCircuitAtCurrentStep = function () {
return this._shortCircuitAtCurrentStep;
};
InterceptorRequestBuilderInternal.prototype.getAlsoForceRequestCompletion = function () {
return this._alsoForceRequestCompletion;
};
InterceptorRequestBuilderInternal.prototype.getAlreadyShortCircuited = function () {
return this._alreadyShortCircuited;
};
InterceptorRequestBuilderInternal.prototype.getShortCircuitTriggeredBy = function () {
return this._shortCircuitTriggeredBy;
};
InterceptorRequestBuilderInternal.prototype.getErr = function () {
return this._err;
};
InterceptorRequestBuilderInternal.prototype.getErrEncounteredAt = function () {
return this._errEncounteredAt;
};
return InterceptorRequestBuilderInternal;
}(InterceptorRequestBuilder));
var __extends$3 = (undefined && undefined.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var InterceptorRequestInternal = (function (_super) {
__extends$3(InterceptorRequestInternal, _super);
/**
* Hack
* TODO: Point to typescript bug
*/
function InterceptorRequestInternal() {
_super.call(this, null);
}
InterceptorRequestInternal.prototype.getShortCircuitAtCurrentStep = function () {
return this._shortCircuitAtCurrentStep;
};
InterceptorRequestInternal.prototype.getAlsoForceRequestCompletion = function () {
return this._alsoForceRequestCompletion;
};
InterceptorRequestInternal.prototype.getAlreadyShortCircuited = function () {
return this._alreadyShortCircuited;
};
InterceptorRequestInternal.prototype.getShortCircuitTriggeredBy = function () {
return this._shortCircuitTriggeredBy;
};
InterceptorRequestInternal.prototype.getErr = function () {
return this._err;
};
InterceptorRequestInternal.prototype.getErrEncounteredAt = function () {
return this._errEncounteredAt;
};
return InterceptorRequestInternal;
}(InterceptorRequest));
/**
* Utility builder for creating a new instance of InterceptorResponseWrapper
* Use InterceptorResponseBuilder.new() to instantiate the builder
*/
var InterceptorResponseWrapperBuilder = (function () {
/**
* Use InterceptorResponseBuilder.new() to instantiate the builder
*/
function InterceptorResponseWrapperBuilder() {
}
InterceptorResponseWrapperBuilder.new = function (response) {
var builder = new InterceptorResponseWrapperBuilder();
if (response instanceof _angular_http.Response) {
builder._response = response;
}
else if (response) {
InterceptorUtils.assign(builder, response);
}
return builder;
};
InterceptorResponseWrapperBuilder.prototype.response = function (response) {
this._response = response;
return this;
};
InterceptorResponseWrapperBuilder.prototype.sharedData = function (sharedData) {
this._sharedData = sharedData;
return this;
};
InterceptorResponseWrapperBuilder.prototype.forceReturnResponse = function (forceReturnResponse) {
this._forceReturnResponse = forceReturnResponse;
return this;
};
InterceptorResponseWrapperBuilder.prototype.forceRequestCompletion = function (forceRequestCompletion) {
this._forceRequestCompletion = forceRequestCompletion;
return this;
};
InterceptorResponseWrapperBuilder.prototype.err = function (err) {
this._err = err;
return this;
};
InterceptorResponseWrapperBuilder.prototype.build = function () {
this._sharedData = this._sharedData || {};
return new InterceptorResponseWrapper(this);
};
return InterceptorResponseWrapperBuilder;
}());
var __extends$2 = (undefined && undefined.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
* !!INTERNAL USE ONLY!!
* Utility builder for creating a new instance of InterceptorResponseWrapper with additional ability to set internal properties aswell
* Use InterceptorResponseWrapperBuilderInternal.new() to instantiate the builder
*/
var InterceptorResponseWrapperBuilderInternal = (function (_super) {
__extends$2(InterceptorResponseWrapperBuilderInternal, _super);
/**
* Use InterceptorResponseWrapperBuilderInternal.new() to instantiate the builder
*/
function InterceptorResponseWrapperBuilderInternal() {
_super.call(this);
}
InterceptorResponseWrapperBuilderInternal.newInternal = function (interceptorStep, from) {
var builder = new InterceptorResponseWrapperBuilderInternal();
if (from instanceof _angular_http.Response) {
builder._response = from;
}
else if (from instanceof InterceptorResponseWrapper) {
InterceptorUtils.assign(builder, from);
}
else {
var request = new InterceptorRequestInternal();
InterceptorUtils.assign(request, from);
InterceptorUtils.assign(builder, from);
if (request.getShortCircuitAtCurrentStep()) {
builder.shortCircuitTriggeredBy(interceptorStep - 1)
.forceRequestCompletion(request.getAlsoForceRequestCompletion());
}
}
return builder;
};
InterceptorResponseWrapperBuilderInternal.prototype.url = function (url) {
this._url = url;
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.options = function (options) {
this._options = options;
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.response = function (response) {
if (response) {
_super.prototype.response.call(this, response);
}
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.sharedData = function (sharedData) {
_super.prototype.sharedData.call(this, sharedData);
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.shortCircuitTriggeredBy = function (shortCircuitTriggeredBy) {
this._shortCircuitTriggeredBy = shortCircuitTriggeredBy;
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.forceReturnResponse = function (forceReturnResponse) {
_super.prototype.forceReturnResponse.call(this, forceReturnResponse);
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.forceRequestCompletion = function (forceRequestCompletion) {
_super.prototype.forceRequestCompletion.call(this, forceRequestCompletion);
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.responseGeneratedByShortCircuitHandler = function (responseGeneratedByShortCircuitHandler) {
this._responseGeneratedByShortCircuitHandler = responseGeneratedByShortCircuitHandler;
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.err = function (err) {
_super.prototype.err.call(this, err);
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.errEncounteredAt = function (errEncounteredAt) {
this._errEncounteredAt = errEncounteredAt;
return this;
};
InterceptorResponseWrapperBuilderInternal.prototype.errEncounteredInRequestCycle = function (errEncounteredInRequestCycle) {
this._errEncounteredInRequestCycle = errEncounteredInRequestCycle;
return this;
};
return InterceptorResponseWrapperBuilderInternal;
}(InterceptorResponseWrapperBuilder));
var __extends = (undefined && undefined.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
* Wrapper around native angular `Http` service.
* Allows you to add `Interceptor`s that lets you do
* 1. Transform request before it reaches the actual request, such as, add headers transparently
* 2. Transform response, such as stripout the top level object & return the payload (or) raise errors if `response.status` is not `ok`
* 3. To short circuit the request flow based on runtime data
* 4. To selective caching/log request/responses
* 5. Augment the real `Observable<Response>` that native angular `http.request(..)` returns
* 6. Store intermediate data that can be shared across the interceptors
* 7. Generate handcrafted response incase of error/base of your runtime decision
*
* The service executes methods in the interceptor chain in the following manner
* 1. For each of the listed interceptor, tranforms the request by invoking\
* `beforeRequest(..)` on each interceptor in the same order they are added
* 2. Invokes native angular `http.request(..)` with the result of last interceptor's `beforeRequest(..)` response
* 3. Invokes `onResponse(..)` on each interceptor in the reverse order they are added
* 4. The response from `onResponse(..)` of the final interceptor in the chain would be sent to subscriber
*/
var InterceptorService = (function (_super) {
__extends(InterceptorService, _super);
function InterceptorService(backend, defaultOptions) {
_super.call(this, backend, defaultOptions);
this.interceptors = [];
}
/** Parent overrides **/
InterceptorService.prototype.request = function (url, options) {
var interceptorOptions;
if (!options) {
interceptorOptions = {};
}
else if (this.representsInterceptorFlavor(options)) {
interceptorOptions = options;
}
else {
interceptorOptions = InterceptorUtils.from(options);
}
interceptorOptions.headers = interceptorOptions.headers || new _angular_http.Headers();
var request = InterceptorRequestBuilderInternal.new()
.url(url)
.options(interceptorOptions)
.sharedData(interceptorOptions.sharedData || {})
.build();
return this.httpRequest(request);
};
/**
* Performs a request with `get` http method.
*/
InterceptorService.prototype.get = function (url, options) {
options = options || {};
options.method = _angular_http.RequestMethod.Get;
options.url = options.url || url;
return this.request(url, options);
};
/**
* Performs a request with `post` http method.
*/
InterceptorService.prototype.post = function (url, body, options) {
options = options || {};
options.method = _angular_http.RequestMethod.Post;
options.url = options.url || url;
options.body = options.body || body;
return this.request(url, options);
};
/**
* Performs a request with `put` http method.
*/
InterceptorService.prototype.put = function (url, body, options) {
options = options || {};
options.method = _angular_http.RequestMethod.Put;
options.url = options.url || url;
options.body = options.body || body;
return this.request(url, options);
};
/**
* Performs a request with `delete` http method.
*/
InterceptorService.prototype.delete = function (url, options) {
options = options || {};
options.method = _angular_http.RequestMethod.Delete;
options.url = options.url || url;
return this.request(url, options);
};
/**
* Performs a request with `patch` http method.
*/
InterceptorService.prototype.patch = function (url, body, options) {
options = options || {};
options.method = _angular_http.RequestMethod.Patch;
options.url = options.url || url;
options.body = options.body || body;
return this.request(url, options);
};
/**
* Performs a request with `head` http method.
*/
InterceptorService.prototype.head = function (url, options) {
options = options || {};
options.method = _angular_http.RequestMethod.Head;
options.url = options.url || url;
return this.request(url, options);
};
/**
* Performs a request with `options` http method.
*/
InterceptorService.prototype.options = function (url, options) {
options = options || {};
options.method = _angular_http.RequestMethod.Options;
options.url = options.url || url;
return this.request(url, options);
};
/**
* Appends this interceptor to the list of interceptors
*/
InterceptorService.prototype.addInterceptor = function (interceptor) {
this.interceptors.push(interceptor);
};
Object.defineProperty(InterceptorService.prototype, "realResponseObservableTransformer", {
/**
* Sets response transformer
*/
set: function (value) {
this._realResponseObservableTransformer = value;
},
enumerable: true,
configurable: true
});
/** Private functions **/
InterceptorService.prototype.httpRequest = function (request) {
var _this = this;
return this.runBeforeInterceptors(request)
.flatMap(function (transformedRequest, _) {
var transformedRequestInternal = transformedRequest;
var interceptorRequestInternalBuilder = InterceptorRequestBuilderInternal.new(transformedRequestInternal);
if (interceptorRequestInternalBuilder.getErr() || interceptorRequestInternalBuilder.getAlreadyShortCircuited()) {
var responseWrapper = InterceptorResponseWrapperBuilderInternal
.newInternal(_this.interceptors.length, transformedRequestInternal)
.build();
return rxjs_Observable.Observable.of(responseWrapper);
}
else if (interceptorRequestInternalBuilder.getShortCircuitAtCurrentStep()) {
var responseWrapper = InterceptorResponseWrapperBuilderInternal
.newInternal(_this.interceptors.length, transformedRequestInternal)
.build();
return rxjs_Observable.Observable.of(responseWrapper);
}
var response$ = _super.prototype.request.call(_this, transformedRequest.url, transformedRequest.options);
if (_this._realResponseObservableTransformer) {
response$ = _this._realResponseObservableTransformer.transform(response$, transformedRequest, new _this.HttpDirect(), _this);
}
return response$.map(function (response) {
return InterceptorResponseWrapperBuilderInternal
.newInternal(_this.interceptors.length, transformedRequestInternal)
.response(response)
.build();
}).catch(function (err) {
var responseBuilder = InterceptorResponseWrapperBuilderInternal
.newInternal(_this.interceptors.length, transformedRequestInternal)
.err(err)
.errEncounteredAt(_this.interceptors.length)
.errEncounteredInRequestCycle(true);
return rxjs_Observable.Observable.of(responseBuilder.build());
});
})
.flatMap(function (responseWrapper, index) {
return _this.runAfterInterceptors(responseWrapper);
})
.flatMap(function (responseWrapper, index) {
if (!responseWrapper.response) {
if (responseWrapper.err) {
return rxjs_Observable.Observable.throw(responseWrapper.err);
}
else if (responseWrapper.isShortCircuited()) {
return rxjs_Observable.Observable.throw(new Error('Short circuit was triggered, but no short circuit handlers generated any resonse'));
}
else {
return rxjs_Observable.Observable.throw(new Error('Response is empty'));
}
}
return rxjs_Observable.Observable.of(responseWrapper.response);
});
};
InterceptorService.prototype.runBeforeInterceptors = function (params) {
var request$ = rxjs_Observable.Observable.of(params);
var _loop_1 = function(index) {
var interceptor = this_1.interceptors[index];
request$ = request$
.flatMap(function (request, _) {
var requestInternalBuilder = InterceptorRequestBuilderInternal.new(request);
if (requestInternalBuilder.getErr() || requestInternalBuilder.getAlreadyShortCircuited()) {
return rxjs_Observable.Observable.of(request);
}
else if (requestInternalBuilder.getShortCircuitAtCurrentStep()) {
var requestBuilder = InterceptorRequestBuilderInternal.new(request)
.shortCircuitAtCurrentStep(false)
.shortCircuitTriggeredBy(index - 1) // since the last interceptor requested for short circuit
.alreadyShortCircuited(true);
return rxjs_Observable.Observable.of(requestBuilder.build());
}
if (interceptor.beforeRequest !== undefined) {
var processedRequest = interceptor.beforeRequest(request, index);
var processedRequest$ = void 0;
if (!processedRequest) {
processedRequest$ = rxjs_Observable.Observable.of(request);
}
else if (processedRequest instanceof rxjs_Observable.Observable) {
processedRequest$ = processedRequest;
}
else {
processedRequest$ = rxjs_Observable.Observable.of(processedRequest);
}
return processedRequest$
.catch(function (err, caught) {
var responseBuilder = InterceptorRequestBuilderInternal.new(request)
.err(err)
.errEncounteredAt(index);
return rxjs_Observable.Observable.of(responseBuilder.build());
});
}
return rxjs_Observable.Observable.of(request);
});
};
var this_1 = this;
for (var index = 0; index < this.interceptors.length; index++) {
_loop_1(index);
}
return request$;
};
InterceptorService.prototype.runAfterInterceptors = function (responseWrapper) {
var responseWrapper$ = rxjs_Observable.Observable.of(responseWrapper);
var startFrom;
if (responseWrapper.err) {
startFrom = responseWrapper.errEncounteredAt;
}
else if (responseWrapper.isShortCircuited()) {
startFrom = responseWrapper.shortCircuitTriggeredBy;
}
else {
startFrom = this.interceptors.length - 1;
}
// If error occurred when making actual server call, lets start from last interceptor in the chain
if (startFrom === this.interceptors.length) {
startFrom = this.interceptors.length - 1;
}
var _loop_2 = function(index) {
var interceptor = this_2.interceptors[index];
responseWrapper$ = responseWrapper$
.flatMap(function (transformedResponseWrapper, _) {
if (transformedResponseWrapper.forceRequestCompletion) {
if (interceptor.onForceCompleteOrForceReturn !== undefined) {
interceptor.onForceCompleteOrForceReturn(transformedResponseWrapper, index);
}
if (index === 0) {
return rxjs_Observable.Observable.empty();
}
else {
return rxjs_Observable.Observable.of(transformedResponseWrapper);
}
}
else if (transformedResponseWrapper.forceReturnResponse) {
if (interceptor.onForceCompleteOrForceReturn !== undefined) {
interceptor.onForceCompleteOrForceReturn(transformedResponseWrapper, index);
}
return rxjs_Observable.Observable.of(transformedResponseWrapper);
}
var processedResponse;
if (transformedResponseWrapper.err) {
if (interceptor.onErr !== undefined) {
processedResponse = interceptor.onErr(transformedResponseWrapper, index);
}
}
else if (transformedResponseWrapper.isShortCircuited()) {
if (interceptor.onShortCircuit !== undefined) {
processedResponse = interceptor.onShortCircuit(transformedResponseWrapper, index);
}
}
else if (interceptor.onResponse !== undefined) {
processedResponse = interceptor.onResponse(transformedResponseWrapper, index);
}
var procesedResponseWrapper$;
if (!processedResponse) {
procesedResponseWrapper$ = rxjs_Observable.Observable.of(transformedResponseWrapper);
}
else if (processedResponse instanceof InterceptorResponseWrapper) {
procesedResponseWrapper$ = rxjs_Observable.Observable.of(processedResponse);
}
else {
procesedResponseWrapper$ = processedResponse;
}
return procesedResponseWrapper$
.catch(function (err, __) {
var responseBuilder = InterceptorResponseWrapperBuilderInternal.newInternal(index, transformedResponseWrapper)
.err(err)
.errEncounteredAt(index)
.errEncounteredInRequestCycle(false);
return rxjs_Observable.Observable.of(responseBuilder.build());
});
});
};
var this_2 = this;
for (var index = startFrom; index >= 0; index--) {
_loop_2(index);
}
return responseWrapper$;
};
/**
* Tests whether the passed in object represents interceptor version/native request options
*/
InterceptorService.prototype.representsInterceptorFlavor = function (options) {
return options.sharedData !== undefined
&& options.sharedData !== null;
};
Object.defineProperty(InterceptorService.prototype, "HttpDirect", {
/**
* Returns an implementation that mirrors angular `Http` interface;
* This interface allows the response transformers to make calls directly to HTTP calls
* without being interceted by {@code InterceptorService}; i.e `this`
*/
get: function () {
var interceptorService = this;
return (function () {
function class_1() {
}
class_1.prototype.request = function (url, options) {
return interceptorService.requestNative(url, options);
};
class_1.prototype.get = function (url, options) {
return interceptorService.getNative(url, options);
};
class_1.prototype.post = function (url, body, options) {
return interceptorService.postNative(url, body, options);
};
class_1.prototype.put = function (url, body, options) {
return interceptorService.putNative(url, body, options);
};
class_1.prototype.delete = function (url, options) {
return interceptorService.deleteNative(url, options);
};
class_1.prototype.patch = function (url, body, options) {
return interceptorService.patchNative(url, body, options);
};
class_1.prototype.head = function (url, options) {
return interceptorService.headNative(url, options);
};
class_1.prototype.options = function (url, options) {
return interceptorService.optionsNative(url, options);
};
return class_1;
}());
},
enumerable: true,
configurable: true
});
InterceptorService.prototype.requestNative = function (url, options) {
return _super.prototype.request.call(this, url, options);
};
InterceptorService.prototype.getNative = function (url, options) {
return _super.prototype.get.call(this, url, options);
};
InterceptorService.prototype.postNative = function (url, body, options) {
return _super.prototype.post.call(this, url, body, options);
};
InterceptorService.prototype.putNative = function (url, body, options) {
return _super.prototype.put.call(this, url, body, options);
};
InterceptorService.prototype.deleteNative = function (url, options) {
return _super.prototype.delete.call(this, url, options);
};
InterceptorService.prototype.patchNative = function (url, body, options) {
return _super.prototype.patch.call(this, url, body, options);
};
InterceptorService.prototype.headNative = function (url, options) {
return _super.prototype.head.call(this, url, options);
};
InterceptorService.prototype.optionsNative = function (url, options) {
return _super.prototype.options.call(this, url, options);
};
return InterceptorService;
}(_angular_http.Http));
function provideInterceptorService(backend, defaultOptions) {
return new InterceptorService(backend, defaultOptions);
}
var InterceptorModule = (function () {
function InterceptorModule() {
}
InterceptorModule.decorators = [
{ type: _angular_core.NgModule, args: [{
providers: [
{
provide: InterceptorService,
useFactory: provideInterceptorService,
deps: [_angular_http.ConnectionBackend, _angular_http.RequestOptions]
}
]
},] },
];
/** @nocollapse */
InterceptorModule.ctorParameters = function () { return []; };
return InterceptorModule;
}());
exports.InterceptorModule = InterceptorModule;
exports.InterceptorRequestBuilder = InterceptorRequestBuilder;
exports.InterceptorRequest = InterceptorRequest;
exports.InterceptorResponseWrapperBuilder = InterceptorResponseWrapperBuilder;
exports.InterceptorResponseWrapper = InterceptorResponseWrapper;
exports.InterceptorService = InterceptorService;
exports.InterceptorUtils = InterceptorUtils;
Object.defineProperty(exports, '__esModule', { value: true });
})));