From d194e9b3c259874841bd9a4a53bee780ef73b3f1 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Mon, 4 Sep 2023 07:59:04 +0900 Subject: [PATCH] fix(rpc): infer path with `route()` and `basePath()` (#1401) * fix(rpc): infer path with `route()` and `basePath()` * denoify --- deno_dist/hono-base.ts | 4 ++-- src/client/client.test.ts | 18 ++++++++++++++++++ src/hono-base.ts | 4 ++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/deno_dist/hono-base.ts b/deno_dist/hono-base.ts index 04f04170f..0696d277e 100644 --- a/deno_dist/hono-base.ts +++ b/deno_dist/hono-base.ts @@ -147,7 +147,7 @@ class Hono< >( path: SubPath, app: Hono - ): Hono & S, BasePath> + ): Hono> & 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. @@ -162,7 +162,7 @@ class Hono< >( path: SubPath, app?: Hono - ): Hono & S, BasePath> { + ): Hono> & S, BasePath> { const subApp = this.basePath(path) if (!app) { diff --git a/src/client/client.test.ts b/src/client/client.test.ts index 4f6914d10..b20e39e8d 100644 --- a/src/client/client.test.ts +++ b/src/client/client.test.ts @@ -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, + }) + ) }) ) @@ -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('http://localhost') + const res = await client.v1.book.index.$get() + const data = await res.json() + type verify = Expect> + expect(data.ok).toBe(true) + }) + it('Should have correct types - with interface', async () => { interface Result { ok: boolean diff --git a/src/hono-base.ts b/src/hono-base.ts index 23b54920c..bf14e1634 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -147,7 +147,7 @@ class Hono< >( path: SubPath, app: Hono - ): Hono & S, BasePath> + ): Hono> & 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. @@ -162,7 +162,7 @@ class Hono< >( path: SubPath, app?: Hono - ): Hono & S, BasePath> { + ): Hono> & S, BasePath> { const subApp = this.basePath(path) if (!app) {