Handler type
Hey guys was wondering, what is the best way to implement a
RouteHandler
like the one present in @hono/zod-openapi
based om my code below, I am using hono-openapi
for .lazy()
and a couple other features31 Replies
File, yes it's a TSX because Discord mobile things .TS is a media file
Can you share a snippet or a link to GitHub?
Opening files from internet strangers is kinda sus
GitHub
Custom RouteHandler type · honojs · Discussion #4087
Hey guys was wondering, what is the best way to implement a RouteHandler like the one present in @hono/zod-openapi based on my function below createRoute, I am using hono-openapi for .lazy() and a ...
thanks! i'll take a look
i was on mobile + didn't realize that the file was embedded; my bad
@Ego you mean this? https://github.com/honojs/middleware/blob/6c36f525f9682909196eaffd620e30a6a1f34b76/packages/zod-openapi/src/index.ts#L318
Can you share a bit more about what your goal is?
It seems like you’re creating a factory to construct handlers based mainly on schema inputs
And what issues are you running into?
I basically want to do something like that yeah, my experience with typing inference related stuff is close to none so I honestly just don't know where to start
I’d recommend taking a look at the factory helpers
And spend some time studying the hono source code
But tbh it’s a pretty ambitious project if you’re not already comfortable w generics, and it seems like an overly rigid way to use hono
My problem could be simplified quite a bit if I wasn't using the speerate
definitions.ts
and handlers.ts
files and I would just register stuff right away tho it would make my code way less readable tbh so I end up with stuff like this
I have come up with this but it feels incredibly incorrect and honestly kinda disgusting and it still doesn't do the return types correctly
What do you mean by “doesn’t do the return types correctly”?
It's not inferring the
return c.json
Always ends up in a neverUsing a higher-order function might help a bit. Then you can pass the handler itself as a callback
Treating validators as a tuple might also free you up a bit, but I’m just riffing
My first goal would be to get rid of most of the custom types. They’re really going to slow down the TS server
The HOF will help w that; leaning more on hono helpers might as well
Yeah I'm pretty sure I should be using the hono types directly, tbh I'm just a bit clueless and this is actually way too complicated for me to start with
It's just a bit sad that the official zod-openapi middleware uses a package underneath that is abandonware
Otherwise it would work perfectly
Have you tried
hono-openapi
?
It’s newer, and the typing works a bit differently, but if nothing else it could be a helpful referenceThat's exactly what I'm using it just handles things way different and doesn't provide generic types for this
That's why I made the createRoute factory to try and be as plug and play as possible
Gotcha. The
createApp
factory paired with the createHandlers
helper might be what you’re looking for
I think that one of the issues you’re running into though is Hono’s own typing. It’s good-enough for (mostly) nice DX, but it does some funky things that make it hard to extend, or work with in complicated waysI'm pretty sure I could use hono's own handler type if I knew more but I'm sadly not smart enough
From a learning perspective, a simpler starting point might be to create a custom validation middleware that applies all the relevant validators
That would work if I defined my route in the function itself
Maybe. I wouldn’t be so sure. Regardless, it’s just a matter of practice, so you’re on the right track!
Unfortunately that would clutter my files incredibly
If I did use a new Hono on each file etc it could work but then separation of concerns would be an annoying thing
I like to create each route (sometimes each endpoint) in its own file. Each has its own Hono instance, and most of the code is inline
I only abstract custom middleware and db boilerplate
Yeah that would simplify my life a lot but at the same time I personally don't like how the approach looks
No? What don’t you like about it? (Not that you have to)
It feels awkward, I come from a Laravel background and I really like how it handles most stuff
Interesting. I really like it because everything that matters for each handler is in one place, and I find it easy to read, modify, and maintain
IMO it’s a really clean way to write JS
That makes sense as long as you don't share a lot of code like schemas etc
My schemas live in their own directory
As do helpers, utils, and anything else shared
I keep handlers specifically scoped to their own implementation concerns
Makes sense if they're not owned by a specific resource
Also makes it easy to extend them, or share them with other schemas
That resource has 4 or 5 methods that might or might not slightly change the way the schema looks
Sounds like that resource could be 4 or 5 resources with their own schema
But I also burned myself out on abstractions and complex types last year, lol
Yeah I can see that
The complex types in this specific use case do feel perfect for my style