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:
parent
3fecc045e4
commit
13b58556c5
@ -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
|
||||
|
@ -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', () => {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user