Skip to content

Commit

Permalink
chore: pass trailing option to pathToRegexp call when strict is true (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
harryby1149 authored Sep 13, 2024
1 parent b5b08ba commit 5ce4e19
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = class Layer {
* @param {String=} opts.name route name
* @param {String=} opts.sensitive case sensitive (default: false)
* @param {String=} opts.strict require the trailing slash (default: false)
* @param {Boolean=} opts.pathIsRegexp if true, treat `path` as a regular expression
* @returns {Layer}
* @private
*/
Expand Down Expand Up @@ -45,6 +46,11 @@ module.exports = class Layer {
if (this.opts.pathIsRegexp === true) {
this.regexp = new RegExp(path);
} else if (this.path) {
if (this.opts.strict === true) {
// path-to-regexp renamed strict to trailing in v8.1.0
this.opts.trailing = false;
}

const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
this.regexp = regex;
this.paramNames = keys;
Expand Down
3 changes: 1 addition & 2 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ class Router {
strict: opts.strict || false,
prefix: opts.prefix || '',
ignoreCaptures: opts.ignoreCaptures,
pathIsRegexp: opts.pathIsRegexp,
trailing: opts.trailing
pathIsRegexp: opts.pathIsRegexp
});

// if parent prefix exists, add prefix to new route
Expand Down
8 changes: 4 additions & 4 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ describe('Router#opts', () => {
it('responds with 200', async () => {
const app = new Koa();
const router = new Router({
trailing: false
strict: true
});
router.get('/info', (ctx) => {
ctx.body = 'hello';
Expand Down Expand Up @@ -1689,7 +1689,7 @@ describe('Router#opts', () => {
it('responds with 404 when has a trailing slash', async () => {
const app = new Koa();
const router = new Router({
trailing: false
strict: true
});
router.get('/info', (ctx) => {
ctx.body = 'hello';
Expand All @@ -1704,7 +1704,7 @@ describe('use middleware with opts', () => {
it('responds with 200', async () => {
const app = new Koa();
const router = new Router({
trailing: false
strict: true
});
router.get('/info', (ctx) => {
ctx.body = 'hello';
Expand All @@ -1720,7 +1720,7 @@ describe('use middleware with opts', () => {
it('responds with 404 when has a trailing slash', async () => {
const app = new Koa();
const router = new Router({
trailing: false
strict: true
});
router.get('/info', (ctx) => {
ctx.body = 'hello';
Expand Down

0 comments on commit 5ce4e19

Please sign in to comment.