mirror of
https://github.com/cupcakearmy/koa-router.git
synced 2024-12-22 16:16:29 +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'],
|
METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'ALL'],
|
||||||
|
|
||||||
// Default Response
|
// Default Response
|
||||||
defaultResponse = [() => {}, {}]
|
defaultResponse = [async(c, n) => {
|
||||||
|
await n()
|
||||||
|
}, {}]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defaults an options object
|
* Defaults an options object
|
||||||
@ -181,7 +183,7 @@ module.exports = function (options, builder) {
|
|||||||
// Build the routes
|
// Build the routes
|
||||||
const routes = mkRouter(options, builder)
|
const routes = mkRouter(options, builder)
|
||||||
|
|
||||||
return function (c, n) {
|
return async function (c, n) {
|
||||||
// For building nested routes
|
// For building nested routes
|
||||||
if (typeof c === 'string') {
|
if (typeof c === 'string') {
|
||||||
options.prefix = c + options.prefix
|
options.prefix = c + options.prefix
|
||||||
@ -190,9 +192,8 @@ module.exports = function (options, builder) {
|
|||||||
|
|
||||||
const fn = chooseFn(routes, c.request.url, c.request.method)
|
const fn = chooseFn(routes, c.request.url, c.request.method)
|
||||||
c.request.params = fn[1]
|
c.request.params = fn[1]
|
||||||
if (typeof fn[0] === 'function')
|
if (typeof fn[0] !== 'function')
|
||||||
fn[0](c, n)
|
fn[0] = defaultResponse[0]
|
||||||
else
|
await fn[0](c, n)
|
||||||
defaultResponse[0]()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user