Typescript API router is weird
Answered
Rhinelander posted this in #help-forum
RhinelanderOP
First approach
Second approach
Third approach
Fourth approach
I've seen people use bunch of diffrent syntax don't know what is old what is new what should i use.... Could somebody explain which approach is best for latest next.js app router. The 4th with combination of 3rd one i am guessing but could somebody clear this up for me
export default async function handler(
req: request,
res: response,
) {}Second approach
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {}Third approach
export default async function handler(
req: NextRequest,
res: NextResponse,
) {}Fourth approach
export default async function POST / GET / etc...(
...
) {}I've seen people use bunch of diffrent syntax don't know what is old what is new what should i use.... Could somebody explain which approach is best for latest next.js app router. The 4th with combination of 3rd one i am guessing but could somebody clear this up for me
Answered by B33fb0n3
next supports the native request and response objects (that you mentioned) and they are extended as NextRequest and NextResponse to provide convenient helper for advance use cases
6 Replies
@Rhinelander **First approach**
typescript
export default async function handler(
req: request,
res: response,
) {}
**Second approach**
typescript
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {}
**Third approach**
typescript
export default async function handler(
req: NextRequest,
res: NextResponse,
) {}
**Fourth approach**
typescript
export default async function POST / GET / etc...(
...
) {}
I've seen people use bunch of diffrent syntax don't know what is old what is new what should i use.... Could somebody explain which approach is best for latest next.js app router. The 4th with combination of 3rd one i am guessing but could somebody clear this up for me
next supports the native request and response objects (that you mentioned) and they are extended as NextRequest and NextResponse to provide convenient helper for advance use cases
Answer
RhinelanderOP
Okay that makes sense but are those native ones same as in express?
the native ones are:
Request: https://developer.mozilla.org/en-US/docs/Web/API/Request
Response: https://developer.mozilla.org/en-US/docs/Web/API/Response
Request: https://developer.mozilla.org/en-US/docs/Web/API/Request
Response: https://developer.mozilla.org/en-US/docs/Web/API/Response
RhinelanderOP
Thank you
happy to help