mirror of
https://github.com/cupcakearmy/koa-router.git
synced 2024-12-22 16:16:29 +00:00
Fixed Parameter Offset
Fixed Issue with parameters being offset when nesting
This commit is contained in:
parent
0eb643f5da
commit
5037467aa5
17
Router.js
17
Router.js
@ -62,12 +62,15 @@ function pathToReg(path, options) {
|
|||||||
* Gets the position of each parameter type and returns a map with it
|
* Gets the position of each parameter type and returns a map with it
|
||||||
* @param {*} path
|
* @param {*} path
|
||||||
*/
|
*/
|
||||||
function pathToParams(path) {
|
function pathToParams(path, options) {
|
||||||
let params = new Map()
|
const
|
||||||
|
offset = options.prefix.split('/').slice(1).length,
|
||||||
|
params = new Map()
|
||||||
|
|
||||||
let i = 0
|
let i = 0
|
||||||
for (const seg of path.split('/').slice(1)) {
|
for (const seg of path.split('/').slice(1)) {
|
||||||
if (seg[0] === ':')
|
if (seg[0] === ':')
|
||||||
params.set(seg.slice(1), i)
|
params.set(seg.slice(1), i + offset)
|
||||||
++i
|
++i
|
||||||
}
|
}
|
||||||
return params
|
return params
|
||||||
@ -83,7 +86,7 @@ function mkRouter(options, builder) {
|
|||||||
let routes = new Map()
|
let routes = new Map()
|
||||||
let routesKeys = new Map()
|
let routesKeys = new Map()
|
||||||
|
|
||||||
// This object 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
|
||||||
@ -98,7 +101,7 @@ function mkRouter(options, builder) {
|
|||||||
key = pathToReg(arguments[0], options),
|
key = pathToReg(arguments[0], options),
|
||||||
data = {
|
data = {
|
||||||
fn: arguments[1],
|
fn: arguments[1],
|
||||||
params: pathToParams(arguments[0])
|
params: pathToParams(arguments[0], options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the same regex already exits, grab the object
|
// If the same regex already exits, grab the object
|
||||||
@ -157,7 +160,6 @@ function chooseFn(routes, path, method) {
|
|||||||
for (const key of fn.params.keys())
|
for (const key of fn.params.keys())
|
||||||
paramObj[key] = pathArr[fn.params.get(key)]
|
paramObj[key] = pathArr[fn.params.get(key)]
|
||||||
|
|
||||||
|
|
||||||
return [fn.fn, paramObj]
|
return [fn.fn, paramObj]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,6 +190,9 @@ 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')
|
||||||
fn[0](c, n)
|
fn[0](c, n)
|
||||||
|
else
|
||||||
|
defaultResponse[0]()
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user