Skip to content

Commit 331b3d8

Browse files
authored
fix(client): replace optional params to url correctly (#3304)
1 parent 95a6b39 commit 331b3d8

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/client/client.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,9 @@ describe('Infer the response type with different status codes', () => {
815815
})
816816

817817
describe('$url() with a param option', () => {
818-
const app = new Hono().get('/posts/:id/comments', (c) => c.json({ ok: true }))
818+
const app = new Hono()
819+
.get('/posts/:id/comments', (c) => c.json({ ok: true }))
820+
.get('/something/:firstId/:secondId/:version?', (c) => c.json({ ok: true }))
819821
type AppType = typeof app
820822
const client = hc<AppType>('http://localhost')
821823

@@ -832,6 +834,17 @@ describe('$url() with a param option', () => {
832834
const url = client.posts[':id'].comments.$url()
833835
expect(url.pathname).toBe('/posts/:id/comments')
834836
})
837+
838+
it('Should return the correct path - /something/123/456', async () => {
839+
const url = client.something[':firstId'][':secondId'][':version?'].$url({
840+
param: {
841+
firstId: '123',
842+
secondId: '456',
843+
version: undefined,
844+
},
845+
})
846+
expect(url.pathname).toBe('/something/123/456')
847+
})
835848
})
836849

837850
describe('Client can be awaited', () => {

src/client/utils.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ describe('replaceUrlParams', () => {
4646
const replacedUrl = replaceUrlParam(url, params)
4747
expect(replacedUrl).toBe('http://localhost/year/2024/month/2')
4848
})
49+
50+
it('Should replace correctly when it has optional parameters', () => {
51+
const url = 'http://localhost/something/:firstId/:secondId/:version?'
52+
const params = {
53+
firstId: '123',
54+
secondId: '456',
55+
version: undefined,
56+
}
57+
const replacedUrl = replaceUrlParam(url, params)
58+
expect(replacedUrl).toBe('http://localhost/something/123/456')
59+
})
4960
})
5061

5162
describe('replaceUrlProtocol', () => {

src/client/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export const mergePath = (base: string, path: string) => {
77
return base + path
88
}
99

10-
export const replaceUrlParam = (urlString: string, params: Record<string, string>) => {
10+
export const replaceUrlParam = (urlString: string, params: Record<string, string | undefined>) => {
1111
for (const [k, v] of Object.entries(params)) {
12-
const reg = new RegExp('/:' + k + '(?:{[^/]+})?')
13-
urlString = urlString.replace(reg, `/${v}`)
12+
const reg = new RegExp('/:' + k + '(?:{[^/]+})?\\??')
13+
urlString = urlString.replace(reg, v ? `/${v}` : '')
1414
}
1515
return urlString
1616
}

0 commit comments

Comments
 (0)