1
0

Create branch

This commit is contained in:
2022-06-24 16:15:50 +02:00
parent ae0c933868
commit 2ebe97fe57
3 changed files with 67 additions and 21 deletions

View File

@@ -47,6 +47,16 @@ class Router {
* @property {RouterHandler[]} handlers Array of handler functions
*/
/**
* Router Context
*
* @typedef RouterContext
* @property {Object<string, string>} env Environment
* @property {RouterRequest} req Request Object
* @property {RouterResponse} res Response Object
* @property {RouterNext} next Next Handler
*/
/**
* Request Object
*
@@ -80,9 +90,7 @@ class Router {
* Handler Function
*
* @callback RouterHandler
* @param {RouterRequest} request
* @param {RouterResponse} response
* @param {RouterNext} next
* @param {RouterContext} ctx
*/
/**
@@ -308,11 +316,12 @@ class Router {
/**
* Handle requests
*
* @param {any} env
* @param {Request} request
* @param {any=} extend
* @returns {Response}
*/
async handle(request, extend = {}) {
async handle(env, request, extend = {}) {
try {
if (request instanceof Event) {
request = request.request
@@ -373,7 +382,7 @@ class Router {
throw new Error('next() called multiple times')
prevIndex = index
if (typeof handlers[index] === 'function')
await handlers[index](req, res, async () => await runner(index + 1))
await handlers[index]({ env, req, res, next: async () => await runner(index + 1) })
}
await runner(0)
if (typeof res.body === 'object') {