-
Notifications
You must be signed in to change notification settings - Fork 539
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
Proposal: defineValidatedEventHandler for typed body/query $fetch options #2244
Comments
I'd love to work on this. I just wanted to make sure that the concept sounds acceptable and does not conflict with any of the existing plans for Nitro. @pi0 |
Hi dear @DallasHoff thanks for sharing idea and proposal. I think we want to introduce it as an option for |
@pi0 Okay, so you would favor the API to be a new overload of export default defineEventHandler({
bodyValidator: bodySchema.parse,
queryValidator: querySchema.parse,
handler: async (event) => {}
}); Does the general idea seem sound otherwise? |
@pi0 I went ahead and opened a draft pull request for the h3 half of the changes. |
Draft PRs for both nitro and h3 are now up. The functionality is there for initial feedback; then I can work on adding additional tests and documentation. h3 PR: unjs/h3#742 |
Let's track via unjs/h3#742 / unjs/h3#496 |
@pi0 could you edit the link? The autolink from GitHub points to an unrelated nitro PR. The post of @DallasHoff contains the right one, but I will not be the only one at first confused when clicking on the link in your comment. |
Describe the feature
Currently, the type of the
method
option in$fetch
can be inferred from the server files present in the project. I would like to propose a way to infer the types forbody
andquery
for routes that use a newdefineValidatedEventHandler
function, implemented using similar techniques to the type inference formethod
. Here is roughly how I think it could work.defineValidatedEventHandler
is similar to h3'sgetValidatedQuery
andreadValidatedBody
functions in that you pass a validation function (such as theparse
method of a Zod schema) that it will then run to validate the input and infer the type.defineValidatedEventHandler
would accept a function forquery
and/orbody
in an object before thehandler
argument.defineValidatedEventHandler
would then use the validation functions to populate the generic ofdefineEventHandler
.This would open the door for Nitro to extract the types for
body
andquery
from the event handler and use them in theNitroFetchOptions
type alongside themethod
type it already has.This would probably involve generating a second interface in
nitro-routes.d.ts
alongsideInternalApi
to get the type fromdefineValidatedEventHandler
.With that, Nitro can create a type similar to the existing
AvailableRouterMethod
that extracts the types forbody
andquery
. Something along the lines of this with some extra typeextends
checks:With this all in place, the goal would be to have type inference for
body
andquery
in the options of$fetch
anduseFetch
when calling any API route that usesdefineValidatedEventHandler
.This completes the full circle of type integration between the Nitro API and the consumer by allowing both the inputs and outputs of fetches to be typed. Please let me know what you think.
Additional information
The text was updated successfully, but these errors were encountered: