1
0

Global middleware

This commit is contained in:
2022-03-18 19:37:43 +01:00
parent 6e6a771c1a
commit 93d4590528
3 changed files with 51 additions and 4 deletions

View File

@@ -16,6 +16,11 @@ class Router {
*/
this.routes = []
/**
* Global Handlers
*/
this.globalHandlers = []
/**
* Debug Mode
*
@@ -80,6 +85,16 @@ class Router {
* @param {RouterNext} next
*/
/**
* Register global handler
*
* @param {RouterHandler} handler
*/
use(handlers) {
this.globalHandlers.push(handlers)
return this
}
/**
* Register CONNECT route
*
@@ -355,13 +370,14 @@ class Router {
'Access-Control-Max-Age': this.corsConfig.maxAge,
}
}
const handlers = [...this.globalHandlers, ...route.handlers]
let prevIndex = -1
const runner = async index => {
if (index === prevIndex)
throw new Error('next() called multiple times')
prevIndex = index
if (typeof route.handlers[index] === 'function')
await route.handlers[index](req, res, async () => await runner(index + 1))
if (typeof handlers[index] === 'function')
await handlers[index](req, res, async () => await runner(index + 1))
}
await runner(0)
if (typeof res.body === 'object') {