Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining multiple middleware with compose #21

Open
bertho-zero opened this issue Feb 5, 2020 · 1 comment
Open

Combining multiple middleware with compose #21

bertho-zero opened this issue Feb 5, 2020 · 1 comment

Comments

@bertho-zero
Copy link
Collaborator

bertho-zero commented Feb 5, 2020

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
Copy link
Collaborator Author

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();
    }
  ]
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant