// pages/api/users/[id].tsexportdefaulthandleMethods({id:string}).get<Array<User>>(async(req,res)=>{try{// you can access query.id type safely.constresult=awaitfindUsers({id:req.query.id})returnres.json({result:true,data:result})}catch(e){returnres.status(400).json({result:false,data:e})}}).prepare()
Next.js NextApiHandler can define only one response body type.
So if a route has multiple methods and response body types, you will use the Type Assertion and you will need to write many codes more.
This utility method can decrease codes a little.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Latest comments (4)
Nice article, btw you have a little typo in your Array types :)
I missed it. Thank you ;)
This is good,
But don't you think you have made very simple thing into a very complicated one?
Next.js NextApiHandler can define only one response body type.
So if a route has multiple methods and response body types, you will use the Type Assertion and you will need to write many codes more.
This utility method can decrease codes a little.