micro-switch-router
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "micro-switch-router", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -7,22 +7,36 @@ const matchPath = require('react-router/matchPath').default | ||
'post', | ||
'patch', | ||
'delete', | ||
'any', | ||
'all', | ||
] | ||
/** | ||
* Adds a path to a router | ||
* Adds a pathParam to a router | ||
* @method addPath | ||
* @param {Router} router The router in which to add the path | ||
* @param {String} method The method that will handle this path | ||
* @param {Router} router The router in which to add the pathParam | ||
* @param {String} method The method that will handle this pathParam | ||
* @param {Array} args The mixed params (String|Function|Array) | ||
*/ | ||
const addPath = (router, method, matchProps, handler) => { | ||
const addPath = (router, method, pathParam, handler) => { | ||
if (!matchProps.path) { | ||
throw Error('You must provide matchProps as expected by matchPath.') | ||
let path | ||
if (typeof pathParam === 'string') { | ||
path = { | ||
path: pathParam, | ||
exact: true, | ||
} | ||
} | ||
else { | ||
if (!pathParam.path) { | ||
throw Error('You must provide a path string or a path object (see API)') | ||
} | ||
path = pathParam | ||
} | ||
// Add the path to the paths list | ||
// Add the pathParam to the paths list | ||
router.paths.push({ | ||
method, | ||
matchProps, | ||
path, | ||
handler, | ||
@@ -59,6 +73,6 @@ }); | ||
const route = router.paths.find((route) => { | ||
if (req.method.toLowerCase() !== route.method) { | ||
if (route.method !== 'any' && route.method !== 'all' && req.method.toLowerCase() !== route.method) { | ||
return | ||
} | ||
const match = matchPath(req.path, route.matchProps) | ||
const match = matchPath(req.path, route.path) | ||
if (!match) { | ||
@@ -73,3 +87,3 @@ return | ||
} | ||
throw Error(`No route found to match req.path ${req.path}`) | ||
throw Error(`No route found to match ${req.method.toUpperCase()} ${req.path}`) | ||
} | ||
@@ -76,0 +90,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
155286
10
188