hono icon indicating copy to clipboard operation
hono copied to clipboard

`c.req.valid` does not work in route without path

Open yusukebe opened this issue 8 months ago • 7 comments

What version of Hono are you using?

4.4.7

What runtime/platform is your app running on?

any

What steps can reproduce the bug?

const app = new Hono()

app
  .get('/', (c) => {
    return c.json(0)
  })
  .post(
    validator('json', () => {
      return {
        foo: 'bar',
      }
    }),
    (c) => {
      const foo = c.req.valid('json')
      return c.json(1)
    }
  )

What is the expected behavior?

It does not throw a type error.

What do you see instead?

CleanShot 2024-06-24 at 04 24 58@2x

Additional information

This will be fixed when changing the order for overloading in HandlerInterface.

diff --git a/src/types.ts b/src/types.ts
index 7c9ba8c..ce95d6b 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -121,22 +121,6 @@ export interface HandlerInterface<
     handler: H<E2, P, I, R>
   ): Hono<IntersectNonAnyTypes<[E, E2]>, S & ToSchema<M, P, I, MergeTypedResponse<R>>, BasePath>

-  // app.get(path, handler)
-  <
-    P extends string,
-    MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
-    R extends HandlerResponse<any> = any,
-    I extends Input = BlankInput,
-    E2 extends Env = E
-  >(
-    path: P,
-    handler: H<E2, MergedPath, I, R>
-  ): Hono<
-    IntersectNonAnyTypes<[E, E2]>,
-    S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>,
-    BasePath
-  >
-
   // app.get(handler x2)
   <
     P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
@@ -153,6 +137,22 @@ export interface HandlerInterface<
     BasePath
   >

+  // app.get(path, handler)
+  <
+    P extends string,
+    MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
+    R extends HandlerResponse<any> = any,
+    I extends Input = BlankInput,
+    E2 extends Env = E
+  >(
+    path: P,
+    handler: H<E2, MergedPath, I, R>
+  ): Hono<
+    IntersectNonAnyTypes<[E, E2]>,
+    S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>,
+    BasePath
+  >
+
   // app.get(path, handler x2)
   <
     P extends string,

yusukebe avatar Jun 23 '24 19:06 yusukebe