Skip to content

Commit

Permalink
Allow middleware to be defined for each route.
Browse files Browse the repository at this point in the history
  • Loading branch information
roncli committed Sep 1, 2021
1 parent 1dac54a commit 9407316
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
15 changes: 9 additions & 6 deletions router.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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", "") || "/";

Expand All @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 16 additions & 2 deletions routerBase.d.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>>[]
}

export type Route = (BaseRoute & WebsocketRoute) | (BaseRoute & WebRoute)
}

declare class RouterBase {
Expand Down
3 changes: 2 additions & 1 deletion routerBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class RouterBase {
methods: [],
notFound: false,
methodNotAllowed: false,
serverError: false
serverError: false,
middleware: []
};
}
}
Expand Down

0 comments on commit 9407316

Please sign in to comment.