8000 Service method call normalization by daffl · Pull Request #124 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/mixins/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
'use strict';

var _ = require('lodash');

module.exports = function() {
return [
var mixins = [
require('./promise'),
require('./event')
require('./event'),
require('./normalizer')
];

// Override push to make sure that normalize is always the last
mixins.push = function() {
var args = [ this.length - 1, 0].concat(_.toArray(arguments));
this.splice.apply(this, args);
return this.length;
};

return mixins;
};
19 changes: 19 additions & 0 deletions lib/mixins/normalizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var _ = require('lodash');
var getArguments = require('feathers-commons').getArguments;

module.exports = function (service) {
if (typeof service.mixin === 'function') {
var mixin = {};

_.each(this.methods, function(method) {
if(typeof service[method] === 'function') {
mixin[method] = function() {
var args = getArguments(method, arguments);
return this._super.apply(this, args);
};
}
});

service.mixin(mixin);
}
};
33 changes: 17 additions & 16 deletions lib/mixins/promise.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
'use strict';

var _ = require('lodash');
var makeWrapper = function() {
return function() {
var result = this._super.apply(this, arguments);
var callback = arguments[arguments.length - 1];

if(typeof result !== 'undefined' && _.isFunction(result.then) && _.isFunction(callback)) {
result.then(function(data) {
callback(null, data);
}, function(error) {
callback(error);
});
}
return result;
};
var wrapper = function () {
var result = this._super.apply(this, arguments);
var callback = arguments[arguments.length - 1];

if(typeof result !== 'undefined' && _.isFunction(result.then) && _.isFunction(callback)) {
result.then(function(data) {
callback(null, data);
}, function(error) {
callback(error);
});
}
return result;
};

module.exports = function (service) {
if (typeof service.mixin === 'function') {
var mixin = _.transform(_.pick(service, this.methods), function(result, value, key) {
if(typeof value === 'function') {
result[key] = makeWrapper();
var mixin = {};

_.each(this.methods, function(method) {
if(typeof service[method] === 'function') {
mixin[method] = wrapper;
}
});

Expand Down
4 changes: 2 additions & 2 deletions test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ describe('Feathers application', function () {
it('mixins are unique to one application', function() {
var app = feathers();
app.mixins.push(function() {});
assert.equal(app.mixins.length, 3);
assert.equal(app.mixins.length, 4);

var otherApp = feathers();
otherApp.mixins.push(function() {});
assert.equal(otherApp.mixins.length, 3);
assert.equal(otherApp.mixins.length, 4);
});
});
63 changes: 63 additions & 0 deletions test/mixins/normalizer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

var assert = require('assert');
var Proto = require('uberproto');
var normalizer = require('../../lib/mixins/normalizer');
var mixins = require('../../lib/mixins');

describe('Argument normalizer mixin', function () {
it('normalizer mixin is always the last to run', function() {
var arr = mixins();
var dummy = function() { };

assert.equal(arr.length, 3);

arr.push(dummy);

assert.equal(arr[arr.length - 1], normalizer, 'Last mixin is still the normalizer');
assert.equal(arr[arr.length - 2], dummy, 'Dummy got added before last');
});

// The normalization is already tests in all variations in `getArguments`
// so we just so we only test two random samples

it('normalizes .find without a callback', function (done) {
var context = {
methods: ['find']
};
var FixtureService = Proto.extend({
find: function(params, callback) {
assert.ok(typeof callback === 'function');
assert.equal(params.test, 'Here');
done();
}
});

normalizer.call(context, FixtureService);

var instance = Proto.create.call(FixtureService);

instance.find({ test: 'Here' });
});

it('normalizes .update without params and callback', function (done) {
var context = {
methods: ['update']
};
var FixtureService = Proto.extend({
update: function(id, data, params, callback) {
assert.equal(id, 1);
assert.ok(typeof callback === 'function');
assert.deepEqual(data, { test: 'Here' });
assert.deepEqual(params, {});
done();
}
});

normalizer.call(context, FixtureService);

var instance = Proto.create.call(FixtureService);

instance.update(1, { test: 'Here' });
});
});
0