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.

65 lines (64 loc) 2.43 kB
import { NextFunction, Request, Response } from "express"; import log4js from "log4js"; /** * Function signature for RESTful API handlers */ export type RestfulFunction = (req: Request) => any; /** * Function signature for control handlers */ export type ControlFunction = (req: Request, res: Response) => any; /** * Common router helper class providing middleware and utilities for Express routing */ export default class CommonRouterHelper { /** Logger instance for this router helper */ protected readonly logger: log4js.Logger; /** * Sets HTTP response header to JSON format * @param req Express request object * @param res Express response object * @param next Express next function */ setJsonHeader(req: Request, res: Response, next: NextFunction): void; /** * Sets response headers to disable caching * @param req Express request object * @param res Express response object * @param next Express next function */ setNoCache(req: Request, res: Response, next: NextFunction): void; /** * 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: RestfulFunction): any; /** * Invokes an asynchronous controller function with error handling * @param func The controller function to execute * @returns Express middleware function */ invokeController(func: ControlFunction): (req: Request, res: Response) => Promise<void>; /** * Handles invalid request paths by throwing ActionNotFoundError * @returns Express middleware function for handling 404 errors */ actionNotFound(): (req: Request, res: Response, next: NextFunction) => void; /** * Retrieves user information from request headers * @param req Express request object * @protected */ protected retrieveUserFormHeader(req: Request): void; /** * Middleware to retrieve user information from headers * @returns Express middleware function */ retrieveUser(): (req: Request, res: Response, next: any) => void; /** * Middleware to check if user is authenticated * @returns Express middleware function that validates user authentication */ checkLoggedUser(): (req: Request, res: Response, next: any) => void; }