[VlidatedMethod] How to get method args in mixin

Hi!

I’m trying to make a mixin but I need the args passed to the original methods.
How can I get these?

Thanks.

export const isRegistered = new ValidatedMethod({
  name: 'sessionDate.isRegistered',
  mixins : [myMixin],
  validate: new SimpleSchema({
    sessionDateID: { type: String },
    userID: { type: String }
  }).validator({ clean: true, filter: false }),
  run({ sessionDateID, userID }) {
    return SessionDates.find({  _id: sessionDateID,  "players.userID": userID }).count() > 0 ? true : false;
  }
});

function myMixin(methodOptions) {
 // How to get sessionDateID and userID ???
  return methodOptions;
}

I was just trying to figure this out yesterday. Borrowed some of the pattern from Mdg:validated-method mixin .call or ._execute

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;
};

Here it is an example:

export function loggedIn(currentMethod) {
  const method = currentMethod;
  const originalFunc = method.run;

  method.run = function (params) {
    if (this.connection && !this.userId) {
      throw new Meteor.Error('not-logged-in', 'You must be logged in.');
    }
    return originalFunc.call(this, params);
  };

  return method;
}
2 Likes

Thanks both of you!

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 :thinking:

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.

1 Like

Yeah but if I made those mixins but I call them directly in the run part ?
Like this:

export const isRegistered = new ValidatedMethod({
  name: 'sessionDate.isRegistered',
  //mixins : [myMixin], // instead of here
  validate: new SimpleSchema({
    sessionDateID: { type: String },
    userID: { type: String }
  }).validator({ clean: true, filter: false }),
  run({ sessionDateID, userID }) {
    myMixin(myParams); //here
    return SessionDates.find({  _id: sessionDateID,  "players.userID": userID }).count() > 0 ? true : false;
  }
});

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 :thinking: