From 13b58556c5bbc872de554df816fed9771ad0b861 Mon Sep 17 00:00:00 2001 From: Fukui <50707352+the-fukui@users.noreply.github.com> Date: Tue, 13 Feb 2024 21:34:54 +0900 Subject: [PATCH] fix(client): Fix regex pattern with length limit at replaceUrlParam (#2193) * fix: regex pattern with length limit at replaceUrlParam * fix: multiple length limit case * fix: use shorter pattern --- deno_dist/client/utils.ts | 2 +- src/client/utils.test.ts | 10 ++++++++++ src/client/utils.ts | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/deno_dist/client/utils.ts b/deno_dist/client/utils.ts index 017c7c10..aea09122 100644 --- a/deno_dist/client/utils.ts +++ b/deno_dist/client/utils.ts @@ -9,7 +9,7 @@ export const mergePath = (base: string, path: string) => { export const replaceUrlParam = (urlString: string, params: Record) => { for (const [k, v] of Object.entries(params)) { - const reg = new RegExp('/:' + k + '({[^}]*})?') + const reg = new RegExp('/:' + k + '(?:{[^/]+})?') urlString = urlString.replace(reg, `/${v}`) } return urlString diff --git a/src/client/utils.test.ts b/src/client/utils.test.ts index f348d734..a67a0140 100644 --- a/src/client/utils.test.ts +++ b/src/client/utils.test.ts @@ -30,6 +30,16 @@ describe('replaceUrlParams', () => { const replacedUrl = replaceUrlParam(url, params) expect(replacedUrl).toBe('http://localhost/posts/abc/comments/456') }) + + it('Should replace correctly when there is regex pattern with length limit', () => { + const url = 'http://localhost/year/:year{[1-9]{1}[0-9]{3}}/month/:month{[0-9]{2}}' + const params = { + year: '2024', + month: '2', + } + const replacedUrl = replaceUrlParam(url, params) + expect(replacedUrl).toBe('http://localhost/year/2024/month/2') + }) }) describe('removeIndexString', () => { diff --git a/src/client/utils.ts b/src/client/utils.ts index 4cdc0702..0b2573fb 100644 --- a/src/client/utils.ts +++ b/src/client/utils.ts @@ -9,7 +9,7 @@ export const mergePath = (base: string, path: string) => { export const replaceUrlParam = (urlString: string, params: Record) => { for (const [k, v] of Object.entries(params)) { - const reg = new RegExp('/:' + k + '({[^}]*})?') + const reg = new RegExp('/:' + k + '(?:{[^/]+})?') urlString = urlString.replace(reg, `/${v}`) } return urlString