blomqma / next-rest-framework

Type-safe, self-documenting APIs for Next.js
https://next-rest-framework.vercel.app
Other
134 stars 17 forks source link

TypedNextRequest not exported / no longer castable to NextRequest in v6 #153

Closed jameslshannon closed 3 months ago

jameslshannon commented 4 months ago

Just made the switch to v6 beta and another breaking change is that I can no longer cast TypedNextRequest to a NextRequest in order to pass it to other functions from a handler (for logging, etc.):

Conversion of type 'TypedNextRequest<"POST", "application/json", { name: string; }, BaseQuery>' to type 'NextRequest' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Types of property 'nextUrl' are incompatible.
    Type 'Modify<NextURL, { searchParams: Modify<URLSearchParams, { get: (key: string) => string | null; getAll: (key: string) => string[]; }>; }>' is missing the following properties from type 'NextURL': analyze, formatPathname, formatSearch, [Internal]

However, an even better solution would be to export TypedNextRequest so that it could be passed as-is to other functions from a handler and retain type safety/autocomplete, etc..

Thanks!

austinkelleher commented 4 months ago

We have a nearly identical use case that @jameslshannon described (thanks for writing up the issue btw). We are wrapping our handlers in a function that leverages req.nextUrl and now it's undefined causing a runtime error.

Any suggestions @blomqma? Thanks in advance and thanks for the awesome lib! 🚀

blomqma commented 4 months ago

Yeah exporting the types for TypedNextRequest makes sense 👍🏻 The incompability of TypedNextRequest with NextRequest is a regression introduced in the v6 beta version, I will try to fix it ASAP.

blomqma commented 3 months ago

This should be fixed in v6.0.0, the types for TypedNextRequest, TypedNextApiRequest and TypedNextApiResponse are also now exported from the bundle.