feat: Improve the extensibility of Hono's Context
What is the feature you are proposing?
Currently, Hono's Context is instantiated internally within HonoBase.
By allowing the Context to be injected into HonoBase externally, third-party libraries could freely provide Hono objects with an extended Context.
https://github.com/honojs/hono/blob/f9a23a9992979fed79b0703ab8b3a3ce49f7175f/src/hono-base.ts#L411-L417
Pros
- Easier to extend
- Enhances the purity of
HonoBase
Cons
- Potential for unintended usage
- Could lead to breaking changes
Background
Hono's Context has become bloated over time as new features have been added with each version update. Since fields included in the Context are not tree-shakable, efforts like #1846 have been made to reduce the weight of the Context.
If we implemented extensible Contexts, we could provide various presets built on top of a minimal Context, allowing users to select only the necessary fields according to their specific use case.
I have some ideas. I'll share them later.
I tried working on this, but the method I used to inject the Context via DI resulted in the handler parameters in types.ts being doubled due to the Context inheritance, which seems to increase the inference cost even more (since the Env changes for each handler).