UNPKG

neura-express-app

Version:

Basic express application starter with some common utilities.

106 lines (105 loc) 4.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NeuraController = exports.NeuraRouter = void 0; const express_1 = __importDefault(require("express")); const app_error_1 = require("../errors/app.error"); const helpers_1 = require("../other/helpers"); const api_error_1 = require("../errors/api.error"); class NeuraRouter { constructor(container) { this.container = container; const logger = container.get("logger"); if (!logger) { throw new app_error_1.NeuraAppError("app-logger-missing", "Application logger not defined!", false); } this.logger = logger; this.router = express_1.default.Router(); } use(...handlers) { this.router.use(handlers); } get(path, cb, bodyToValidate, queryToValidate) { this.logger.info(`[Router]: New route [GET] -> ${this.getFullRoutePath(path)}`); this.router.get(path, async (req, res, next) => { await this.validateAndRespond(req, res, next, cb, bodyToValidate, queryToValidate); }); } post(path, cb, bodyToValidate, queryToValidate) { this.logger.info(`[Router]: New route [POST] -> ${this.getFullRoutePath(path)}`); this.router.post(path, async (req, res, next) => { await this.validateAndRespond(req, res, next, cb, bodyToValidate, queryToValidate); }); } put(path, cb, bodyToValidate, queryToValidate) { this.logger.info(`[Router]: New route [PUT] -> ${this.getFullRoutePath(path)}`); this.router.put(path, async (req, res, next) => { await this.validateAndRespond(req, res, next, cb, bodyToValidate, queryToValidate); }); } delete(path, cb, bodyToValidate, queryToValidate) { this.logger.info(`[Router]: New route [DELETE] -> ${this.getFullRoutePath(path)}`); this.router.delete(path, async (req, res, next) => { await this.validateAndRespond(req, res, next, cb, bodyToValidate, queryToValidate); }); } async validateAndRespond(req, res, next, cb, bodyToValidate, queryToValidate) { try { if (bodyToValidate) { const validationErrors = await (0, helpers_1.validateData)(bodyToValidate, req.body); if (validationErrors instanceof api_error_1.ValidationRequestError) { return next(validationErrors); } } if (queryToValidate) { const validationErrors = await (0, helpers_1.validateData)(queryToValidate, req.query); if (validationErrors instanceof api_error_1.ValidationRequestError) { return next(validationErrors); } } const result = await cb({ body: req.body, query: req.query, headers: req.headers, session: req.session ?? {}, expressRequest: req, expressResponse: res, cookies: req.cookies, }); return res.send(result ?? "Ok"); } catch (error) { return next(error); } } getFullRoutePath(path) { return this.routePrefix ? `${this.routePrefix}${path}` : path; } } exports.NeuraRouter = NeuraRouter; class NeuraController { constructor(container) { this.container = container; this.routesRegistered = false; const logger = container.get("logger"); if (!logger) { throw new app_error_1.NeuraAppError("app-logger-missing", "Application logger not defined!", false); } this.logger = logger; this.router = new NeuraRouter(container); } getRouterPrefix() { return undefined; } getRouter() { if (!this.routesRegistered) { this.router.routePrefix = this.getRouterPrefix(); this.registerRoutes(); this.routesRegistered = true; } return this.router.router; } } exports.NeuraController = NeuraController;