Howard86 / next-api-handler

lightweight nextjs api handler wrapper, portable & configurable for serverless environment
https://next-api-handler.vercel.app
Other
50 stars 1 forks source link

Middleware object type inference #97

Open MutabaziAlleluia opened 1 year ago

MutabaziAlleluia commented 1 year ago

Is there a way that it can be handled? If not, I would suggest to add the ability of adding use in the builder constructor. That way the entire router can have access to the return type of each use middleware:

const router = new RouterBuilder({ use: [() => ...] });
MutabaziAlleluia commented 1 year ago

Currently I am handling it with (not so friendly when you have many routes and middlewares) hacks like this:

const abcMiddleware = async () => ({ abc: true });
type ABC = Awaited<ReturnType<typeof abcMiddleware>>;

router.use(abcMiddleware).get<{}, ABC>(
  async ( req, res ) => ({
    abc: req.middleware.abc,
  })
);
Howard86 commented 1 year ago

hmm this is a good feature that we should be able to use with use & inject think its doable with current implementation, let me have a think

Howard86 commented 1 year ago

@MutabaziAlleluia I tried a few ways to tweak with typescript definition, but still no lucks

Feel free to open a PR?