// Method definition
const method = new ValidatedMethod({
name, // DDP method name
mixins, // Method extensions
validate, // argument validation
run // Method body
});
// Method call
method.call({ arg1, arg2 });
This is a simple wrapper package for Meteor.methods
. The need for such a package came
when the Meteor Guide was being written and we realized there was a lot of best-practices
boilerplate around methods that could be easily abstracted away.
- Have an object that represents your method. Refer to it through JavaScript scope rather than by a magic string name.
- Built-in validation of arguments through
aldeed:simple-schema
, or roll your own argument validation. - Easily call your method from tests or server-side code, passing in any user ID you want. No need for two-tiered methods anymore!
- Throw errors from the client-side method simulation to prevent execution of the server-side method - this means you can do complex client-side validation in the body on the client, and not waste server-side resources.
- Get the return value of the stub by default, to take advantage of consistent ID generation. This way you can implement a custom insert method with optimistic UI.
- Install Method extensions via mixins.
See extensive code samples in the Todos example app.
Let's examine a method from the new Todos example app which makes a list private and takes the listId
as an argument. The method also does permissions checks based on the currently logged-in user. Note this code uses new ES2015 JavaScript syntax features.
// Attach your method to a namespace
Lists.methods.makePrivate = new ValidatedMethod({
// The name of the method, sent over the wire. Same as the key provided
// when calling Meteor.methods
name: 'Lists.methods.makePrivate',
// Validation function for the arguments. Only keyword arguments are accepted,
// so the arguments are an object rather than an array. The SimpleSchema validator
// throws a ValidationError from the mdg:validation-error package if the args don't
// match the schema
validate: new SimpleSchema({
listId: { type: String }
}).validator(),
// This is the body of the method. Use ES2015 object destructuring to get
// the keyword arguments
run({ listId }) {
// `this` is the same method invocation object you normally get inside
// Meteor.methods
if (!this.userId) {
// Throw errors with a specific error code
throw new Meteor.Error('Lists.methods.makePrivate.notLoggedIn',
'Must be logged in to make private lists.');
}
const list = Lists.findOne(listId);
if (list.isLastPublicList()) {
throw new Meteor.Error('Lists.methods.makePrivate.lastPublicList',
'Cannot make the last public list private.');
}
Lists.update(listId, {
$set: { userId: this.userId }
});
Lists.userIdDenormalizer.set(listId, this.userId);
}
});
The validator
function called in the example requires SimpleSchema version 1.4+.
If aldeed:simple-schema
doesn't work for your validation needs, just define a custom validate
method that throws a ValidationError
instead:
const method = new ValidatedMethod({
name: 'methodName',
validate({ myArgument }) {
const errors = [];
if (myArgument % 2 !== 0) {
errors.push({
name: 'myArgument',
type: 'not-even',
details: {
value: myArgument
}
});
}
if (errors.length) {
throw new ValidationError(errors);
}
},
...
});
You can use check
in your validate function if you don't want to pass ValidationError
objects to the client, like so:
const method = new ValidatedMethod({
name: 'methodName',
validate(args) {
check(args, {
myArgument: String
});
},
...
});
If your method does not need argument validation, perhaps because it does not take any arguments, you can use validate: null
to skip argument validation.
You can define a method on a non-default DDP connection by passing an extra connection
option to the constructor.
If you want to keep some of your method code secret on the server, check out Served Files from the Meteor Guide.
Call a method like so:
Lists.methods.makePrivate.call({
listId: list._id
}, (err, res) => {
if (err) {
handleError(err.error);
}
doSomethingWithResult(res);
});
The return value of the server-side method is available as the second argument of the method callback.
Call this from your test code to simulate calling a method on behalf of a particular user:
it('only makes the list public if you made it private', () => {
// Set up method arguments and context
const context = { userId };
const args = { listId };
Lists.methods.makePrivate._execute(context, args);
const otherUserContext = { userId: Random.id() };
assert.throws(() => {
Lists.methods.makePublic._execute(otherUserContext, args);
}, Meteor.Error, /Lists.methods.makePublic.accessDenied/);
// Make sure things are still private
assertListAndTodoArePrivate();
});
Every ValidatedMethod
can optionally take an array of mixins. A mixin is simply a function that takes the options argument from the constructor, and returns a new object of options. For example, a mixin that enables a schema
property and fills in validate
for you would look like this:
function schemaMixin(methodOptions) {
methodOptions.validate = methodOptions.schema.validator();
return methodOptions;
}
Then, you could use it like this:
const methodWithSchemaMixin = new ValidatedMethod({
name: 'methodWithSchemaMixin',
mixins: [schemaMixin],
schema: new SimpleSchema({
int: { type: Number },
string: { type: String },
}),
run() {
return 'result';
}
});
If you write a helpful ValidatedMethod
mixin, please file an issue so that it can be listed here!
- It could be nice to have a
SimpleSchemaMethod
which just lets you specify aschema
option rather than having to pass avalidator
function into thevalidate
option. This would enable the below. - With a little bit of work, this package could be improved to allow easily generating a form from a method, based on the schema of the arguments it takes. We just need a way of specifying some of the arguments programmatically - for example, if you want to make a form to add a comment to a post, you need to pass the post ID somehow - you don't want to just have a text field called "Post ID".
By default, using Meteor.call
to call a Meteor method invokes the client-side simulation and the server-side implementation. If the simulation fails or throws an error, the server-side implementation happens anyway. However, we believe that it is likely that an error in the simulation is a good indicator that an error will happen on the server as well. For example, if there is a validation error in the arguments, or the user doesn't have adequate permissions to call that method, it's often easy to identify that ahead of time on the client.
If you already know the method will fail, why call it on the server at all? That's why this package turns on a hidden option to Meteor.apply
called throwStubExceptions
.
With this option enabled, an error thrown by the client simulation will stop the server-side method from being called at all.
Watch out - while this behavior is good for conserving server resources in the case where you know the call will fail, you need to make sure the simulation doesn't throw errors in the case where the server call would have succeeded. This means that if you have some permission logic that relies on data only available on the server, you should wrap it in an if (!this.isSimulation) { ... }
statement.
One big benefit of the built-in client-side Collection#insert
call is that you can get the ID of
the newly inserted document on the client right away. This is sometimes listed as a benefit of
using allow/deny over custom defined methods. Not anymore!
For a while now, Meteor has had a hard-to-find option to Meteor.apply
called returnStubValue
. This lets you return a value from a client-side simulation, and use that value immediately on the client. Also, Meteor goes to great lengths to make sure that ID generation on the client and server is consistent. Now, it's easy to take advantage of this feature since this package enables returnStubValue
by default.
Here's an example of how you could implement a custom insert method, taken from the Todos example app we are working on for the Meteor Guide:
Lists.methods.insert = new ValidatedMethod({
name: 'Lists.methods.insert',
validate: new SimpleSchema({}).validator(),
run() {
return Lists.insert({});
}
});
You can get the ID generated by insert
by reading the return value of call
:
// The return value of the stub is an ID generated on the client
const listId = Lists.methods.insert.call((err) => {
if (err) {
// At this point, we have already redirected to the new list page, but
// for some reason the list didn't get created. This should almost never
// happen, but it's good to handle it anyway.
FlowRouter.go('home');
alert('Could not create list.');
}
});
FlowRouter.go('listsShow', { _id: listId });
meteor test-packages --driver-package practicalmeteor:mocha ./