Global middleware
This commit is contained in:
19
README.md
19
README.md
@@ -27,9 +27,17 @@ const router = new Router()
|
||||
// Enabling buildin CORS support
|
||||
router.cors()
|
||||
|
||||
// Register global middleware
|
||||
router.use((req, res, next) => {
|
||||
res.headers = {
|
||||
...res.headers,
|
||||
'X-Global-Middlewares': 'true'
|
||||
}
|
||||
next()
|
||||
})
|
||||
|
||||
// Simple get
|
||||
router.get('/user', (req, res) => {
|
||||
|
||||
res.body = {
|
||||
data: {
|
||||
id: 1,
|
||||
@@ -89,6 +97,15 @@ Enable or disable debug mode. Which will return the `error.stack` in case of an
|
||||
State is a `boolean` which determines if debug mode should be enabled or not (default: `true`)
|
||||
|
||||
|
||||
### `router.use(handler)
|
||||
|
||||
Register a global middleware handler.
|
||||
|
||||
#### `handler` (function)
|
||||
|
||||
Handler is a `function` which will be called for every request.
|
||||
|
||||
|
||||
### `router.cors([config])`
|
||||
|
||||
If enabled will overwrite other `OPTIONS` requests.
|
||||
|
||||
16
index.d.ts
vendored
16
index.d.ts
vendored
@@ -14,6 +14,13 @@ declare class Router {
|
||||
* @type {Route[]}
|
||||
*/
|
||||
protected routes: Route[]
|
||||
/**
|
||||
* Global Handlers
|
||||
*
|
||||
* @protected
|
||||
* @type {Handler[]}
|
||||
*/
|
||||
protected globalHandlers: Handler[]
|
||||
/**
|
||||
* Debug Mode
|
||||
*
|
||||
@@ -70,6 +77,13 @@ declare class Router {
|
||||
* @param {Response} response
|
||||
* @param {next} next
|
||||
*/
|
||||
/**
|
||||
* Register global handler
|
||||
*
|
||||
* @param {RouterHandler} handler
|
||||
* @param handlers
|
||||
*/
|
||||
use(handler: RouterHandler): Router
|
||||
/**
|
||||
* Register CONNECT route
|
||||
*
|
||||
@@ -303,7 +317,7 @@ type RouterResponse = {
|
||||
/**
|
||||
* Object you can set response headers in
|
||||
*/
|
||||
headers: Headers
|
||||
headers: Object<string, string>
|
||||
/**
|
||||
* Return status code (default: `204`)
|
||||
*/
|
||||
|
||||
20
index.js
20
index.js
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user