-
-
Notifications
You must be signed in to change notification settings - Fork 870
Description
What is the feature you are proposing?
Hi Hono team!
I love that Hono is fast, tiny, and avoids unnecessary abstractions. But when building apps, I keep coming back to a simple need: a built-in way to write plain class-based handlers without decorators, middleware chains, or reflection.
The core idea is this: a service is just a few classes (for GET, POST, WS, etc.), each with:
Special members like headers, cookie, query, body
Public methods like json(), jsx(), redirect(), validate(), jwt.sign()
No @get(), no DI containers, no pipeline — just explicit, testable, readable code.
Example:
ts
编辑
class LoginHandler extends BaseHandler {
async handle() {
const { email, password } = await this.body;
if (!this.cookie.includes('auth=')) {
return this.json({ error: 'Unauthorized' }, 401);
}
const user = await db.find(email);
return this.json({ id: user.id, name: user.name });
}
}
app.post('/login', (c) => new LoginHandler(c).handle());
This pattern is:
Easy to test (just new LoginHandler(mockCtx))
Fully tree-shakable
Zero runtime overhead
Aligns with Hono’s “you don’t pay for what you don’t use” philosophy
Proposal:
Export a lightweight BaseHandler class from hono (or hono/handler)
Pre-bind common properties (this.body, this.cookie, etc.) and helpers (this.json())
Document this as an official “minimal class-based” style
It wouldn’t change anything for existing users — just give simplicity-minded developers a blessed path.
Would you consider this? I’d be happy to help draft a PR.
Thanks for your amazing work! 🙏