UNPKG

ferngully-aurelia-tools

Version:

Ferngully Tools for Aurelia

311 lines 11.3 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'; let Ajax = Ajax_1 = class Ajax { constructor(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; } configure() { if (this._initialized) { return; } this.http.configure((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; } fetch(url, init) { 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((response) => { if (response instanceof DOMException) { return this.handleDOMException(response); } else if (response.json) { return response.json() .then((errorInfo) => { if (errorInfo && errorInfo.Code) { return this.handleServerError(errorInfo); } else { return this.handleServerException(response); } }); } else { return this.handleTypeError(response); } }) .then((response) => { if (response instanceof Response) { const realResponse = response; if (!realResponse.ok) { return realResponse.json() .catch(() => { return this.handleServerException(realResponse); }) .then((errorInfo) => { return this.handleServerError(errorInfo); }); } } return this.getAjaxResponse(response); }); } fetchNumber(url, init = {}) { try { return this.fetch(url, init) .then((response) => { return this.getAjaxResultForType(response, DataReturnType.Number); }); } catch (ex) { return this.handleJavaScriptException(ex); } } fetchText(url, init = {}) { try { return this.fetch(url, init) .then((response) => { return this.getAjaxResultForType(response, DataReturnType.Text); }); } catch (ex) { return this.handleJavaScriptException(ex); } } fetchJson(url, init = {}) { try { return this.fetch(url, init) .then((response) => { return this.getAjaxResultForType(response, DataReturnType.Json); }); } catch (ex) { return this.handleJavaScriptException(ex); } } put(url, data, init = {}) { try { let body = JSON.stringify(data); return this.fetchJson(url, Object.assign({ method: "PUT", body: body })) .then((response) => { return this.getAjaxResultForType(response, DataReturnType.IAjaxResult); }); } catch (ex) { return this.handleJavaScriptException(ex); } } post(url, data, init = {}) { try { let body = JSON.stringify(data); return this.fetchJson(url, Object.assign({ method: "POST", body: body }, init)) .then((response) => { return this.getAjaxResultForType(response, DataReturnType.IAjaxResult); }); } catch (ex) { return this.handleJavaScriptException(ex); } } delete(url, init = {}) { try { return this.fetchJson(url, Object.assign({ method: "DELETE" }, init)) .then((response) => { return this.getAjaxResultForType(response, DataReturnType.IAjaxResult); }); } catch (ex) { return this.handleJavaScriptException(ex); } } getAjaxResponse(response) { let 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); } getAjaxResultForType(response, dataReturnType) { if (response.Success) { switch (dataReturnType) { case DataReturnType.Json: return response.Response.json() .then((data) => { response.Data = data; return response; }) .catch((jsonError) => { return this.handleJavaScriptException(jsonError); }); case DataReturnType.Text: return response.Response.text() .then((data) => { response.Data = data; return response; }); case DataReturnType.Number: return response.Response.text() .then((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); } } handleJavaScriptException(ex) { const 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(() => { return errorStatus; }); } } handleTypeError(ex) { const errorStatus = new ErrorStatus(ex.toString()); if (!this.errorHandler) { return Promise.resolve(errorStatus); } else { return this.errorHandler.HandleError(`${this.i18n.tr('requestFailed')}: ${errorStatus.ErrorMessage}`) .then(() => { return errorStatus; }); } } handleServerException(ex) { const 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(() => { return errorStatus; }); } } handleDOMException(ex) { const 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(() => { return errorStatus; }); } } handleServerError(error) { const 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(() => { return errorStatus; }); } } }; Ajax.GlobalBaseUrl = "/api/"; Ajax.GlobalRequestInit = {}; Ajax = Ajax_1 = __decorate([ autoinject, __metadata("design:paramtypes", [HttpClient, IErrorHandler, LoggingService, NumberService, I18N]) ], Ajax); export { Ajax }; class ErrorStatus { constructor(message) { this.Success = false; this.ErrorMessage = message; } } class JsonSuccessStatus { constructor(data) { this.Success = true; this.Data = data; } } class JsonErrorStatus { constructor(message) { this.Success = false; this.ErrorMessage = message; } } class 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 = {})); var Ajax_1; //# sourceMappingURL=ajax.js.map