@nu-art/thunder
Version:
Thunder - React & Typescript based frontend framework
120 lines • 5.61 kB
JavaScript
;
/*
* Thunder is a typescript & react frontend foundation that natively
* runs on firebase function and is a part of the Thunderstorm larger project
*
* Copyright (C) 2018 Adam van der Kruk aka TacB0sS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ts_common_1 = require("@nu-art/ts-common");
var http_request_1 = require("./http-request");
var toaster_module_1 = require("../toaster-module");
// noinspection TypeScriptPreferShortImport
var Thunder_1 = require("../../core/Thunder");
exports.ErrorHandler_Toast = function (key, message) { return toaster_module_1.ToastModule.toastError(message); };
exports.SuccessHandler_Toast = function (key, message) { return toaster_module_1.ToastModule.toastSuccess(message); };
var HttpModule_Class = /** @class */ (function (_super) {
__extends(HttpModule_Class, _super);
function HttpModule_Class() {
var _this = _super.call(this) || this;
_this.dispatch_onRequestCompleted = new Thunder_1.UIDispatcher("onRequestCompleted");
_this.errorHandlers = [exports.ErrorHandler_Toast];
_this.successHandlers = [exports.SuccessHandler_Toast];
_this.timeout = 10000;
_this.defaultHeaders = {};
_this.createRequest = function (method, key, data) {
var defaultHeaders = Object.keys(_this.defaultHeaders).reduce(function (toRet, _key) {
toRet[_key] = _this.defaultHeaders[_key]();
return toRet;
}, {});
return new http_request_1.HttpRequest(key, data)
.setOrigin(_this.origin)
.setMethod(method)
.setTimeout(_this.timeout)
.addHeaders(defaultHeaders);
};
_this.processDefaultResponseHandlers = function (httpRequest) {
return _this.responseHandler && _this.responseHandler(httpRequest);
};
_this.handleRequestFailure = function (resError, xhr, requestKey, errorMessage, requestData) {
var error = errorMessage || "Api call failed";
if (resError)
error += "\nError: " + resError.debugMessage;
_this.logError("request for key(" + requestKey + ") failed: " + error);
if (!errorMessage)
return;
for (var _i = 0, _a = _this.errorHandlers; _i < _a.length; _i++) {
var errorHandler = _a[_i];
errorHandler(requestKey, error);
}
_this.dispatch_onRequestCompleted.dispatch([requestKey,
false,
requestData]);
};
_this.handleRequestSuccess = function (requestKey, successMessage, requestData) {
if (successMessage) {
_this.logInfo("request for key(" + requestKey + ") completed: " + successMessage);
for (var _i = 0, _a = _this.successHandlers; _i < _a.length; _i++) {
var successHandler = _a[_i];
successHandler(requestKey, successMessage);
}
}
else
_this.logInfo("request for key(" + requestKey + ") completed!");
_this.dispatch_onRequestCompleted.dispatch([requestKey,
true,
requestData]);
};
return _this;
}
HttpModule_Class.prototype.addDefaultHeader = function (key, header) {
this.defaultHeaders[key] = header;
};
HttpModule_Class.prototype.init = function () {
this.origin = this.config.origin.replace("${hostname}", window.location.hostname).replace("${protocol}", window.location.protocol);
this.timeout = this.config.timeout || this.timeout;
};
HttpModule_Class.prototype.validate = function () {
if (!this.origin)
throw new Error("MUST specify server origin path, e.g. 'https://localhost:3000'");
};
;
HttpModule_Class.prototype.addResponseHandler = function (responseHandler) {
this.responseHandler = responseHandler;
};
HttpModule_Class.prototype.setErrorHandlers = function (errorHandlers) {
this.errorHandlers = errorHandlers;
};
HttpModule_Class.prototype.setSuccessHandlers = function (successHandlers) {
this.successHandlers = successHandlers;
};
return HttpModule_Class;
}(ts_common_1.Module));
exports.HttpModule_Class = HttpModule_Class;
exports.HttpModule = new HttpModule_Class();
//# sourceMappingURL=http-module.js.map