Skip to content

Commit

Permalink
Modernize and remove underscore (#83)
Browse files Browse the repository at this point in the history
Adaptation of #82 with some fixes and enhancements.
  • Loading branch information
sebakerckhof authored and abernix committed Sep 20, 2018
1 parent a3d7d1f commit 1ebb407
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
13 changes: 6 additions & 7 deletions package.js
@@ -1,33 +1,32 @@
Package.describe({
name: 'mdg:validated-method',
summary: 'A simple wrapper for Meteor.methods',
version: '1.1.0',
version: '1.2.0',
documentation: 'README.md',
});

Package.onUse(function (api) {
api.versionsFrom('1.2');
api.versionsFrom('1.7');

api.use([
'ecmascript',
'check',
'ddp',
'underscore',
]);

api.addFiles('validated-method.js');
api.mainModule('validated-method.js');
api.export('ValidatedMethod');
});

Package.onTest(function (api) {
api.use([
'ecmascript',
'practicalmeteor:mocha@2.1.0_5',
'practicalmeteor:mocha@2.4.5_6',
'practicalmeteor:chai@2.1.0_1',
'aldeed:simple-schema@1.4.0',
'aldeed:simple-schema@1.5.4',
'mdg:validated-method',
'random'
]);

api.addFiles('validated-method-tests.js');
api.mainModule('validated-method-tests.js');
});
7 changes: 6 additions & 1 deletion validated-method-tests.js
@@ -1,3 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { assert } from 'meteor/practicalmeteor:chai';

const plainMethod = new ValidatedMethod({
name: 'plainMethod',
validate: new SimpleSchema({}).validator(),
Expand Down Expand Up @@ -144,7 +149,7 @@ describe('mdg:method', () => {
assert.throws(() => {
new ValidatedMethod({
name: 'methodWithFaultySchemaMixin',
mixins: [function (args) { return args}, function () {}],
mixins: [args => args, function () {}],
schema: null,
run() {
return 'result';
Expand Down
23 changes: 11 additions & 12 deletions validated-method.js
@@ -1,6 +1,6 @@
/* global ValidatedMethod:true */
import { check, Match } from 'meteor/check';

ValidatedMethod = class ValidatedMethod {
export class ValidatedMethod {
constructor(options) {
// Default to no mixins
options.mixins = options.mixins || [];
Expand Down Expand Up @@ -39,10 +39,13 @@ ValidatedMethod = class ValidatedMethod {
throwStubExceptions: true,
};

options.applyOptions = _.extend({}, defaultApplyOptions, options.applyOptions);
options.applyOptions = {
...defaultApplyOptions,
...options.applyOptions
};

// Attach all options to the ValidatedMethod instance
_.extend(this, options);
Object.assign(this, options);

const method = this;
this.connection.methods({
Expand All @@ -58,7 +61,7 @@ ValidatedMethod = class ValidatedMethod {

call(args, callback) {
// Accept calling with just a callback
if (_.isFunction(args)) {
if ( typeof args === 'function' ) {
callback = args;
args = {};
}
Expand All @@ -77,9 +80,7 @@ ValidatedMethod = class ValidatedMethod {
}
}

_execute(methodInvocation, args) {
methodInvocation = methodInvocation || {};

_execute(methodInvocation = {}, args) {
// Add `this.name` to reference the Method name
methodInvocation.name = this.name;

Expand All @@ -96,12 +97,10 @@ perhaps you meant to throw an error?`);

// Mixins get a chance to transform the arguments before they are passed to the actual Method
function applyMixins(args, mixins) {
// You can pass nested arrays so that people can ship mixin packs
const flatMixins = _.flatten(mixins);
// Save name of the method here, so we can attach it to potential error messages
const {name} = args;
const { name } = args;

flatMixins.forEach((mixin) => {
mixins.forEach((mixin) => {
args = mixin(args);

if(!Match.test(args, Object)) {
Expand Down

0 comments on commit 1ebb407

Please sign in to comment.