mirror of
https://github.com/cupcakearmy/koa-router.git
synced 2024-12-22 16:16:29 +00:00
Minor Fixes
Adaption for params Prefix validation changes
This commit is contained in:
parent
21119ffa4f
commit
f6dea98097
13
Router.js
13
Router.js
@ -11,7 +11,7 @@ const
|
|||||||
METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'ALL'],
|
METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'ALL'],
|
||||||
|
|
||||||
// Default Response
|
// Default Response
|
||||||
defaultResponse = () => {}
|
defaultResponse = [() => {}, {}]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defaults an options object
|
* Defaults an options object
|
||||||
@ -39,13 +39,13 @@ function pathToReg(path, options) {
|
|||||||
return path
|
return path
|
||||||
|
|
||||||
assert(typeof path === 'string', 'Path must be a String or RegExp')
|
assert(typeof path === 'string', 'Path must be a String or RegExp')
|
||||||
|
|
||||||
options = defaultOptions(options)
|
options = defaultOptions(options)
|
||||||
path = options.prefix + path
|
|
||||||
|
|
||||||
// Test Path & Prefix
|
// Test Path & Prefix
|
||||||
assert(reg_url.test(path), 'Invalid Path')
|
assert(reg_url.test(path) || path === '/', 'Invalid Path')
|
||||||
assert(options.prefix === '' || reg_prefix.test(options.prefix), 'Invalid Prefix')
|
assert(reg_prefix.test(options.prefix) || options.prefix === '', 'Invalid Prefix')
|
||||||
|
|
||||||
|
path = options.prefix + path
|
||||||
|
|
||||||
let ret = '^'
|
let ret = '^'
|
||||||
for (const seg of path.split('/').slice(1))
|
for (const seg of path.split('/').slice(1))
|
||||||
@ -138,13 +138,11 @@ function chooseFn(routes, path, method) {
|
|||||||
|
|
||||||
// Choose the route
|
// Choose the route
|
||||||
let route = null
|
let route = null
|
||||||
|
|
||||||
if (candidates.size === 1)
|
if (candidates.size === 1)
|
||||||
route = candidates.entries().next().value[1]
|
route = candidates.entries().next().value[1]
|
||||||
else if (candidates.size > 1)
|
else if (candidates.size > 1)
|
||||||
// TODO route chooser
|
// TODO route chooser
|
||||||
route = candidates.entries().next().value[1]
|
route = candidates.entries().next().value[1]
|
||||||
|
|
||||||
if (route === null)
|
if (route === null)
|
||||||
return defaultResponse
|
return defaultResponse
|
||||||
|
|
||||||
@ -159,6 +157,7 @@ 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]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user