Skip to content
Myr

fix(client): keep a param value of "index" in $url() and $path() (#5297)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Mahathir Mohammad Shuvo · committed by GitHub · parent 06880c4 · Browse files · 2 files+75 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+75 added−10 removed

  • tests:+71 added−7 removed
  • other:+4 added−3 removed

2 files+75 added−10 removed

modified:src/client/client.test.ts+71 added-7 removed
1241 unmodified lines
1242describe.each(['$path', '$url'] as const)('%s() with a param option', (cmd) => {1243 const app = new Hono()1244 .get('/posts/:id/comments', (c) => c.json({ ok: true }))1245 .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 }))1250 type AppType = typeof app1251 const client = hc<AppType>('http://localhost')12521253 it('Should return the correct url path - /posts/123/comments', async () => {
19 unmodified lines
1273 },1274 })1275 expect(pathname(value)).toBe('/something/123/456')1276 })12771278 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 })12861287 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 })12951296 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 })13041305 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 })1313})13141315describe('$url() / $path() with a query option', () => {1316 const app = new Hono()1276 const app = new Hono().get(1277 '/posts',1278 validator('query', () => {1279 return {} as { filter: 'test' }1280 }),1281 (c) => c.json({ ok: true })1282 )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 }))1325 type AppType = typeof app1326 const client = hc<AppType>('http://localhost')13271328 it('Should return the correct path - /posts?filter=test', async () => {
10 unmodified lines
1339 },1340 })1341 expect(path).toBe('/posts?filter=test')1342 })13431344 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')13541355 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 })1365})13661367describe('Client can be awaited', () => {1368 it('Can be awaited without side effects', async () => {
930 unmodified lines
modified:src/client/client.ts+4 added-3 removed
185 unmodified lines
186187 const path = parts.join('/')188 const url = mergePath(baseUrl, path)189 if (method === 'url' || method === 'path') {190 let result = url190 // Strip the synthetic `index` segment before substituting params, so that a param191 // whose value is `index` is not mistaken for one.192 let result = removeIndexString(url)193 if (opts.args[0]) {194 if (opts.args[0].param) {193 result = replaceUrlParam(url, opts.args[0].param)195 result = replaceUrlParam(result, opts.args[0].param)196 }197 if (opts.args[0].query) {198 result = appendQueryParams(result, buildSearchParamsOption(opts.args[0].query))199 }200 }199 result = removeIndexString(result)201 if (method === 'url') {202 return new URL(result)203 }204 return result.slice(baseUrl.replace(/\/+$/, '').length).replace(/^\/?/, '/')
44 unmodified lines