-
Notifications
You must be signed in to change notification settings - Fork 516
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
fix(hooks): enabled use of useAppConfig() and useRuntimeConfig() within request hooks #2483
base: main
Are you sure you want to change the base?
Conversation
Thanks for PR. Can you please link to a minimal reproduction please? ππΌ |
What about the changes in the tests? Specifically, That's pretty much as minimal of a reproduction as you can get. Restore I have this fairly minimal project of mine: Where I originally encountered this issue, but even that one is not quite as minimal as the test case changes. |
3e2c808
to
c39423d
Compare
β¦in request hooks If the nitro context isn't already initialized, it will be initialized, rather than error.
c39423d
to
009620c
Compare
@@ -20,7 +20,7 @@ declare module "h3" { | |||
captureError: CaptureError; | |||
} | |||
interface H3Context { | |||
nitro: { | |||
nitro?: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is already de facto the case, with or without this change.
Any Nitro plugins where builds break due to this change would be breaking the build because the plugin is now required to handle a (potential) problem when they access the "nitro" property. The behavior is not changed, just enforced on a TS level.
Any Nitro plugin that already handles the possibility of nitro being undefined when accessing it would have their builds keep working.
And any Nitro plugin that doesn't use the Nitro property isn't affected at all.
Any plugin that's currently relying on nitro already being instantiated is in fact just asserting it is, when in fact, it may not be, even today, if the code is triggered in a hook.
And Nitro itself handles it in all cases (now including also useRuntimeConfig(event)
and useAppConfig(event)
).
All of that said, I guess if there's a way to enforce Nitro's event handlers to use a context where "nitro" is not optional, while having hooks take in a context where "nitro" is optional would be best.
I'll see what I can do, and meanwhile, I amended the commit to init the errors array, as I didn't do that before.
β¦in request hooks If the nitro context isn't already initialized, it will be initialized, rather than error.
If the nitro context isn't already initialized, it will be initialized, rather than error.
π Linked issue
β Type of change
π Description
When calling
useAppConfig(event)
oruseRuntimeConfig(event)
from within a hook, the hook errors, because the "nitro" context option is not initialized yet at that time. It does get initialized later, making it available in middleware and route handlers. This PR extends support to include hooks as well, since although the nitro context option is not initialized, the event is available, so one would expect it to work.π Checklist