Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit f586c29

Browse files
committed
adding the ability to define multiple hooks in an array. Refs #2
1 parent 68cccb6 commit f586c29

File tree

4 files changed

+190
-82
lines changed

4 files changed

+190
-82
lines changed

lib/after.js

+64-46
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var _ = require('lodash');
22
var utils = require('./utils');
33

44
module.exports = function(service) {
5-
if(typeof service.mixin !== 'function') {
5+
if(!_.isFunction(service.mixin)) {
66
return;
77
}
88

@@ -11,61 +11,79 @@ module.exports = function(service) {
1111

1212
service.mixin({
1313
after: function(obj) {
14-
var mixin = _.transform(obj, function(result, hook, name) {
14+
var mixin = _.transform(obj, function(result, hooks, name) {
1515
// Don't mix in if it is not a service method
1616
if(app.methods.indexOf(name) === -1) {
1717
return;
18+
}
19+
20+
if (!_.isArray(hooks)){
21+
hooks = [hooks];
1822
}
1923

20-
result[name] = function() {
21-
var self = this;
22-
var args = _.toArray(arguments);
23-
var hookObject = utils.hookObject(name, args);
24-
// The callback for the after hook
25-
var hookCallback = function(error, newHookObject) {
26-
// Call the callback with the result we set in `newCallback`
27-
var hook = newHookObject || hookObject;
28-
29-
if(error) {
30-
// Call the old callback with the hook error
31-
return hook.callback(error);
32-
}
33-
34-
hook.callback(null, hook.result);
35-
};
36-
// The new _super method callback
37-
var newCallback = function(error, result) {
38-
if(error) {
39-
// Call the old callback with the error
40-
return hookObject.callback(error);
41-
}
42-
43-
// Set hookObject result
44-
hookObject.result = result;
45-
46-
// Call the hook object
47-
var promise = hook.call(self, hookObject, hookCallback);
48-
if(typeof promise !== 'undefined' && typeof promise.then === 'function') {
49-
promise.then(function() {
50-
hookCallback();
51-
}, function(error) {
52-
hookCallback(error);
53-
});
54-
}
55-
};
24+
result[name] = [];
25+
26+
_.each(hooks, function(hook){
27+
var fn = function() {
28+
var self = this;
29+
var args = _.toArray(arguments);
30+
var hookObject = utils.hookObject(name, args);
31+
// The callback for the after hook
32+
var hookCallback = function(error, newHookObject) {
33+
// Call the callback with the result we set in `newCallback`
34+
var hook = newHookObject || hookObject;
35+
36+
if(error) {
37+
// Call the old callback with the hook error
38+
return hook.callback(error);
39+
}
40+
41+
hook.callback(null, hook.result);
42+
};
43+
// The new _super method callback
44+
var newCallback = function(error, result) {
45+
if(error) {
46+
// Call the old callback with the error
47+
return hookObject.callback(error);
48+
}
49+
50+
// Set hookObject result
51+
hookObject.result = result;
5652

57-
hookObject.type = 'after';
53+
// Call the hook object
54+
var promise = hook.call(self, hookObject, hookCallback);
55+
if(typeof promise !== 'undefined' && typeof promise.then === 'function') {
56+
promise.then(function() {
57+
hookCallback();
58+
}, function(error) {
59+
hookCallback(error);
60+
});
61+
}
62+
};
5863

59-
// Remove the old callback and replace with the new callback that runs the hook
60-
args.pop();
61-
args.push(newCallback);
64+
hookObject.type = 'after';
6265

63-
// Call super method
64-
return this._super.apply(this, args);
65-
};
66+
// Remove the old callback and replace with the new callback that runs the hook
67+
args.pop();
68+
args.push(newCallback);
69+
70+
// Call super method
71+
return this._super.apply(this, args);
72+
};
73+
74+
result[name].push(fn);
75+
});
6676
});
6777

68-
this.mixin(mixin);
78+
var self = this;
79+
80+
_.each(mixin, function(hooks, method){
81+
_.each(hooks, function(hook){
82+
var obj = {};
83+
obj[method] = hook;
84+
self.mixin(obj);
85+
});
86+
});
6987

7088
return this;
7189
}

lib/before.js

+49-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
2+
13
var _ = require('lodash');
24
var utils = require('./utils');
35

46
module.exports = function(service) {
5-
if(typeof service.mixin !== 'function') {
7+
if (!_.isFunction(service.mixin)) {
68
return;
79
}
810

@@ -11,44 +13,62 @@ module.exports = function(service) {
1113

1214
service.mixin({
1315
before: function(obj) {
14-
var mixin = _.transform(obj, function(result, hook, name) {
16+
var mixin = _.transform(obj, function(result, hooks, name) {
1517
// Don't mix in if it is not a service method
1618
if(app.methods.indexOf(name) === -1) {
1719
return;
1820
}
1921

20-
result[name] = function() {
21-
var self = this;
22-
// The original method
23-
var _super = this._super;
24-
// The hook data object
25-
var hookObject = utils.hookObject(name, arguments);
26-
// The callback for the hook
27-
var hookCallback = function(error, newHookObject) {
28-
if(error) {
29-
// We got an error
30-
return hookObject.callback(error);
31-
}
22+
if (!_.isArray(hooks)){
23+
hooks = [hooks];
24+
}
25+
26+
result[name] = [];
3227

33-
// Call the _super method. We either use the original hook object
34-
// to create the arguments or the new one passed to the hook callback
35-
_super.apply(self, utils.makeArguments(newHookObject || hookObject));
28+
_.each(hooks, function(hook){
29+
var fn = function() {
30+
var self = this;
31+
// The original method
32+
var _super = this._super;
33+
// The hook data object
34+
var hookObject = utils.hookObject(name, arguments);
35+
// The callback for the hook
36+
var hookCallback = function(error, newHookObject) {
37+
if(error) {
38+
// We got an error
39+
return hookObject.callback(error);
40+
}
41+
42+
// Call the _super method. We either use the original hook object
43+
// to create the arguments or the new one passed to the hook callback
44+
_super.apply(self, utils.makeArguments(newHookObject || hookObject));
45+
};
46+
47+
hookObject.type = 'before';
48+
49+
var promise = hook.call(self, hookObject, hookCallback);
50+
if(typeof promise !== 'undefined' && typeof promise.then === 'function') {
51+
promise.then(function() {
52+
hookCallback();
53+
}, function(error) {
54+
hookCallback(error);
55+
});
56+
}
3657
};
3758

38-
hookObject.type = 'before';
39-
40-
var promise = hook.call(self, hookObject, hookCallback);
41-
if(typeof promise !== 'undefined' && typeof promise.then === 'function') {
42-
promise.then(function() {
43-
hookCallback();
44-
}, function(error) {
45-
hookCallback(error);
46-
});
47-
}
48-
};
59+
result[name].push(fn);
60+
});
4961
});
62+
63+
var self = this;
5064

51-
this.mixin(mixin);
65+
_.each(mixin, function(hooks, method){
66+
_.each(hooks, function(hook){
67+
var obj = {};
68+
obj[method] = hook;
69+
self.mixin(obj);
70+
});
71+
});
5272

5373
return this;
5474
}

test/after.test.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('.after hooks', function() {
7777
});
7878
});
7979

80-
it('adds .after() and chains multiple calls', function(done) {
80+
it('adds .after() and chains multiple hooks for the same method', function(done) {
8181
var dummyService = {
8282
create: function(data, params, callback) {
8383
callback(null, data);
@@ -112,6 +112,42 @@ describe('.after hooks', function() {
112112
});
113113
});
114114

115+
it('chains multiple after hooks using array syntax', function(done) {
116+
var dummyService = {
117+
create: function(data, params, callback) {
118+
callback(null, data);
119+
}
120+
};
121+
122+
var app = feathers().configure(hooks()).use('/dummy', dummyService);
123+
var service = app.lookup('dummy');
124+
125+
service.after({
126+
create: [
127+
function(hook, next) {
128+
hook.result.some = 'thing';
129+
next();
130+
},
131+
function(hook, next) {
132+
hook.result.other = 'stuff';
133+
134+
next();
135+
}
136+
]
137+
});
138+
139+
service.create({ my: 'data' }, {}, function(error, data) {
140+
console.log('DATA', data);
141+
142+
assert.deepEqual({
143+
my: 'data',
144+
some: 'thing',
145+
other: 'stuff'
146+
}, data, 'Got modified data');
147+
done();
148+
});
149+
});
150+
115151
it('.after hooks can return a promise', function(done) {
116152
var app = feathers().configure(hooks()).use('/dummy', {
117153
get: function(id) {

test/before.test.js

+40-6
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('.before hooks', function() {
9595
service.remove(1, { my: 'param' }, done);
9696
});
9797

98-
it('adds .before() and chains multiple calls', function(done) {
98+
it('adds .before() and chains multiple hooks for the same method', function(done) {
9999
var dummyService = {
100100
create: function(data, params, callback) {
101101
assert.deepEqual(data, {
@@ -130,14 +130,48 @@ describe('.before hooks', function() {
130130
}
131131
});
132132

133-
service.create({ some: 'thing' }, {}, function(error, data) {
133+
service.create({ some: 'thing' }, {}, function(error) {
134134
assert.ok(!error, 'No error');
135+
done();
136+
});
137+
});
135138

136-
assert.deepEqual(data, {
137-
some: 'thing',
138-
modified: 'second data'
139-
}, 'Data got modified');
139+
it('chains multiple after hooks using array syntax', function(done) {
140+
var dummyService = {
141+
create: function(data, params, callback) {
142+
assert.deepEqual(data, {
143+
some: 'thing',
144+
modified: 'second data'
145+
}, 'Data modified');
146+
147+
assert.deepEqual(params, {
148+
modified: 'params'
149+
}, 'Params modified');
140150

151+
callback(null, data);
152+
}
153+
};
154+
155+
var app = feathers().configure(hooks()).use('/dummy', dummyService);
156+
var service = app.lookup('dummy');
157+
158+
service.before({
159+
create: [
160+
function(hook, next) {
161+
hook.params.modified = 'params';
162+
163+
next();
164+
},
165+
function(hook, next) {
166+
hook.data.modified = 'second data';
167+
168+
next();
169+
}
170+
]
171+
});
172+
173+
service.create({ some: 'thing' }, {}, function(error) {
174+
assert.ok(!error, 'No error');
141175
done();
142176
});
143177
});

0 commit comments

Comments
 (0)