Minor Fixes

Adaption for params
Prefix validation changes
This commit is contained in:
nicco 2017-08-20 23:48:38 +02:00
parent 21119ffa4f
commit f6dea98097

View File

@ -11,7 +11,7 @@ const
METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'ALL'],
// Default Response
defaultResponse = () => {}
defaultResponse = [() => {}, {}]
/**
* Defaults an options object
@ -39,13 +39,13 @@ function pathToReg(path, options) {
return path
assert(typeof path === 'string', 'Path must be a String or RegExp')
options = defaultOptions(options)
path = options.prefix + path
// Test Path & Prefix
assert(reg_url.test(path), 'Invalid Path')
assert(options.prefix === '' || reg_prefix.test(options.prefix), 'Invalid Prefix')
assert(reg_url.test(path) || path === '/', 'Invalid Path')
assert(reg_prefix.test(options.prefix) || options.prefix === '', 'Invalid Prefix')
path = options.prefix + path
let ret = '^'
for (const seg of path.split('/').slice(1))
@ -138,13 +138,11 @@ function chooseFn(routes, path, method) {
// Choose the route
let route = null
if (candidates.size === 1)
route = candidates.entries().next().value[1]
else if (candidates.size > 1)
// TODO route chooser
route = candidates.entries().next().value[1]
if (route === null)
return defaultResponse
@ -159,6 +157,7 @@ function chooseFn(routes, path, method) {
for (const key of fn.params.keys())
paramObj[key] = pathArr[fn.params.get(key)]
return [fn.fn, paramObj]
}