hono icon indicating copy to clipboard operation
hono copied to clipboard

feat: Improve the extensibility of Hono's Context

Open sor4chi opened this issue 1 year ago • 1 comments

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.

sor4chi avatar Sep 10 '24 08:09 sor4chi

I have some ideas. I'll share them later.

yusukebe avatar Sep 10 '24 08:09 yusukebe

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).

sor4chi avatar Jan 25 '25 06:01 sor4chi