@hono/zod-openapi Middleware Issue
Hi there,
I need some help in implementing middleware while using hono and @hono/zod-openapi.
What I want to implement is running middleware after zod validation of query params but before the actual route handler.
My code is as follows:
This is the route code
This is route initialization code
9 Replies
This is the route definition
And this is the handler
And these are the types
To summarize I want to get access to
c.req.valid("query");
inside a middleware (running before the handler).
If it is possible like this then it would be awesome.
router.openapi(routes.list,authMiddleware, handlers.list);
fwiw, this level of abstraction doesn't play all that nicely with hono, and ultimately makes code maintenance more difficult
but to make intermediate middleware aware of validation types you can use the
createMiddleware
generic types
e.g.,
the in
param doesn't really matter afaik, but out
should specify the resolved type of the validated target dataBut is there a way to infer it from ListRoute type similar to how AppRouteHandler Generic works?
no. there's no cross-middleware inference, unless you inline it
that's the cost of abstraction in hono
That sad to hear 🥲 But thanks for the input
There’s an open discussion RE adding support for the kind of inference you’re looking for, but it would likely involve overhauling Hono’s type system, so it won’t happen any time soon
Can you post the link for it?
I don’t have it locked and loaded. I’m sure it will come up if you search GH issues for middleware typing though
Ok thanks