-
-
Notifications
You must be signed in to change notification settings - Fork 641
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
feat(context): introduce c.var
#1406
Conversation
Hi @usualoma , @ThatOneBro , and others. What do you think about this feature? I think this is good because it will achieve big things with minimal change of the APIs. |
I think this makes a lot of sense overall. 👍 LGTM |
With this commit, I've set the properties to read-only. This is to prevent confusion, emphasizing that users should exclusively use |
I think there is a large gap in documentation/public knowledge on how to use honojs with cloudflare durable objects in the sense that there is no way shown how to access durable object storage in the route handlers. Would the c.set() and c.text(c.var.echo('Hello!')) solution be good to store and access durable object storage in routes? |
This PR introduces two new features: the ability to set variables using
Middleware
generics and accessing them viac.var
.Currently, you can globally define variable types using generics for
Hono
.Alternatively, you can specify them for a specific middleware using
ContextVariableMap
:The challenge here is that using
ContextVariableMap
makes it globally available. For instance, after importing the JWT middleware, you can accessjwtPayload
in any handler.With this PR, you can confine middleware-defined variables to specific handlers where the middleware is applied.
This offers a scoped approach to variable type availability.
The second feature is
c.var
. While the conventional method to access the actual value of a variable isc.get()
:With
c.var
, you can use a more intuitive syntax:The combination of these two features will provide a powerful tool for developers:
This was discussed in issue #414 and is now being realized.