mirror of
https://github.com/cupcakearmy/koa-router.git
synced 2024-12-22 08:06:27 +00:00
Async support
Now awaits function
This commit is contained in:
parent
f134a5814e
commit
79f7daf0b5
13
Router.js
13
Router.js
@ -11,7 +11,9 @@ const
|
||||
METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'ALL'],
|
||||
|
||||
// Default Response
|
||||
defaultResponse = [() => {}, {}]
|
||||
defaultResponse = [async(c, n) => {
|
||||
await n()
|
||||
}, {}]
|
||||
|
||||
/**
|
||||
* Defaults an options object
|
||||
@ -181,7 +183,7 @@ module.exports = function (options, builder) {
|
||||
// Build the routes
|
||||
const routes = mkRouter(options, builder)
|
||||
|
||||
return function (c, n) {
|
||||
return async function (c, n) {
|
||||
// For building nested routes
|
||||
if (typeof c === 'string') {
|
||||
options.prefix = c + options.prefix
|
||||
@ -190,9 +192,8 @@ module.exports = function (options, builder) {
|
||||
|
||||
const fn = chooseFn(routes, c.request.url, c.request.method)
|
||||
c.request.params = fn[1]
|
||||
if (typeof fn[0] === 'function')
|
||||
fn[0](c, n)
|
||||
else
|
||||
defaultResponse[0]()
|
||||
if (typeof fn[0] !== 'function')
|
||||
fn[0] = defaultResponse[0]
|
||||
await fn[0](c, n)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user