Skip to content

Commit

Permalink
chore: update tree router
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Dec 28, 2020
1 parent d466a25 commit 8d9962e
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 180 deletions.
18 changes: 8 additions & 10 deletions lib/router/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* istanbul ignore file */
// /~https://github.com/steambap/koa-tree-router @ 0.4.4 / MIT
// /~https://github.com/steambap/koa-tree-router @ 0.7.0 / MIT
// Modifications:
// - code style (npm run lint-fix)
// - fixed composed middleware rather then composing on every request
// - code style
// - compose middlewares upfronts rather then composing on every request
// - removed 405 handling
// - removed unused methods

const compose = require('koa-compose');

Expand All @@ -17,11 +17,11 @@ class Router {
this.opts = opts;
}

on(method, path, ...middlewares) {
on(method, path, ...handle) {
if (!this.trees[method]) {
this.trees[method] = new Node();
}
this.trees[method].addRoute(path, compose(middlewares));
this.trees[method].addRoute(path, compose(handle));
return this;
}

Expand Down Expand Up @@ -54,18 +54,16 @@ class Router {
}

routes() {
const router = this;
return (ctx, next) => {
const { handle, params } = this.find(ctx.method, ctx.path);

const { handle, params } = router.find(ctx.method, ctx.path);
if (!handle) {
return next();
}

ctx.params = {};
params.forEach(({ key, value }) => {
ctx.params[key] = value;
});

return handle(ctx, next);
};
}
Expand Down
Loading

0 comments on commit 8d9962e

Please sign in to comment.