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