UNPKG

bitcitawebfrontlib-ibsalut

Version:

Angular CLI project used by other Angular projects in the bitcita web project.<br />

172 lines 9.4 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var http_1 = require("@angular/common/http"); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var error_1 = require("../data/error"); var locator_service_1 = require("./locator.service"); var message_service_1 = require("./message.service"); var general_utils_service_1 = 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; var injector = locator_service_1.LocatorService.injector; HttpUtilsService_1.CONTEXT_PATH = contextPath; console.log("HttpUtilsSerive creado con API Url:" + this.apiUrl); } HttpUtilsService_1 = HttpUtilsService; HttpUtilsService.prototype.handleError = function (error) { this.generalUtils.stopLoading(); var customError = HttpUtilsService_1.getCustomError(error); this.messageService.sendError(customError); return rxjs_1.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_1.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_1.CONTEXT_PATH); var newUrl; if (index != -1) { newUrl = currentUrl.substring(0, index + HttpUtilsService_1.CONTEXT_PATH.length); } else { newUrl = currentUrl; } console.log("new url " + newUrl); window.location.href = newUrl; }; HttpUtilsService.prototype.getHttpPost = function (url, hqlRequest, emptyResponse) { var _this = this; 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(operators_1.tap(function (res) { return _this.generalUtils.stopLoading(); }, function (error) { return _this.handleError(error); })); }; HttpUtilsService.prototype.getHttpPut = function (url, hqlRequest) { var _this = this; 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(operators_1.tap(function (res) { return _this.generalUtils.stopLoading(); }, function (error) { return _this.handleError(error); })); }; HttpUtilsService.prototype.getHttpDelete = function (url) { var _this = this; 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(operators_1.tap(function (res) { return _this.generalUtils.stopLoading(); }, function (error) { return _this.handleError(error); })); }; HttpUtilsService.prototype.getHttpGet = function (url) { var _this = this; 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(operators_1.tap(function (res) { return _this.generalUtils.stopLoading(); }, function (error) { return _this.handleError(error); })); }; HttpUtilsService.prototype.addAuthenticationData = function (httpHeaders) { if (HttpUtilsService_1.LOGIN_AUTHENTICATION && HttpUtilsService_1.LOGIN_AUTHENTICATION != "") { httpHeaders = httpHeaders.append("Authorization", "Basic " + HttpUtilsService_1.LOGIN_AUTHENTICATION); } return httpHeaders; }; var HttpUtilsService_1; // customizado por proyecto: {provide: 'CONTEXT_PATH', useValue: environment.CONTEXT} HttpUtilsService.CONTEXT_PATH = "bitcitafront"; HttpUtilsService = HttpUtilsService_1 = __decorate([ core_1.Injectable(), __param(3, core_1.Inject("API_URL")), __param(4, core_1.Inject("CONTEXT_PATH")), __metadata("design:paramtypes", [http_1.HttpClient, message_service_1.MessageService, general_utils_service_1.GeneralUtils, String, String]) ], HttpUtilsService); return HttpUtilsService; }()); exports.HttpUtilsService = HttpUtilsService; exports.httpUtilsServiceInjectables = [ { provide: HttpUtilsService, useClass: HttpUtilsService } ]; //# sourceMappingURL=http-utils.service.js.map