UNPKG

ferngully-aurelia-tools

Version:

Ferngully Tools for Aurelia

334 lines 12.7 kB
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); }; import { autoinject } from "aurelia-framework"; import { HttpClient } from "aurelia-fetch-client"; import { NumberService } from "./number-service"; import { AjaxErrorHandler as IErrorHandler } from "./handle-errors-service"; import { LoggingService } from "./logging-service"; import { I18N } from 'aurelia-i18n'; var Ajax = (function () { function Ajax(http, errorHandler, loggingService, numberService, i18n) { this.http = http; this.errorHandler = errorHandler; this.loggingService = loggingService; this.numberService = numberService; this.i18n = i18n; this.BaseUrl = Ajax_1.GlobalBaseUrl; this.noCacheHeader = { headers: { "cache-control": "no-cache", "If-Modified-Since": "0" } }; this._initialized = false; } Ajax_1 = Ajax; Ajax.prototype.configure = function () { var _this = this; if (this._initialized) { return; } this.http.configure(function (config) { config .withBaseUrl(_this.BaseUrl) .withDefaults({ credentials: "same-origin", headers: { "Content-Type": "application/json", "Accept": "application/json", "X-Requested-With": "Fetch" } }) .withInterceptor({}); }); this._initialized = true; }; Ajax.prototype.fetch = function (url, init) { var _this = this; this.configure(); this.loggingService.debug("fetching from: '" + url + "'"); return this.http.fetch(url, Object.assign(Object.assign({}, this.noCacheHeader), Object.assign({}, Ajax_1.GlobalRequestInit), init)) .catch(function (response) { if (response instanceof DOMException) { return _this.handleDOMException(response); } else if (response.json) { return response.json() .then(function (errorInfo) { if (errorInfo && errorInfo.Code) { return _this.handleServerError(errorInfo); } else { return _this.handleServerException(response); } }); } else { return _this.handleTypeError(response); } }) .then(function (response) { if (response instanceof Response) { var realResponse_1 = response; if (!realResponse_1.ok) { return realResponse_1.json() .catch(function () { return _this.handleServerException(realResponse_1); }) .then(function (errorInfo) { return _this.handleServerError(errorInfo); }); } } return _this.getAjaxResponse(response); }); }; Ajax.prototype.fetchNumber = function (url, init) { var _this = this; if (init === void 0) { init = {}; } try { return this.fetch(url, init) .then(function (response) { return _this.getAjaxResultForType(response, DataReturnType.Number); }); } catch (ex) { return this.handleJavaScriptException(ex); } }; Ajax.prototype.fetchText = function (url, init) { var _this = this; if (init === void 0) { init = {}; } try { return this.fetch(url, init) .then(function (response) { return _this.getAjaxResultForType(response, DataReturnType.Text); }); } catch (ex) { return this.handleJavaScriptException(ex); } }; Ajax.prototype.fetchJson = function (url, init) { var _this = this; if (init === void 0) { init = {}; } try { return this.fetch(url, init) .then(function (response) { return _this.getAjaxResultForType(response, DataReturnType.Json); }); } catch (ex) { return this.handleJavaScriptException(ex); } }; Ajax.prototype.put = function (url, data, init) { var _this = this; if (init === void 0) { init = {}; } try { var body = JSON.stringify(data); return this.fetchJson(url, Object.assign({ method: "PUT", body: body })) .then(function (response) { return _this.getAjaxResultForType(response, DataReturnType.IAjaxResult); }); } catch (ex) { return this.handleJavaScriptException(ex); } }; Ajax.prototype.post = function (url, data, init) { var _this = this; if (init === void 0) { init = {}; } try { var body = JSON.stringify(data); return this.fetchJson(url, Object.assign({ method: "POST", body: body }, init)) .then(function (response) { return _this.getAjaxResultForType(response, DataReturnType.IAjaxResult); }); } catch (ex) { return this.handleJavaScriptException(ex); } }; Ajax.prototype.delete = function (url, init) { var _this = this; if (init === void 0) { init = {}; } try { return this.fetchJson(url, Object.assign({ method: "DELETE" }, init)) .then(function (response) { return _this.getAjaxResultForType(response, DataReturnType.IAjaxResult); }); } catch (ex) { return this.handleJavaScriptException(ex); } }; Ajax.prototype.getAjaxResponse = function (response) { var result; if (!response) { result = new ErrorStatus("HTTP response is null"); } else if (response instanceof Response) { result = new JsonSuccessStatus(null); result.Response = response; } else { result = response; } return Promise.resolve(result); }; Ajax.prototype.getAjaxResultForType = function (response, dataReturnType) { var _this = this; if (response.Success) { switch (dataReturnType) { case DataReturnType.Json: return response.Response.json() .then(function (data) { response.Data = data; return response; }) .catch(function (jsonError) { return _this.handleJavaScriptException(jsonError); }); case DataReturnType.Text: return response.Response.text() .then(function (data) { response.Data = data; return response; }); case DataReturnType.Number: return response.Response.text() .then(function (data) { response.Data = _this.numberService.fromString(data); return response; }); case DataReturnType.IAjaxResult: return Promise.resolve(response.Data); case DataReturnType.Void: default: return Promise.resolve(response); } } else { return Promise.resolve(response); } }; Ajax.prototype.handleJavaScriptException = function (ex) { var errorStatus = new JsonErrorStatus(ex.message); if (!this.errorHandler) { return Promise.resolve(errorStatus); } else { return this.errorHandler.HandleError(this.i18n.tr('requestFailed') + ": " + ex.message, this.i18n.tr('applicationError') + ": " + ex) .then(function () { return errorStatus; }); } }; Ajax.prototype.handleTypeError = function (ex) { var errorStatus = new ErrorStatus(ex.toString()); if (!this.errorHandler) { return Promise.resolve(errorStatus); } else { return this.errorHandler.HandleError(this.i18n.tr('requestFailed') + ": " + errorStatus.ErrorMessage) .then(function () { return errorStatus; }); } }; Ajax.prototype.handleServerException = function (ex) { var errorStatus = new ErrorStatus(ex.status); if (!this.errorHandler) { return Promise.resolve(errorStatus); } else { return this.errorHandler.HandleError(this.i18n.tr('requestFailed') + ": " + ex.message, this.i18n.tr('applicationException') + ": " + ex) .then(function () { return errorStatus; }); } }; Ajax.prototype.handleDOMException = function (ex) { var errorStatus = new ErrorStatus("Code " + ex.code + ": Type: " + ex.name); if (!this.errorHandler) { return Promise.resolve(errorStatus); } else { return this.errorHandler.HandleError(this.i18n.tr('requestFailed') + ": " + errorStatus.ErrorMessage, this.i18n.tr('applicationException') + ": " + ex) .then(function () { return errorStatus; }); } }; Ajax.prototype.handleServerError = function (error) { var errorStatus = new ErrorStatus(error.Message); if (!this.errorHandler) { return Promise.resolve(errorStatus); } else { return this.errorHandler.HandleError("" + error.Message, this.i18n.tr('applicationError') + ": " + error.Code + " " + error.Message + "\n" + error.StackTrace) .then(function () { return errorStatus; }); } }; Ajax.GlobalBaseUrl = "/api/"; Ajax.GlobalRequestInit = {}; Ajax = Ajax_1 = __decorate([ autoinject, __metadata("design:paramtypes", [HttpClient, IErrorHandler, LoggingService, NumberService, I18N]) ], Ajax); return Ajax; var Ajax_1; }()); export { Ajax }; var ErrorStatus = (function () { function ErrorStatus(message) { this.Success = false; this.ErrorMessage = message; } return ErrorStatus; }()); var JsonSuccessStatus = (function () { function JsonSuccessStatus(data) { this.Success = true; this.Data = data; } return JsonSuccessStatus; }()); var JsonErrorStatus = (function () { function JsonErrorStatus(message) { this.Success = false; this.ErrorMessage = message; } return JsonErrorStatus; }()); var JsonError = (function () { function JsonError() { } return JsonError; }()); var DataReturnType; (function (DataReturnType) { DataReturnType[DataReturnType["Json"] = 0] = "Json"; DataReturnType[DataReturnType["Number"] = 1] = "Number"; DataReturnType[DataReturnType["Text"] = 2] = "Text"; DataReturnType[DataReturnType["IAjaxResult"] = 3] = "IAjaxResult"; DataReturnType[DataReturnType["Void"] = 4] = "Void"; })(DataReturnType || (DataReturnType = {})); //# sourceMappingURL=ajax.js.map