mirror of
https://github.com/cupcakearmy/koa-router.git
synced 2024-12-22 16:16:29 +00:00
Async Fix
This commit is contained in:
parent
b3eae74ade
commit
8b794323fe
24
Router.js
24
Router.js
@ -85,28 +85,30 @@ function pathToParams(path, options) {
|
|||||||
*/
|
*/
|
||||||
function mkRouter(options, builder) {
|
function mkRouter(options, builder) {
|
||||||
|
|
||||||
let routes = new Map()
|
const
|
||||||
let routesKeys = new Map()
|
routes = new Map(),
|
||||||
|
routesKeys = new Map()
|
||||||
|
|
||||||
// This object (routesMaker) will process the builder function
|
// This object (routesMaker) will process the builder function
|
||||||
let routesMaker = {
|
let routesMaker = {
|
||||||
nest: function () {
|
nest: function () {
|
||||||
// Join the lower paths with the current ones
|
// Join the lower paths with the current ones
|
||||||
routes = new Map([...routes, ...arguments[0](options.prefix)])
|
const nested = arguments[0](options.prefix)
|
||||||
|
for (const key of nested.keys())
|
||||||
|
routes.set(key, nested.get(key))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the other methods to the routesMaker object
|
// Add the other methods to the routesMaker object
|
||||||
for (const method of METHODS)
|
for (const method of METHODS)
|
||||||
routesMaker[method.toLowerCase()] = function () {
|
routesMaker[method.toLowerCase()] = function () {
|
||||||
let
|
const data = {
|
||||||
key = pathToReg(arguments[0], options),
|
fn: arguments[1],
|
||||||
data = {
|
params: pathToParams(arguments[0], options)
|
||||||
fn: arguments[1],
|
}
|
||||||
params: pathToParams(arguments[0], options)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the same regex already exits, grab the object
|
// If the same regex already exits, grab the object
|
||||||
|
let key = pathToReg(arguments[0], options)
|
||||||
if (routesKeys.has(key.source))
|
if (routesKeys.has(key.source))
|
||||||
key = routesKeys.get(key.source)
|
key = routesKeys.get(key.source)
|
||||||
else
|
else
|
||||||
@ -183,7 +185,7 @@ module.exports = function (options, builder) {
|
|||||||
// Build the routes
|
// Build the routes
|
||||||
const routes = mkRouter(options, builder)
|
const routes = mkRouter(options, builder)
|
||||||
|
|
||||||
return async function (c, n) {
|
return 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
|
||||||
@ -194,6 +196,6 @@ module.exports = function (options, builder) {
|
|||||||
c.request.params = fn[1]
|
c.request.params = fn[1]
|
||||||
if (typeof fn[0] !== 'function')
|
if (typeof fn[0] !== 'function')
|
||||||
fn[0] = defaultResponse[0]
|
fn[0] = defaultResponse[0]
|
||||||
await fn[0](c, n)
|
return fn[0](c, n)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user