-
Notifications
You must be signed in to change notification settings - Fork 223
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: add event handler generics for typed request body and query #417
Conversation
Codecov Report
@@ Coverage Diff @@
## main #417 +/- ##
==========================================
+ Coverage 79.24% 79.61% +0.36%
==========================================
Files 27 27
Lines 2924 2977 +53
Branches 437 437
==========================================
+ Hits 2317 2370 +53
Misses 607 607
|
We have quickly talked with @danielroe about this. It is a really nice idea to support typed input (body, query, headers, etc) and output for the event handler interface. Only pending this future to first introduce a simple version of object syntax to see if there are any alternative ideas for defining generics (such as infering io types from middleware) and avoiding breaking changes. |
I don't see why this change shouldn't be compatible with future object syntax. The generic would simply point to a different property of the object. (Nothing about how this is written should forestall inferring type.) Having said that, I'm happy to defer this, but would love to see forward movement on typed input if possible. |
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.
Could you precise how end users will use this ? Unlike validation, this type can't really be trusted. What does this unblock for $fetch / useFetch exactly ?
This allows us to granularly specify upstream and downstream types. So This PR also implements downstream types for export default defineEventHandler<{ body: CustomBody }>(event => {
const body = await readBody(event)
// body is typed
}) (However, the main point is to allow upstream typing of calling You are right that this is unsafe but it is currently typed as As I've said, the fact that users can specify types for body without validating them is still better than the current implementation - and additionally provides the safety of enforcing types via internal calls via |
Thanks for the explanation ! For reference I'll tag #431 for runtime validation support |
@danielroe Can you please verify my latest changes? 🙏🏼 |
resolves #129
this adds support explicit typing of input for event handlers, which will unlock autocomplete when consumed in
$fetch
down the line (in nitro).I've kept the default return types of
any
intact for backwards compatibility, and also added backwards compatibility for existing generic order, but I would advise reconsidering this (see #387).