hemerajs / hemera

🔬 Writing reliable & fault-tolerant microservices in Node.js https://hemerajs.github.io/hemera/
MIT License
806 stars 70 forks source link

Add specific middleware only for server method. #40

Closed StarpTech closed 7 years ago

StarpTech commented 7 years ago

It would be nice to have the chance to define middleware only for specific add

I prefer a chaining syntax instead parameter overloading.

hemera.add({
  topic: 'test',
  cmd: 'add'
}).use(function(req, resp, next) {
//process request
})
.end(function(req, cb) {
   cb()
})

Approach: Providing an interface to attach handlers to the actMeta object. When the add is called we could return an interface.

    return new Add(actMeta)
    /*
     Interface:
       use(<function)
       end(<function>)
     */

Those handlers can be executed in serial when a message arrived https://github.com/hemerajs/hemera/blob/master/packages/hemera/lib/index.js#L510.

StarpTech commented 7 years ago

I create a PR https://github.com/hemerajs/hemera/pull/42

Solution:

hemera.add({
  topic: 'test',
  cmd: 'add'
}).use(function(req, resp, next) {
//process request
})
.end(function(req, cb) {
   cb()
})

If you pass an error to the next function the request-response cycle is canceled and the error responded.