Skip to content

Commit

Permalink
fix(rpc): infer path with route() and basePath() (#1401)
Browse files Browse the repository at this point in the history
* fix(rpc): infer path with `route()` and `basePath()`

* denoify
  • Loading branch information
yusukebe authored Sep 3, 2023
1 parent 4550b31 commit d194e9b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions deno_dist/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Hono<
>(
path: SubPath,
app: Hono<SubEnv, SubSchema, SubBasePath>
): Hono<E, MergeSchemaPath<SubSchema, SubPath> & S, BasePath>
): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath>
/** @description
* Use `basePath` instead of `route` when passing **one** argument, such as `app.route('/api')`.
* The use of `route` with **one** argument has been removed in v4.
Expand All @@ -162,7 +162,7 @@ class Hono<
>(
path: SubPath,
app?: Hono<SubEnv, SubSchema, SubBasePath>
): Hono<E, MergeSchemaPath<SubSchema, SubPath> & S, BasePath> {
): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath> {
const subApp = this.basePath(path)

if (!app) {
Expand Down
18 changes: 18 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ describe('Merge path with `app.route()`', () => {
ok: true,
})
)
}),
rest.get('http://localhost/v1/book', async (req, res, ctx) => {
return res(
ctx.json({
ok: true,
})
)
})
)

Expand Down Expand Up @@ -374,6 +381,17 @@ describe('Merge path with `app.route()`', () => {
expect(data.ok).toBe(true)
})

it('Should have correct types - basePath(), route(), get()', async () => {
const book = new Hono().get('/', (c) => c.jsonT({ ok: true }))
const app = new Hono().basePath('/v1').route('/book', book)
type AppType = typeof app
const client = hc<AppType>('http://localhost')
const res = await client.v1.book.index.$get()
const data = await res.json()
type verify = Expect<Equal<boolean, typeof data.ok>>
expect(data.ok).toBe(true)
})

it('Should have correct types - with interface', async () => {
interface Result {
ok: boolean
Expand Down
4 changes: 2 additions & 2 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Hono<
>(
path: SubPath,
app: Hono<SubEnv, SubSchema, SubBasePath>
): Hono<E, MergeSchemaPath<SubSchema, SubPath> & S, BasePath>
): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath>
/** @description
* Use `basePath` instead of `route` when passing **one** argument, such as `app.route('/api')`.
* The use of `route` with **one** argument has been removed in v4.
Expand All @@ -162,7 +162,7 @@ class Hono<
>(
path: SubPath,
app?: Hono<SubEnv, SubSchema, SubBasePath>
): Hono<E, MergeSchemaPath<SubSchema, SubPath> & S, BasePath> {
): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath> {
const subApp = this.basePath(path)

if (!app) {
Expand Down

0 comments on commit d194e9b

Please sign in to comment.