leaf-framework
Version:
Light Everis Angular Frontend Framework
159 lines • 6.47 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Injectable } from '@angular/core';
import { ConnectionBackend, RequestOptions, Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { CommonUtil } from 'base-api-service';
import { AuthHelper } from '../services/auth.helper';
import { SpinnerService } from '../spinner/spinner.service';
import { CookieIdentifiers } from '../constants/cookie.identifiers';
var InterceptedHttp = /** @class */ (function (_super) {
__extends(InterceptedHttp, _super);
function InterceptedHttp(backend, defaultOptions, spinnerService, authHelper) {
var _this = _super.call(this, backend, defaultOptions) || this;
_this.spinnerService = spinnerService;
_this.authHelper = authHelper;
// TODO how to inject these values into the service
_this.NOT_REQUIRE_AUTH = ['/oauth/token', 'logout'];
_this.AUTH_TYPE = 'Bearer';
return _this;
}
InterceptedHttp.prototype.get = function (url, options) {
var _this = this;
if (this.isHttpService(url)) {
if (this.canCallHttp(url)) {
this.requestInterceptor(url);
return _super.prototype.get.call(this, url, this.getRequestOptionArgs(options)).finally(function () { return _this.onFinally(); });
}
else {
return this.getAuthError();
}
}
else {
// if is other kind of resource
return _super.prototype.get.call(this, url, this.getRequestOptionArgs(options));
}
};
InterceptedHttp.prototype.post = function (url, body, options) {
var _this = this;
if (this.isHttpService(url)) {
if (this.canCallHttp(url)) {
this.requestInterceptor(url);
return _super.prototype.post.call(this, url, body, this.getRequestOptionArgs(options)).finally(function () { return _this.onFinally(); });
}
else {
return this.getAuthError();
}
}
else {
// if is other kind of resource
return _super.prototype.post.call(this, url, body, this.getRequestOptionArgs(options));
}
};
InterceptedHttp.prototype.put = function (url, body, options) {
var _this = this;
if (this.isHttpService(url)) {
if (this.canCallHttp(url)) {
this.requestInterceptor(url);
return _super.prototype.put.call(this, url, body, this.getRequestOptionArgs(options)).finally(function () { return _this.onFinally(); });
}
else {
return this.getAuthError();
}
}
else {
// if is other kind of resource
return _super.prototype.put.call(this, url, body, this.getRequestOptionArgs(options));
}
};
InterceptedHttp.prototype.delete = function (url, options) {
var _this = this;
if (this.isHttpService(url)) {
if (this.canCallHttp(url)) {
this.requestInterceptor(url);
return _super.prototype.delete.call(this, url, this.getRequestOptionArgs(options)).finally(function () { return _this.onFinally(); });
}
else {
return this.getAuthError();
}
}
else {
// if is other kind of resource
return _super.prototype.delete.call(this, url, this.getRequestOptionArgs(options));
}
};
/**
* Determine if the url is a valid http service
*/
InterceptedHttp.prototype.isHttpService = function (url) {
return url && url.startsWith('http');
};
/**
* Returns true If the url doesn't require previous authentication or require it bu the user is already logged in
*/
InterceptedHttp.prototype.canCallHttp = function (url) {
return this.authHelper.isUserLogged || !this.needAuthBefore(url);
};
InterceptedHttp.prototype.needAuthBefore = function (url) {
if (url == null) {
return null;
}
return this.NOT_REQUIRE_AUTH.find(function (partUrl) { return url.indexOf(partUrl) >= 0; }) == null;
};
InterceptedHttp.prototype.getAuthError = function () {
return Observable.throw({ status: 401, statusText: 'UNAUTHORIZED' });
};
InterceptedHttp.prototype.getRequestOptionArgs = function (options) {
options = options || new RequestOptions();
options.headers = options.headers || new Headers();
if (!options.headers.get('content-type')) {
options.headers.append('content-type', 'application/json; charset=utf-8');
}
var token = CommonUtil.getCookie(CookieIdentifiers.TOKEN_ID);
if (!CommonUtil.isEmpty(token)) {
options.headers.set('Authorization', this.AUTH_TYPE + " " + token);
}
return options;
};
/**
* Request interceptor.
*/
InterceptedHttp.prototype.requestInterceptor = function (url) {
if (this.needAuthBefore(url)) {
this.spinnerService.show();
}
};
/**
* Response interceptor.
*/
InterceptedHttp.prototype.responseInterceptor = function () {
this.spinnerService.hide();
};
/**
* onFinally
*/
InterceptedHttp.prototype.onFinally = function () {
this.responseInterceptor();
};
InterceptedHttp.decorators = [
{ type: Injectable },
];
/** @nocollapse */
InterceptedHttp.ctorParameters = function () { return [
{ type: ConnectionBackend, },
{ type: RequestOptions, },
{ type: SpinnerService, },
{ type: AuthHelper, },
]; };
return InterceptedHttp;
}(Http));
export { InterceptedHttp };
//# sourceMappingURL=http.interceptor.js.map