bitfront-library
Version:
Angular CLI project with components and classes used by other Angular projects of the BIT foundation.
155 lines • 8.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.httpUtilsServiceInjectables = exports.HttpUtilsService = void 0;
var core_1 = require("@angular/core");
var http_1 = require("@angular/common/http");
var error_1 = require("../data/error");
var message_service_1 = require("./message.service");
var general_utils_service_1 = require("./general-utils.service");
var i0 = require("@angular/core");
var i1 = require("@angular/common/http");
var i2 = require("./message.service");
var i3 = require("./general-utils.service");
var HttpUtilsService = /** @class */ (function () {
function HttpUtilsService(http, messageService, generalUtils, apiUrl, contextPath) {
this.http = http;
this.messageService = messageService;
this.generalUtils = generalUtils;
this.apiUrl = apiUrl;
this.contextPath = contextPath;
HttpUtilsService.CONTEXT_PATH = contextPath;
console.log("HttpUtilsSerive creado con API Url:" + this.apiUrl);
}
/*
private handleError(error: HttpErrorResponse) {
//this.generalUtils.stopLoading();
let customError = HttpUtilsService.getCustomError(error);
this.messageService.sendError(customError);
return throwError(customError);
}*/
HttpUtilsService.getCustomError = function (error) {
var lastUrlAcceded = error.url;
var innerError;
var customError;
if (error.message &&
(error.message.includes("Unexpected token < in JSON") || //chrome
error.message.includes("JSON.parse: unexpected character at line 1 column 1") || //firefox
error.message.includes("JSON.parse Error: Invalid character at position:1")) //explorer
) {
console.log("Parece que hemos perdido la sesión");
//customError = new CustomError(lastUrlAcceded, "S'ha detectat que s'ha perdut la sessió amb el servidor. És necessari que torni a autenticar-se en el sistema.\n Si no apareix la pàgina d'introducció d'usuari i contrasenya, per favor tanqui el navegador i torni a obrir l'aplicació.",
//"S'ha detectat que s'ha perdut la sessió amb el servidor. És necessari que torni a autenticar-se en el sistema.\n Si no apareix la pàgina d'introducció d'usuari i contrasenya, per favor tanqui el navegador i torni a obrir l'aplicació.", undefined, true, null);
HttpUtilsService.reloadPage();
return;
}
try {
innerError = JSON.parse(error.error);
}
catch (err) {
innerError = error.error;
}
if (innerError && (innerError["notificacion"] || innerError["trazaError"])) {
//el error es controlado del servidor
customError = innerError;
}
else {
//el error es un problema de comunicaciones o del propio cliente Angular
if (error.status || error.status === 0) {
//caso jetty o error controlado del servidor
console.log("HTTP Error con status " + error.status);
if (error.status >= 300 && error.status < 500) {
customError = new error_1.Error(lastUrlAcceded, "Error d'accés o de connexió: No s'ha trobat la informació sol·licitada", "Error de acceso o de conexión: No se ha encontrado la información solicitada", undefined, true, null);
}
else {
customError = new error_1.Error(lastUrlAcceded, "S'ha produït un error en el servidor; per favor, esperi uns segons i torni a realitzar la mateixa acció", "Se ha producido un error en el servidor; por favor, espere unos segundos y vuelva a realizar la misma acción", undefined, true, null);
}
}
else {
customError = new error_1.Error(lastUrlAcceded, error.message, error.message, error.error);
}
}
return customError;
};
HttpUtilsService.reloadPage = function () {
var currentUrl = window.location.href;
console.log("current url " + currentUrl);
var index = currentUrl.lastIndexOf(HttpUtilsService.CONTEXT_PATH);
var newUrl;
if (index != -1) {
newUrl = currentUrl.substring(0, index + HttpUtilsService.CONTEXT_PATH.length);
}
else {
newUrl = currentUrl;
}
console.log("new url " + newUrl);
window.location.href = newUrl;
};
HttpUtilsService.prototype.getHttpPost = function (url, hqlRequest, emptyResponse) {
if (emptyResponse === void 0) { emptyResponse = false; }
console.log("calling url server via POST:" + this.apiUrl + url);
//this.generalUtils.startLoading();
var body = JSON.stringify(hqlRequest);
var headers = new http_1.HttpHeaders().set("Content-Type", "application/json");
headers = this.addAuthenticationData(headers);
var vResponseType = emptyResponse ? "text" : "json";
return this.http.post(this.apiUrl + url, body, { headers: headers, responseType: vResponseType });
//.pipe(tap(res => //this.generalUtils.stopLoading(), error => this.handleError(error)));
};
HttpUtilsService.prototype.getHttpPut = function (url, hqlRequest) {
console.log("calling url server via PUT:" + this.apiUrl + url);
//this.generalUtils.startLoading();
var body = JSON.stringify(hqlRequest);
var headers = new http_1.HttpHeaders().set("Content-Type", "application/json");
headers = this.addAuthenticationData(headers);
return this.http.put(this.apiUrl + url, body, { headers: headers });
//.pipe(//tap(res => //this.generalUtils.stopLoading(), error => this.handleError(error)));
};
HttpUtilsService.prototype.getHttpDelete = function (url) {
console.log("calling url server via DELETE:" + this.apiUrl + url);
//this.generalUtils.startLoading();
var headers = new http_1.HttpHeaders().set("Content-Type", "application/json");
headers = this.addAuthenticationData(headers);
return this.http.delete(this.apiUrl + url, {
headers: headers,
responseType: "text"
});
//.pipe(tap(res => //this.generalUtils.stopLoading(), error => this.handleError(error)));
};
HttpUtilsService.prototype.getHttpGet = function (url) {
console.log("calling url server via GET:" + this.apiUrl + url);
//this.generalUtils.startLoading();
var headers = this.addAuthenticationData(new http_1.HttpHeaders());
return this.http.get(this.apiUrl + url, {
headers: headers
});
//.pipe(tap(res => //this.generalUtils.stopLoading(), error => this.handleError(error)));
};
HttpUtilsService.prototype.addAuthenticationData = function (httpHeaders) {
if (HttpUtilsService.LOGIN_AUTHENTICATION && HttpUtilsService.LOGIN_AUTHENTICATION != "") {
httpHeaders = httpHeaders.append("Authorization", "Basic " + HttpUtilsService.LOGIN_AUTHENTICATION);
}
return httpHeaders;
};
// customizado por proyecto: {provide: 'CONTEXT_PATH', useValue: environment.CONTEXT}
HttpUtilsService.CONTEXT_PATH = "bitcitafront";
HttpUtilsService.ɵfac = function HttpUtilsService_Factory(t) { return new (t || HttpUtilsService)(i0.ɵɵinject(i1.HttpClient), i0.ɵɵinject(i2.MessageService), i0.ɵɵinject(i3.GeneralUtils), i0.ɵɵinject("API_URL"), i0.ɵɵinject("CONTEXT_PATH")); };
HttpUtilsService.ɵprov = i0.ɵɵdefineInjectable({ token: HttpUtilsService, factory: HttpUtilsService.ɵfac });
return HttpUtilsService;
}());
exports.HttpUtilsService = HttpUtilsService;
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(HttpUtilsService, [{
type: core_1.Injectable
}], function () { return [{ type: i1.HttpClient }, { type: i2.MessageService }, { type: i3.GeneralUtils }, { type: undefined, decorators: [{
type: core_1.Inject,
args: ["API_URL"]
}] }, { type: undefined, decorators: [{
type: core_1.Inject,
args: ["CONTEXT_PATH"]
}] }]; }, null); })();
exports.httpUtilsServiceInjectables = [
{
provide: HttpUtilsService,
useClass: HttpUtilsService
}
];
//# sourceMappingURL=http-utils.service.js.map