hono icon indicating copy to clipboard operation
hono copied to clipboard

RPC supports middleware responses

Open yusukebe opened this issue 1 year ago • 12 comments

What is the feature you are proposing?

Currently, the RPC mode does not support middleware response. The client can't handle the response from the middleware:

CleanShot 2024-05-19 at 06 46 01@2x

I'm not 100% sure, but perhaps we can implement it by modifying types.ts:

diff --git a/src/types.ts b/src/types.ts
index 3c7a53d..5e93fc1 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -190,13 +190,22 @@ export interface HandlerInterface<
     I3 extends Input = I & I2,
     E2 extends Env = E,
     E3 extends Env = E,
-    E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>
+    E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>,
+    // Response from Middleware
+    RM1 extends HandlerResponse<any> = any,
+    RM2 extends HandlerResponse<any> = any
   >(
     path: P,
-    ...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2>, H<E4, MergedPath, I3, R>]
+    ...handlers: [H<E2, MergedPath, I, RM1>, H<E3, MergedPath, I2, RM2>, H<E4, MergedPath, I3, R>]
   ): Hono<
     IntersectNonAnyTypes<[E, E2, E3, E4]>,
-    S & ToSchema<M, MergePath<BasePath, P>, I3, MergeTypedResponse<R>>,
+    S &
+      ToSchema<
+        M,
+        MergePath<BasePath, P>,
+        I3,
+        MergeTypedResponse<R> | MergeTypedResponse<RM1> | MergeTypedResponse<RM2>
+      >,
     BasePath
   >

CleanShot 2024-05-19 at 06 48 11@2x

It will be worth implementing, though the code will be very long.

yusukebe avatar May 18 '24 21:05 yusukebe

Hey @kosei28 What do you think about it?

yusukebe avatar May 18 '24 21:05 yusukebe

This is exactly what I wanted!

However, merging this may cause type errors in many projects currently using Hono's RPC, so I think this should be considered carefully.

kosei28 avatar May 18 '24 22:05 kosei28

@kosei28 Thanks for the comment!

However, merging this may cause type errors in many projects currently using Hono's RPC, so I think this should be considered carefully.

Exactly. We don't have to hurry!

yusukebe avatar May 18 '24 22:05 yusukebe

I think res should always include some sort of unknown response anyway, any route could return 500 if it throws an error or 502/504 if there's a proxy in the way, and even if middleware responses were included there's still throw new HTTPException which can't be picked up by typescript.

KaelWD avatar Jul 05 '24 15:07 KaelWD

Hey guys any timeline on this? currently i have a authentication middleware but without this feature I am having to authenticate at every request to get typings correctly. Any work around that you can suggest will also be fine.

dvrfluxchat avatar Nov 26 '24 11:11 dvrfluxchat

Also wondering if there's any update on this?

lewisedc avatar Dec 13 '24 20:12 lewisedc

Hey do you have any news on this particular issue ? I was fighting against error types not being inferred on my front end and realized it was coming from this issue.

Sadly at the moment, validators and RPC are not 100% working together.

FLchs avatar Mar 11 '25 15:03 FLchs

Any way to write a compose util to solve this until this is implemented?

app.get("/", compose(auth(), () => ...))

motherthestate avatar Mar 20 '25 13:03 motherthestate