From 9407316e2287e99596cca7e3b8ca2fef601edcd3 Mon Sep 17 00:00:00 2001 From: "Ronald M. Clifford" Date: Tue, 31 Aug 2021 18:02:16 -0700 Subject: [PATCH] Allow middleware to be defined for each route. --- router.js | 15 +++++++++------ routerBase.d.ts | 18 ++++++++++++++++-- routerBase.js | 3 ++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/router.js b/router.js index 5d29f8c..f1cbee5 100644 --- a/router.js +++ b/router.js @@ -1,11 +1,14 @@ /** * @typedef {["error", function({message: string, err: Error, req: Express.Request}): void]} Events - * @typedef {import("http-errors").HttpError} HttpError * @typedef {import("express").NextFunction} Express.NextFunction * @typedef {import("express").Request} Express.Request * @typedef {import("express").Response} Express.Response * @typedef {import("express").Router} Express.Router + * @typedef {import("http-errors").HttpError} HttpErrors.HttpError + * @typedef {import("./routerBase").BaseRoute} RouterBase.BaseRoute * @typedef {import("./routerBase").Route} RouterBase.Route + * @typedef {import("./routerBase").WebRoute} RouterBase.WebRoute + * @typedef {import("./routerBase").WebsocketRoute} RouterBase.WebsocketRoute */ const EventEmitter = require("events").EventEmitter, @@ -161,9 +164,9 @@ class Router extends EventEmitter { // Setup websocket routes. webSockets.forEach((filename) => { - const route = routes[filename]; + const route = /** @type {RouterBase.BaseRoute & RouterBase.WebsocketRoute} */(routes[filename]); // eslint-disable-line no-extra-parens - router.ws(route.path, (ws, req) => { + router.ws(route.path, ...route.middleware, (ws, req) => { // @ts-ignore ws._url = req.url.replace("/.websocket", "").replace(".websocket", "") || "/"; @@ -177,10 +180,10 @@ class Router extends EventEmitter { // Setup page routes. pages.forEach((filename) => { - const route = routes[filename]; + const route = /** @type {RouterBase.BaseRoute & RouterBase.WebRoute} */(routes[filename]); // eslint-disable-line no-extra-parens route.methods.forEach((method) => { - router[method](route.path, async (/** @type {Express.Request} */ req, /** @type {Express.Response} */ res, /** @type {function} */ next) => { + router[method](route.path, ...route.middleware, async (/** @type {Express.Request} */ req, /** @type {Express.Response} */ res, /** @type {function} */ next) => { try { if (options.hot) { for (const include of includes) { @@ -256,7 +259,7 @@ class Router extends EventEmitter { // ## # # ## # /** * Handles a router error. - * @param {HttpError} err The error object. + * @param {HttpErrors.HttpError} err The error object. * @param {Express.Request} req The request. * @param {Express.Response} res The response. * @param {Express.NextFunction} next The function to be called if the error is not handled. diff --git a/routerBase.d.ts b/routerBase.d.ts index d8df400..ccce330 100644 --- a/routerBase.d.ts +++ b/routerBase.d.ts @@ -1,17 +1,31 @@ +import { RequestHandler } from "express" +import { WebsocketRequestHandler } from "express-ws" + declare module RouterBase { - export type Route = { + interface BaseRoute { file: string lastModified: Date path?: string class?: any include: boolean - webSocket: boolean events: string[] methods: string[] notFound: boolean methodNotAllowed: boolean serverError: boolean } + + interface WebsocketRoute { + webSocket: true + middleware: WebsocketRequestHandler[] + } + + interface WebRoute { + webSocket: false + middleware: RequestHandler<{[key: string]: string}, any, any, qs.ParsedQs, Record>[] + } + + export type Route = (BaseRoute & WebsocketRoute) | (BaseRoute & WebRoute) } declare class RouterBase { diff --git a/routerBase.js b/routerBase.js index 3b72f87..fcac9ae 100644 --- a/routerBase.js +++ b/routerBase.js @@ -39,7 +39,8 @@ class RouterBase { methods: [], notFound: false, methodNotAllowed: false, - serverError: false + serverError: false, + middleware: [] }; } }