0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-22 11:17:33 +01:00

fix(types): MergePath merge blank paths correctly (#2365)

* fix(types): `MergePath` merge blank paths correctly

* denoify
This commit is contained in:
Yusuke Wada 2024-03-19 05:47:44 +09:00 committed by GitHub
parent f60ab8b93e
commit ebdcd246b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 2 deletions

View File

@ -1649,7 +1649,9 @@ export type AddParam<I, P extends string> = ParamKeys<P> extends never
type AddDollar<T extends string> = `$${Lowercase<T>}`
export type MergePath<A extends string, B extends string> = A extends ''
export type MergePath<A extends string, B extends string> = B extends ''
? MergePath<A, '/'>
: A extends ''
? B
: A extends '/'
? B

View File

@ -500,6 +500,27 @@ describe('Merge path with `app.route()`', () => {
expect(url.href).toBe('http://localhost/api/bar')
})
})
describe('With a blank path', () => {
const app = new Hono().basePath('/api/v1')
const routes = app.route(
'/me',
new Hono().route(
'',
new Hono().get('', async (c) => {
return c.json({ name: 'hono' })
})
)
)
const client = hc<typeof routes>('http://localhost')
it('Should infer paths correctly', async () => {
// Should not a throw type error
const url = client.api.v1.me.$url()
expectTypeOf<URL>(url)
expect(url.href).toBe('http://localhost/api/v1/me')
})
})
})
describe('Use custom fetch method', () => {

View File

@ -535,6 +535,14 @@ describe('MergePath', () => {
type verify3 = Expect<Equal<'/api/', path3>>
type path4 = MergePath<'/api', '/'>
type verify4 = Expect<Equal<'/api', path4>>
type path5 = MergePath<'/', ''>
type verify5 = Expect<Equal<'/', path5>>
type path6 = MergePath<'', '/'>
type verify6 = Expect<Equal<'/', path6>>
type path7 = MergePath<'/', '/'>
type verify7 = Expect<Equal<'/', path7>>
type path8 = MergePath<'', ''>
type verify8 = Expect<Equal<'/', path8>>
})
})

View File

@ -1649,7 +1649,9 @@ export type AddParam<I, P extends string> = ParamKeys<P> extends never
type AddDollar<T extends string> = `$${Lowercase<T>}`
export type MergePath<A extends string, B extends string> = A extends ''
export type MergePath<A extends string, B extends string> = B extends ''
? MergePath<A, '/'>
: A extends ''
? B
: A extends '/'
? B