0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-22 02:27:49 +01:00

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
This commit is contained in:
Fukui 2024-02-13 21:34:54 +09:00 committed by GitHub
parent 3fecc045e4
commit 13b58556c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 2 deletions

View File

@ -9,7 +9,7 @@ export const mergePath = (base: string, path: string) => {
export const replaceUrlParam = (urlString: string, params: Record<string, string>) => {
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

View File

@ -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', () => {

View File

@ -9,7 +9,7 @@ export const mergePath = (base: string, path: string) => {
export const replaceUrlParam = (urlString: string, params: Record<string, string>) => {
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