UNPKG

@intuitionrobotics/thunderstorm

Version:
89 lines 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Storm = void 0; const ts_common_1 = require("@intuitionrobotics/ts-common"); const backend_functions_1 = require("@intuitionrobotics/firebase/backend-functions"); const HttpServer_1 = require("../modules/server/HttpServer"); const server_api_1 = require("../modules/server/server-api"); const BaseStorm_1 = require("./BaseStorm"); const express = require("express"); const server_errors_1 = require("../modules/server/server-errors"); const backend_1 = require("@intuitionrobotics/firebase/backend"); class Storm extends BaseStorm_1.BaseStorm { constructor() { super(); this.functions = []; this.apis = []; this.logClient = ts_common_1.LogClient_Function; this.errorMessageComposer = (0, server_errors_1.DefaultApiErrorMessageComposer)(); this.addModules(backend_1.FirebaseModule); } setErrorMessageComposer(errorMessageComposer) { this.errorMessageComposer = errorMessageComposer; return this; } setApp(app) { this.express = app; return this; } setLogClient(logClient) { this.logClient = logClient; return this; } getLogClient() { return this.logClient; } setOnDestroy(onDestroy) { this.onDestroy = onDestroy; return this; } setOnStart(onStart) { this.onStart = onStart; return this; } static getInstance() { return Storm.instance; } init() { var _a; ts_common_1.BeLogged.addClient(process.env.GCLOUD_PROJECT && process.env.FUNCTIONS_EMULATOR ? ts_common_1.LogClient_Terminal : this.logClient); server_api_1.ServerApi.isDebug = !!((_a = this.config) === null || _a === void 0 ? void 0 : _a.isDebug); super.init(); return this; } registerApis(...apis) { this.apis = apis; return this; } setInitialRouteResolver(routeResolver) { this.routeResolver = routeResolver; return this; } setInitialRoutePath(initialPath) { this.initialPath = initialPath; return this; } build(onStarted) { var _a; const app = this.express || express(); const httpServer = new HttpServer_1.HttpServer_Class(app, (_a = this.config) === null || _a === void 0 ? void 0 : _a["HttpServer"]); const modulesAsFunction = this.modules.filter((module) => module instanceof backend_functions_1.FirebaseFunction); this.functions = [new backend_functions_1.Firebase_ExpressFunction(app), ...modulesAsFunction]; this.onStart && this.functions.forEach(func => func.addOnStart(this.onStart)); this.onDestroy && this.functions.forEach(func => func.addOnDestroy(this.onDestroy)); this.init(); httpServer.init(); const urlPrefix = !process.env.GCLOUD_PROJECT ? this.initialPath : ""; if (this.routeResolver) { httpServer.resolveApi(this.routeResolver, urlPrefix, this.apis); httpServer.printRoutes(process.env.GCLOUD_PROJECT ? this.initialPath : ""); } onStarted && onStarted().catch(e => this.logError(e)); return this.functions.reduce((toRet, _function) => { toRet[_function.getName()] = _function.getFunction(); return toRet; }, {}); } } exports.Storm = Storm; //# sourceMappingURL=Storm.js.map