UNPKG

@intuitionrobotics/thunderstorm

Version:
89 lines 3.39 kB
/* * Thunderstorm is a full web app framework! * * Typescript & Express backend infrastructure that natively runs on firebase function * Typescript & React frontend infrastructure * * Copyright (C) 2020 Intuition Robotics * * 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. */ // noinspection TypeScriptPreferShortImport import { HttpMethod } from "./types.js"; import { addItemToArray, Module, removeItemFromArray, } from "@intuitionrobotics/ts-common"; // noinspection TypeScriptPreferShortImport import {} from "./request-types.js"; // noinspection TypeScriptPreferShortImport import { BaseHttpRequest } from "./BaseHttpRequest.js"; export class BaseHttpModule_Class extends Module { defaultErrorHandlers = []; defaultSuccessHandlers = []; origin; timeout = 10000; defaultResponseHandler = []; defaultHeaders = {}; constructor(name) { super(name); this.setDefaultConfig({ compress: true }); } init() { this.timeout = this.config.timeout || this.timeout; } shouldCompress() { return this.config.compress; } addDefaultHeader(key, header) { this.defaultHeaders[key.toLowerCase()] = header; } processDefaultResponseHandlers = (httpRequest) => { let resolved = false; for (const responseHandler of this.defaultResponseHandler) { resolved = resolved || responseHandler(httpRequest); } return resolved; }; addDefaultResponseHandler(defaultResponseHandler) { addItemToArray(this.defaultResponseHandler, defaultResponseHandler); } removeDefaultResponseHandler(defaultResponseHandler) { removeItemFromArray(this.defaultResponseHandler, defaultResponseHandler); } setErrorHandlers(defaultErrorHandlers) { this.defaultErrorHandlers = defaultErrorHandlers; } setSuccessHandlers(defaultErrorHandlers) { this.defaultSuccessHandlers = defaultErrorHandlers; } handleRequestFailure = (request, resError) => { const feError = request.getErrorMessage(); const beError = resError?.debugMessage; this.logError(`Http request for key '${request.key}' failed...`); if (feError) this.logError(` + FE error: ${feError}`); if (beError) this.logError(` + BE error: ${beError}`); for (const errorHandler of this.defaultErrorHandlers) { errorHandler(request, resError); } }; handleRequestSuccess = (request) => { const feMessage = request.getSuccessMessage(); this.logInfo(`Http request for key '${request.key}' completed`); if (feMessage) this.logInfo(` + FE message: ${feMessage}`); for (const successHandler of this.defaultSuccessHandlers) { successHandler(request); } }; } //# sourceMappingURL=BaseHttpModule.js.map