senecajs / seneca

A microservices toolkit for Node.js.
http://senecajs.org
MIT License
3.96k stars 311 forks source link

A single function that will run before all plugins and patterns #871

Open guzon56 opened 3 years ago

guzon56 commented 3 years ago

Hi, I am doing an educational project for myself to explorer microservices. I have to say that seneca is very nice solution but I came to an issue which is: I have several plugins in on of the services and I want for all plugins and patterns to write a single function that will run before each pattern, but I couldn't find a way.

is it possible? if do... how? Thanks, Harel Grozinger.

rjrodger commented 3 years ago

Seneca.wrap could do this by adding a wrapper pattern to each existing pattern.

However, if your function is "pure" the easiest solution is to use an ordinary utility function that returns the actual action function:

function add_foo(action) {
  return function(msg, reply) {
     msg.foo = 'bar'
     return action(msg, reply)
  }
}

seneca.add('my:pattern', add_foo(my_action))
rjrodger commented 3 years ago

Making this easier as a convenience is noted as a feature for the next release! Thank you