Skip to content

Commit 0432f81

Browse files
yusukebejraoult
andauthored
fix(client): remove index string when calling $url() (#4267)
Co-authored-by: Jonathan Raoult <2046871+jraoult@users.noreply.github.com>
1 parent 09a81e4 commit 0432f81

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/client/client.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,42 @@ describe('Basic - query, queries, form, path params, header and cookie', () => {
339339
})
340340
})
341341

342+
describe('Basic - $url()', () => {
343+
const api = new Hono().get('/', (c) => c.text('API')).get('/posts/:id', (c) => c.text('Post'))
344+
const content = new Hono().get(
345+
'/search',
346+
validator('query', () => {
347+
return { page: '1', limit: '10' }
348+
}),
349+
(c) => c.text('Search')
350+
)
351+
const app = new Hono()
352+
.get('/', (c) => c.text('Index'))
353+
.route('/api', api)
354+
.route('/content', content)
355+
356+
it('Should return a correct url via $url().href', async () => {
357+
const client = hc<typeof app>('http://fake')
358+
expect(client.index.$url().href).toBe('http://fake/')
359+
expect(client.api.$url().href).toBe('http://fake/api')
360+
expect(
361+
client.api.posts[':id'].$url({
362+
param: {
363+
id: '123',
364+
},
365+
}).href
366+
).toBe('http://fake/api/posts/123')
367+
expect(
368+
client.content.search.$url({
369+
query: {
370+
page: '123',
371+
limit: '20',
372+
},
373+
}).href
374+
).toBe('http://fake/content/search?page=123&limit=20')
375+
})
376+
})
377+
342378
describe('Form - Multiple Values', () => {
343379
const server = setupServer(
344380
http.post('http://localhost/multiple-values', async ({ request }) => {

src/client/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export const hc = <T extends Hono<any, any, any>>(
166166
result = result + '?' + buildSearchParams(opts.args[0].query).toString()
167167
}
168168
}
169+
result = removeIndexString(result)
169170
return new URL(result)
170171
}
171172
if (method === 'ws') {

0 commit comments

Comments
 (0)