-
-
Notifications
You must be signed in to change notification settings - Fork 680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PoC: feat: Allow handlers and middleware to return functions of the same type as themselves #4012
base: main
Are you sure you want to change the base?
Conversation
…ype as themselves
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4012 +/- ##
==========================================
- Coverage 91.32% 91.29% -0.04%
==========================================
Files 168 168
Lines 10687 10706 +19
Branches 3021 3031 +10
==========================================
+ Hits 9760 9774 +14
- Misses 926 931 +5
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Hi @yusukebe. I'm not sure whether this is a really cool feature or just something that makes things more complicated. What do you think? |
First, regarding the performance of TypeScript. I've measured the performance in the same way of the CI (it does not work on this PR because of a permission issue). The
This PR #4012:
The result is |
Hi @usualoma Thank you for the proposal and the PoC. First of all, I like this feature. Until now, I had always thought that handlers would always return a However, I realized the only didn't need to be to return a One point that concerns me is that, until now, handlers have been defined simply as returning a // Simplified:
type Handler = (c: Context, next: Next) => Response | Promise<Response> | Handler
type MiddlewareHandler = (c: Context, next: Next) => Promise<Response | void | MiddlewareHandler> Anyway, this is a big feature that will change the definition of Hono's handler and middleware. We may be able to leave it for a while. |
Another reason I like this feature is that it allows me to write neatly. This may be a matter of personal taste, but it makes sense because I have always placed importance on being able to write neatly when developing Hono. // Verbose
app.get('/admin/*', (c, next) => {
return basicAuth({ username, password })(c, next)
})
// Neat!
app.get('/admin/*', () => {
return basicAuth({ username, password })
}) |
Hi @yusukebe, Thank you for your response! As you say, describing it as ‘neatly’ might be better. I think the following are two of Hono's core values (there are several).
This PR will expand the range of middleware use and further strengthen Hono's core values. Will ‘neatness’ continue to be important?In the Vibe Coding Era, when writing code with AI Agents, neatness may become less important than it is now. There is a possibility that, rather than being able to write concisely, people will prefer to write in a slightly more verbose but non-omitting way. However, highly abstract and neat code will remain valuable because it is easy to read. |
This PR will add a convenient feature to hono, but it will also increase the complexity, so please be careful. It is currently at the PoC stage.
What does this enable us to do?
We can dynamically configure middleware with very clean code.
Of course, I know that authentication middleware has the ability to dynamically determine credentials, but with this feature, it is easy to pass settings dynamically even if the middleware does not support it.
Isn't this just syntax sugar?
Yes, we can call it with
(c, next)
and return it as it is now, so it is safe to think of it as syntax sugar that omits(c, next)
.When is it particularly useful?
We will be able to solve issues that can be solved simply by using existing middleware in a clean way.
honojs/node-server#205
Is there any degradation in performance?
I don't think there is any decrease in performance at runtime.
When I compared them in "benchmarks/handle-event", there was no decrease in node and bun.
There may also be an impact on performance for "types", but I don't know the details of that, so I haven't been able to measure it.
The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code