Design Type Validation that supports new Vercel App Routes
New Vercel App endpoints don't support res
Could this also help with method-discriminated validation more generally?
export const GET = withRouteSpec.get({
queryParams: z.object({ })
} as const, async (req) => {
return Response(200, JSON.stringify("{}"))
// return NextApiResponse.json({ })
})
Based on the output of the command: grep -r methods pages/ > /tmp/methods.txt, I believe the withRouteSpec should have the methods:
get
post
put
patch
delete
getAndPost
postAndPut
postAndPatch
postAndDelete
We may want to exclude the postAndPut because only one route uses it.
Also, it would be good to add some jsdoc to explain why the methods like getAndPost are helpful (I only understand why we do this after @seveibar explained it in a slack thread for someone else)
Furthermore, we should have a custom method that accepts the method key inside the route_spec to be more future-proof and handle edge cases.
Nice analysis.
As an alternative to "custom", in typescript, it's possible to have a method that has properties like this:
interface myRouteSpec {
get: ({ }) => {};
({ methods: string[] }) => {}
}
// usage
myRouteSpec({ methods: ["GET"] })
myRouteSpec.get({ })