Here is what I’m using – not sure if this could be considered “best practices” when it comes to using the spread operator – I’d be interested to know?
export const AuthMixin = function (methodOptions) {
const runFunc = methodOptions.run;
methodOptions.run = function run(...args) {
// Access the original args with args[0]
const myDocId = args[0].docId;
// Append our function to the original
return runFunc.apply(this, args);
});
// Return updated options
return methodOptions;
};
But now that I see how to do that, I’m asking myself if it’s not more easy and readable to call those functions inside the run part of the method. Semantically it’s true that is not part of run but part of validate
What do you think both? What can be the advantage to call those validation in mixin part?
You can, but then you will have dozens and dozens of methods with duplicated code.
In the case of mixins, you create this function, put it in a file, and just request it when you need it. And if you need to change it it’s easier, because it’s in one single place.
I can have a file with my mixin, request it when I need it, change it in one place and it’s not going to duplicate any code.
In fact the mixin will be more readable because I can pass the args I want without all the code you give previously…
Maybe I’m missing something, but I don’t see anymore any advantages in using validatedMethods mixins