nextlove icon indicating copy to clipboard operation
nextlove copied to clipboard

Design Type Validation that supports new Vercel App Routes

Open seveibar opened this issue 2 years ago • 2 comments

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({ })
})

seveibar avatar Mar 24 '23 16:03 seveibar

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.

itelo avatar Mar 29 '23 23:03 itelo

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({ })

seveibar avatar Mar 29 '23 23:03 seveibar