// 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.
Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.
Top comments (4)
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.
Nice article, btw you have a little typo in your Array types :)
I missed it. Thank you ;)