Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add exclusive option #129

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Create a new router.
| --- | --- | --- |
| [opts] | <code>Object</code> | |
| [opts.prefix] | <code>String</code> | prefix router paths |
| [opts.exclusive] | <code>Boolean</code> | only run last matched route's controller when there are multiple matches |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add some examples?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to add examples in another PR that'd be cool! If not no worries.


**Example**
Basic usage:
Expand Down
4 changes: 3 additions & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = Router;
*
* @alias module:koa-router
* @param {Object=} opts
* @param {Boolean=false} opts.exclusive only run last matched route's controller when there are multiple matches
* @param {String=} opts.prefix prefix router paths
* @constructor
*/
Expand All @@ -60,6 +61,7 @@ function Router(opts) {
'POST',
'DELETE'
];
this.exclusive = !!this.opts.exclusive;

this.params = {};
this.stack = [];
Expand Down Expand Up @@ -359,7 +361,7 @@ Router.prototype.routes = Router.prototype.middleware = function () {
ctx._matchedRouteName = mostSpecificLayer.name;
}

layerChain = matchedLayers.reduce(function(memo, layer) {
layerChain = (router.exclusive ? [mostSpecificLayer] : matchedLayers).reduce(function(memo, layer) {
memo.push(function(ctx, next) {
ctx.captures = layer.captures(path, ctx.captures);
ctx.params = ctx.request.params = layer.params(path, ctx.captures, ctx.params);
Expand Down
50 changes: 50 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,56 @@ describe('Router', function () {
})
});

it('runs multiple controllers when there are multiple matches', function (done) {
const app = new Koa();
const router = new Router();

router
.get('users_single', '/users/:id(.*)', function (ctx, next) {
ctx.body = { single: true };
next();
})
.get('users_all', '/users/all', function (ctx, next) {
ctx.body = Object.assign({}, ctx.body, { all: true });
next();
});

request(http.createServer(app.use(router.routes()).callback()))
.get('/users/all')
.expect(200)
.end(function (err, res) {
if (err) return done(err);
expect(res.body).to.have.property('single', true);
expect(res.body).to.have.property('all', true);
done();
})
});

it('runs only the last match when the \'exclusive\' option is enabled', function (done) {
const app = new Koa();
const router = new Router({ exclusive: true });

router
.get('users_single', '/users/:id(.*)', function (ctx, next) {
ctx.body = { single: true };
next();
})
.get('users_all', '/users/all', function (ctx, next) {
ctx.body = Object.assign({}, ctx.body, { all: true });
next();
});

request(http.createServer(app.use(router.routes()).callback()))
.get('/users/all')
.expect(200)
.end(function (err, res) {
if (err) return done(err);
expect(res.body).to.not.have.property('single');
expect(res.body).to.have.property('all', true);
done();
})
});

it('does not run subsequent middleware without calling next', function (done) {
const app = new Koa();
const router = new Router();
Expand Down