Skip to content

Commit 50b8788

Browse files
fix(client): keep a param value of "index" in $url() and $path() (#5297)
removeIndexString() strips a trailing /index so that the synthetic .index accessor resolves to the route it aliases. In the $url()/$path() branch it ran after the params had been substituted, so anything that merely looked like the alias once substitution was done got stripped as well: client.docs[':page'].$path({ param: { page: 'index' } }) // "/docs" client.index[':v?'].$path({ param: { v: undefined } }) // "/" The second is a route with a literal /index segment; "/" does not match it and returns 404, so that URL never pointed at the route it came from. The alias belongs to the route template and neither of those does, so the strip now runs before substitution. ClientRequestImpl.fetch already does it in that order, which is why $get() has always returned the right path for these routes while $url() and $path() did not.
1 parent 06880c4 commit 50b8788

2 files changed

Lines changed: 75 additions & 10 deletions

File tree

‎src/client/client.test.ts‎

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,10 @@ describe.each(['$path', '$url'] as const)('%s() with a param option', (cmd) => {
12431243
const app = new Hono()
12441244
.get('/posts/:id/comments', (c) => c.json({ ok: true }))
12451245
.get('/something/:firstId/:secondId/:version?', (c) => c.json({ ok: true }))
1246+
.get('/docs/:page', (c) => c.json({ ok: true }))
1247+
.get('/files/:dir/', (c) => c.json({ ok: true }))
1248+
.get('/:page', (c) => c.json({ ok: true }))
1249+
.get('/index/:v?', (c) => c.json({ ok: true }))
12461250
type AppType = typeof app
12471251
const client = hc<AppType>('http://localhost')
12481252

@@ -1270,16 +1274,54 @@ describe.each(['$path', '$url'] as const)('%s() with a param option', (cmd) => {
12701274
})
12711275
expect(pathname(value)).toBe('/something/123/456')
12721276
})
1277+
1278+
it('Should keep a param value of "index" - /docs/index', async () => {
1279+
const value = client.docs[':page'][cmd]({
1280+
param: {
1281+
page: 'index',
1282+
},
1283+
})
1284+
expect(pathname(value)).toBe('/docs/index')
1285+
})
1286+
1287+
it('Should keep a root-level param value of "index" - /index', async () => {
1288+
const value = client[':page'][cmd]({
1289+
param: {
1290+
page: 'index',
1291+
},
1292+
})
1293+
expect(pathname(value)).toBe('/index')
1294+
})
1295+
1296+
it('Should keep a literal index segment when an optional param is omitted - /index', async () => {
1297+
const value = client.index[':v?'][cmd]({
1298+
param: {
1299+
v: undefined,
1300+
},
1301+
})
1302+
expect(pathname(value)).toBe('/index')
1303+
})
1304+
1305+
it('Should still drop the index alias of a route with a param - /files/123', async () => {
1306+
const value = client.files[':dir'].index[cmd]({
1307+
param: {
1308+
dir: '123',
1309+
},
1310+
})
1311+
expect(pathname(value)).toBe('/files/123')
1312+
})
12731313
})
12741314

12751315
describe('$url() / $path() with a query option', () => {
1276-
const app = new Hono().get(
1277-
'/posts',
1278-
validator('query', () => {
1279-
return {} as { filter: 'test' }
1280-
}),
1281-
(c) => c.json({ ok: true })
1282-
)
1316+
const app = new Hono()
1317+
.get(
1318+
'/posts',
1319+
validator('query', () => {
1320+
return {} as { filter: 'test' }
1321+
}),
1322+
(c) => c.json({ ok: true })
1323+
)
1324+
.get('/docs/:page', (c) => c.json({ ok: true }))
12831325
type AppType = typeof app
12841326
const client = hc<AppType>('http://localhost')
12851327

@@ -1298,6 +1340,28 @@ describe('$url() / $path() with a query option', () => {
12981340
})
12991341
expect(path).toBe('/posts?filter=test')
13001342
})
1343+
1344+
it('Should return the correct path - /docs/index?filter=test', async () => {
1345+
const url = client.docs[':page'].$url({
1346+
param: {
1347+
page: 'index',
1348+
},
1349+
query: {
1350+
filter: 'test',
1351+
},
1352+
})
1353+
expect(url.href).toBe('http://localhost/docs/index?filter=test')
1354+
1355+
const path = client.docs[':page'].$path({
1356+
param: {
1357+
page: 'index',
1358+
},
1359+
query: {
1360+
filter: 'test',
1361+
},
1362+
})
1363+
expect(path).toBe('/docs/index?filter=test')
1364+
})
13011365
})
13021366

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

‎src/client/client.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,17 @@ export const hc = <T extends Hono<any, any, any>, Prefix extends string = string
187187
const path = parts.join('/')
188188
const url = mergePath(baseUrl, path)
189189
if (method === 'url' || method === 'path') {
190-
let result = url
190+
// Strip the synthetic `index` segment before substituting params, so that a param
191+
// whose value is `index` is not mistaken for one.
192+
let result = removeIndexString(url)
191193
if (opts.args[0]) {
192194
if (opts.args[0].param) {
193-
result = replaceUrlParam(url, opts.args[0].param)
195+
result = replaceUrlParam(result, opts.args[0].param)
194196
}
195197
if (opts.args[0].query) {
196198
result = appendQueryParams(result, buildSearchParamsOption(opts.args[0].query))
197199
}
198200
}
199-
result = removeIndexString(result)
200201
if (method === 'url') {
201202
return new URL(result)
202203
}

0 commit comments

Comments
 (0)