You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm proposing the ability to define and register custom helper methods that will be directly accessible on the Context object. This would enable developers to create reusable utility functions that have access to the context instance through this binding, improving code organization and developer experience.
Currently, utility functions need to be imported separately and explicitly passed the context, or set as variables on the context. Having first-class support for helper methods would make the code more elegant and maintainable.
Why is this feature valuable?
Improves code organization by allowing related functionality to be grouped as context methods
Reduces boilerplate by eliminating the need to repeatedly pass context to utility functions
Provides better TypeScript integration with proper this typing
Makes middleware and route handlers cleaner and more readable
Follows patterns common in other frameworks where context/request objects can be extended
Current Implementation vs Proposed Feature
Current Implementation:
typeEnv={Bindings: {SUPABASE_URL: string;SUPABASE_ANON_KEY: string;},Variables: {'supabase-token': stringsupabase: TypedSupabaseClientlog: (str: unknown)=>void}}constfactory=createFactory<Env>()exportconstRouter=factory.createAppexportconstcreateMiddleware=factory.createMiddlewareexporttypeContext=HonoContext<Env>// In route handlers:app.get('/users',async(c)=>{console.log(`${c.req.method}${c.req.path}: Fetching users`);// Commonly replaced with a log helper methodconstsupabase=c.get('supabase');const{ data, error }=awaitsupabase.from('users').select('*');// Also could be replaced with helper methodif(error){console.log(`${c.req.method}${c.req.path}: ${error}`);// Same herereturnc.json({error: 'Failed to fetch users'},500);}returnc.json(data);})
What is the feature you are proposing?
What is the feature you are proposing?
I'm proposing the ability to define and register custom helper methods that will be directly accessible on the Context object. This would enable developers to create reusable utility functions that have access to the context instance through
this
binding, improving code organization and developer experience.Currently, utility functions need to be imported separately and explicitly passed the context, or set as variables on the context. Having first-class support for helper methods would make the code more elegant and maintainable.
Why is this feature valuable?
this
typingCurrent Implementation vs Proposed Feature
Current Implementation:
Proposed Feature Implementation:
Possible Implementation
Working possible implementation (which I'm using myself)
The text was updated successfully, but these errors were encountered: