hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.62k stars 65 forks source link

Express middlewares in route handlers (support passport in app router) #228

Open eric-burel opened 1 year ago

eric-burel commented 1 year ago

Hi, If I understand correctly, so far the Express wrapper only targets API routes, not route handlers. This is a tad more complicated to setup because route handlers use a different structure for the request/response (the web platform ones, built-in node/browser and used by fetch) than Express.

Can you please confirm me that this is not possible in next-connect right now?

In the meantime here is what I've stated to work on to bypass this issue: https://github.com/Devographics/Monorepo/blob/develop/surveyform/src/app/api/nespresso.ts

The API is like so:

export function POST(req: NextRequst) {
   return nespresso(
    // a normal Express middleware
     (req, res, next) => { next() },
    (req, res, next) => { res.send("foobar") }
   ) // returns  Next Response
}

Feel free to reuse this code if that sounds relevant to you. The typings are giving me a hard time (somehow "res.status" is not found, and "Handler" type is not accepted as a middleware) and features are missing but it roughly works otherwise.

After a few more experiments, I fail to properly create an Express response/request directly from Express codebase, this forces me to mimick all features but then writing the request seems to fail.

Setting relevant methods by hand, it mostly works however somehow passport can't set "res.user", I can't tell why, while it can set "_passport". I don't have a minimal repro yet.