UNPKG

@ticatec/common-express-server

Version:

A comprehensive TypeScript library providing common classes, controllers, and middleware for building scalable Express.js applications with multi-tenant support.

134 lines 4.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const express_exception_1 = require("@ticatec/express-exception"); const log4js_1 = __importDefault(require("log4js")); /** * Common router helper class providing middleware and utilities for Express routing */ class CommonRouterHelper { constructor() { /** Logger instance for this router helper */ this.logger = log4js_1.default.getLogger(this.constructor.name); } /** * Sets HTTP response header to JSON format * @param req Express request object * @param res Express response object * @param next Express next function */ setJsonHeader(req, res, next) { res.header('Content-Type', 'application/json'); next(); } /** * Sets response headers to disable caching * @param req Express request object * @param res Express response object * @param next Express next function */ setNoCache(req, res, next) { res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); res.header('Expires', '-1'); res.header('Pragma', 'no-cache'); next(); } /** * Invokes a RESTful operation and wraps the result in JSON format for the client * @param func The RESTful function to execute * @returns Express middleware function */ invokeRestfulAction(func) { return async (req, res) => { try { let result = await func(req); if (result != null) { res.json(result); } else { res.status(204).send(); } } catch (ex) { (0, express_exception_1.handleError)(ex, req, res); } }; } /** * Invokes an asynchronous controller function with error handling * @param func The controller function to execute * @returns Express middleware function */ invokeController(func) { return async (req, res) => { try { await func(req, res); } catch (ex) { (0, express_exception_1.handleError)(ex, req, res); } }; } /** * Handles invalid request paths by throwing ActionNotFoundError * @returns Express middleware function for handling 404 errors */ actionNotFound() { return (req, res, next) => { (0, express_exception_1.handleError)(new express_exception_1.ActionNotFoundError(), req, res); }; } /** * Retrieves user information from request headers * @param req Express request object * @protected */ retrieveUserFormHeader(req) { let userStr = req.headers['user']; if (userStr != null) { try { const user = JSON.parse(decodeURIComponent(userStr)); let language = req.headers['x-language']; if (language) { if (user.actAs) { user.actAs['language'] = language; } user['language'] = language; } req['user'] = user; } catch (ex) { this.logger.debug('Invalid user header', userStr); } } } /** * Middleware to retrieve user information from headers * @returns Express middleware function */ retrieveUser() { return (req, res, next) => { this.retrieveUserFormHeader(req); next(); }; } /** * Middleware to check if user is authenticated * @returns Express middleware function that validates user authentication */ checkLoggedUser() { return (req, res, next) => { this.retrieveUserFormHeader(req); if (req['user'] == null) { (0, express_exception_1.handleError)(new express_exception_1.UnauthenticatedError(), req, res); } else { next(); } }; } } exports.default = CommonRouterHelper; //# sourceMappingURL=CommonRouterHelper.js.map