blomqma / next-rest-framework

Type-safe, self-documenting APIs for Next.js
https://next-rest-framework.vercel.app
Other
136 stars 18 forks source link

Share data across middlewares #111

Closed crummy closed 8 months ago

crummy commented 9 months ago

Hi - thanks for the great project! I'm looking at migrating from express-zod-api which lets us share data across middlewares, by returning values from the middleware. Here's how I imagine it looking in next-rest-framework:

      .middleware((req, res) => {
        return { foo: "bar" }
      })
      .middleware((req, res, options) => {
        return { ...options, fooTwice: options.bar + options.bar}
      })
      .handler((req, res, options) => {
        console.log(options.foo)
        console.log(options.fooTwice)
        res.status(200).json(TODOS);
      })

Here's express-zod-api's documentation on it: https://github.com/RobinTail/express-zod-api?tab=readme-ov-file#middlewares

Perhaps it's already possible to do this with next-rest-framework and I'm missing something?

blomqma commented 8 months ago

This can be done, but probably requires the amount of maximum middlewares to be set to some reasonable single digit number with the current implementation.

blomqma commented 8 months ago

An update on this: v4.2.0 adds support for chaining up to three middlewares together and sharing data between the middlewares in the way described in your example.

crummy commented 8 months ago

Thanks so much!