node.jsmoleculer

How to properly detect mixin action in middlewares


I use custom mixins, which have internal actions.

I use middlewares over my own actions.

But actions of mixins fall into middlewares.

Need to use some duck typing check for ignoring mixin actions in middlewares.

Example for moleculer-io:

if (action.name === 'io.call') {
  return next(ctx);
}

Is exist more safe and robust way to check mixin action in middleware?


Solution

  • The better way is that using a custom property in action definition and checking it in middleware.

    In mixins:

    actions: {
        find: {
            myFeature: true,
            handler(ctx) {}
        }   
    }
    

    In middleware:

    {
        localCall(next, action) {
            if (action.myFeature) {
                // ...
            }
    
            return next;
        }
    }