-
Notifications
You must be signed in to change notification settings - Fork 508
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 generation of route input types #892
Conversation
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.
What about using a generic for defineEventHandler
? We could have:
export default defineEventHandler<{ id: string }>(event => {
const data = await readBody(event)
// data is typed
})
This would need to be implemented in https://github.com/unjs/h3 (with backwards-compatible generic implementation), and then this PR could be (very slightly) updated to use the generic instead of exported Input
type.
This would be ideal, but how would we access the generic type through the type of the exported eventHandler? |
We can use a query along the lines of |
Hello, My thought is that it would be nice if we would expose a function eg- export const parseBody = async (event) => {
const data = await readBody(event) // type of data is unknown
const parsedData = parse(data) // do validation, use joi, yup, zod or manual
// now parsed data has a type. Let's say : Input - It will be inferred
return parsedData
}
export default defineEventHandler(event => {
const parsedData = await readBody(event)
// data is typed:`Input` and parsed
}) From this there will be one more benefit: we can assign type to the
|
This was exactly my thinking, but I wanted to leave that up to the developer to decide if they want to validate the request body. A helper could easily be built around my original idea: // defineZodRoute.ts (a custom file which should probably not be included in nitro)
import { z } from "zod";
function defineZodRoute<TInput, TOutput, TResponse>(
input: z.ZodType<TOutput, z.ZodTypeDef, TInput>,
resolver: (input: TOutput) => TResponse
) {
return defineEventHandler(async (event) => {
const body = await readBody(event);
const parsedInput = input.safeParse(body);
if (parsedInput.success) {
return resolver(parsedInput.data);
} else {
return parsedInput.error;
}
});
}
export default defineZodRoute; // routes/index.ts
import { z } from "zod";
import defineZodRoute from "../lib/defineZodRoute";
const validator = z.object({ greeting: z.string() });
export default defineZodRoute(validator, (input) => {
return {
hello: input.greeting,
};
});
export type Input = z.input<typeof validator>; |
@Blechlawine |
Hi, dear @Blechlawine. Thanks for the nice write-up in #841 and this PR. New typed events handlers in unjs/h3#417 will help to implement auto-type generation. First step is #1532 I am closing this one for maintenance but thanks again β€οΈ |
π Linked issue
#841
β Type of change
π Description
Add generated types for inputs to routes. A more detailed explanation is in the issue #841.
Resolves #841
π Checklist