Skip to content

Commit

Permalink
Switch .before and .after to .beforeRead + .beforeWrite and .afterRea…
Browse files Browse the repository at this point in the history
…d + .afterWrite.

Trap legacy transform functions
  • Loading branch information
EugeneKostrikov committed Jun 4, 2014
1 parent 9d0c562 commit c877afa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 69 deletions.
85 changes: 25 additions & 60 deletions lib/fortune.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ Fortune.prototype._defaults = {
suffix: '',
inflect: true,
cors: true,
environment: process.env.NODE_ENV
environment: process.env.NODE_ENV,

//legacy transforms trap
throwOnLegacyTransforms: true

};

Expand Down Expand Up @@ -224,6 +227,17 @@ Fortune.prototype.beforeAllWrite = GlobalHook('_before', 'write');
Fortune.prototype.afterAllRead = GlobalHook('_after', 'read');
Fortune.prototype.afterAllWrite = GlobalHook('_after', 'write');

Fortune.prototype.before = function(name, hooksArray, inlineConfig){
this.beforeRead(name, hooksArray, inlineConfig);
this.beforeWrite(name, hooksArray, inlineConfig);
return this;
};
Fortune.prototype.after = function(name, hooksArray, inlineConfig){
this.afterRead.call(this, name, hooksArray, inlineConfig);
this.afterWrite.call(this, name, hooksArray, inlineConfig);
return this;
};

Fortune.prototype.beforeRead = Hook('_before', 'read');
Fortune.prototype.beforeWrite = Hook('_before', 'write');
Fortune.prototype.afterRead = Hook('_after', 'read');
Expand All @@ -240,8 +254,12 @@ function Hook(time, type){

return function(name, hooksArray, inlineConfig){
var that = this;
if (this.options.throwOnLegacyTransforms && (_.isFunction(name) || _.isFunction(hooksArray))){
throw new Error('You use legacy transforms somewhere');
}

if (!_.isString(name)){
if (_.isArray(name)){
if (_.isArray(name) || _.isFunction(name)){
inlineConfig = hooksArray;
hooksArray = name;
}
Expand All @@ -255,62 +273,6 @@ function Hook(time, type){
}
}

/**
* @deprecated Equals to beforeWrite. Use beforeRead and beforeWrite instead.
*
* Do something before a resource is saved in the database.
* The callback function has two optional parameters, the request and response objects, respectively.
* It may return either the resource or a promise. Here's a contrived
* example that stores the Authorization header into a resource:
*
* ```javascript
* app.before('resource', function (request, response) {
* var authorization = request.get('Authorization');
* if (!authorization) throw new Error('Authorization failed');
* this.authorization = authorization;
* return this;
* });
* ```
*
* @param {String} name may be space separated, i.e. 'cat dog human'
* @param {Function} fn this callback function is called within the context of the resource, and has two optional parameters: the request and response objects, respectively.
* @return {this}
*/
Fortune.prototype.before = function (name, fn) {
var that = this;
process.nextTick(function(){
hooks.addHook.call(that, name, fn, '_before', 'write');
});
return this;
};

/**
* @deprecated Equals to afterRead. Use afterRead and afterWrite instead.
* Do something after a resource is read from the database.
* The callback function has two optional parameters, the request and response objects, respectively.
* It may return either the resource or a promise. Here's a contrived
* example that hides a `password` and `salt` from being exposed:
*
* ```javascript
* app.after('user', function () {
* delete this.password;
* delete this.salt;
* return this;
* });
* ```
*
* @param {String} name may be space separated, i.e. 'cat dog human'
* @param {Function} fn this callback function is called within the context of the resource, and has two optional parameters: the request and response objects, respectively.
* @return {this}
*/
Fortune.prototype.after = function (name, fn) {
var that = this;
process.nextTick(function(){
hooks.addHook.call(that, name, fn, '_after', 'read');
});
return this;
};

/**
* @deprecated Hooks provide more flexible interface
* Convenience method to define before & after at once.
Expand All @@ -326,8 +288,11 @@ Fortune.prototype.transform = function (name, before, after) {
before = name;
name = this._resource;
}
this.before(name, before);
this.after(name, after);
var that = this;
process.nextTick(function(){
hooks.addHook.call(that, name, before, '_before', 'write');
hooks.addHook.call(that, name, after, '_after', 'read');
});
return this;
};

Expand Down
9 changes: 6 additions & 3 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ describe('Fortune', function () {
});
});
});
describe('backward compatibility', function(){
it('legacy before should default to beforeWrite', function(done){
describe('broken backward compatibility', function(){
it('legacy before should now be beforeRead + beforeWrite', function(done){
var man = {
people: [{
name: 'Smith',
Expand All @@ -803,16 +803,19 @@ describe('Fortune', function () {
request(baseUrl).post('/people')
.set('content-type', 'application/json')
.send(JSON.stringify(man))
.expect('before', 'called for both reads and writes')
.end(function(err, res){
should.not.exist(err);
var body = JSON.parse(res.text);
(body.people[0].official).should.equal('Mr. Smith');
(body.people[0].nickname).should.equal('Super ' + body.people[0].name + '!');
done();
});
});
it('legacy after should default to afterRead', function(done){
it('legacy after should now be afterRead + afterWrite', function(done){
request(baseUrl).get('/people/' + ids.people[0])
.expect(200)
.expect('after', 'called for both reads and writes')
.end(function(err, res){
should.not.exist(err);
var body = JSON.parse(res.text);
Expand Down
19 changes: 13 additions & 6 deletions test/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ module.exports = function(options, port) {
})


.before('person', function(){
.before('person', function(req, res){
this.password = Math.random();
this.official = 'Mr. ' + this.name;
res.setHeader('before', 'called for both reads and writes');
return this;
})

Expand All @@ -144,16 +145,22 @@ module.exports = function(options, port) {
init: Hook
}])

.after('person', function() {
.after('person', function(req, res) {
res.setHeader('after', 'called for both reads and writes');
delete this.password;
this.nickname = 'Super ' + this.name;
return this;
})

.after('person', function() {
this.nickname = this.nickname + '!';
return this;
})
.after('person',[{
name: 'secondLegacyAfter',
init: function() {
return function(){
this.nickname = this.nickname + '!';
return this;
}
}
}])

.listen(port);

Expand Down

0 comments on commit c877afa

Please sign in to comment.