From 32b76d09709c2eaea23b40cd2d35881fde45aee2 Mon Sep 17 00:00:00 2001 From: Prabhu Marappan Date: Sun, 6 Jun 2021 16:51:53 +0530 Subject: [PATCH] Added replacement option for path --- spec/normalizePath.spec.js | 5 +++++ src/normalizePath.js | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/normalizePath.spec.js b/spec/normalizePath.spec.js index 1bb2337..17b434f 100644 --- a/spec/normalizePath.spec.js +++ b/spec/normalizePath.spec.js @@ -28,4 +28,9 @@ describe('normalizePath', () => { }); expect(subject).toThrow(); }); + + it('uses urlPathReplacement when passed to transform the path', () => { + expect(normalizePath({url: '/a/12345'}, {urlPathReplacement: ':id'})) + .toBe('/a/:id'); + }); }); diff --git a/src/normalizePath.js b/src/normalizePath.js index f2a0f57..8102840 100644 --- a/src/normalizePath.js +++ b/src/normalizePath.js @@ -11,6 +11,7 @@ module.exports = function(req, opts) { // by middlewares such as 'router'. Note: this function is called onFinish /// i.e. always in the tail of the middleware chain let path = url.parse(req.originalUrl || req.url).pathname; + const urlPathReplacement = opts ? opts.urlPathReplacement : '#val'; const normalizePath = opts && opts.normalizePath; if (Array.isArray(normalizePath)) { @@ -26,5 +27,5 @@ module.exports = function(req, opts) { if (!urlValueParser) { urlValueParser = new UrlValueParser(opts && opts.urlValueParser); } - return urlValueParser.replacePathValues(path); + return urlValueParser.replacePathValues(path, urlPathReplacement); };