hooks icon indicating copy to clipboard operation
hooks copied to clipboard

Combining multiple middleware with compose

Open bertho-zero opened this issue 6 years ago • 1 comments

This part is not documented on @feathersjs/hooks: https://github.com/koajs/koa/blob/master/docs/guide.md#combining-multiple-middleware-with-koa-compose

const { hooks, compose } = require('@feathersjs/hooks');

hooks(Hello, {
  sayHi: [
    compose([
      logRuntime,
      validateName,
      compose([
        /* ... */
      ])
    ])
  ]
});

Try it: https://runkit.com/embed/l7srsn9s0s2q

bertho-zero avatar Feb 05 '20 08:02 bertho-zero

It can also be useful to call a chain of hooks inside a hook:

hooks(Hello, {
  sayHi: [
    async (ctx, next) => {
      // ... stuff

      await compose([
        logRuntime,
        validateName,
        compose([
          /* ... */
        ])
      ])(ctx);

      // ... other stuff

      await next();
    }
  ]
});

bertho-zero avatar Apr 07 '20 16:04 bertho-zero