Comparing version
@@ -1,2 +0,34 @@ | ||
module.exports = function () { | ||
const { exec, match, parse } = require('matchit'); | ||
const METHODS = ['GET', 'POST', 'PUT', 'DELETE']; | ||
class Trouter { | ||
constructor(opts) { | ||
this.opts = opts || {}; | ||
this.routes = {}; | ||
this.handlers = {}; | ||
METHODS.forEach(str => { | ||
this[str.toLowerCase()] = this.add.bind(this, str); | ||
this.handlers[str] = {}; | ||
this.routes[str] = []; | ||
}); | ||
} | ||
add(method, pattern, handler) { | ||
// Save decoded pattern info | ||
this.routes[method].push(parse(pattern)); | ||
// Save route handler | ||
this.handlers[method][pattern] = handler; | ||
} | ||
find(method, url) { | ||
let arr = match(url, this.routes[method]); | ||
if (!arr.length) return false; | ||
return { | ||
params: exec(url, arr), | ||
handler: this.handlers[method][arr[0].old] | ||
}; | ||
} | ||
} | ||
module.exports = Trouter; |
{ | ||
"name": "trouter", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "WIP", | ||
@@ -24,2 +24,5 @@ "repository": "lukeed/mri", | ||
"keywords": [], | ||
"dependencies": { | ||
"matchit": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -26,0 +29,0 @@ "tap-spec": "^4.1.1", |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
2763
38.64%29
1350%0
-100%1
Infinity%+ Added
+ Added
+ Added