From b7ea4a686de4887423053690721d6b186b906f2f Mon Sep 17 00:00:00 2001 From: Harry B Young <40366989+harryby1149@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:37:39 -0400 Subject: [PATCH] chore: pass trailing option to pathToRegexp call when strict is true (#190) --- lib/layer.js | 6 ++++++ lib/router.js | 3 +-- test/lib/router.js | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/layer.js b/lib/layer.js index dc8cab5..decc73b 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -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 */ @@ -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; diff --git a/lib/router.js b/lib/router.js index 9c5fadc..75e9d8f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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 diff --git a/test/lib/router.js b/test/lib/router.js index affbb14..63b454f 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -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'; @@ -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'; @@ -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'; @@ -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';