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

Commit

Permalink
Adds the app object at hook.app.
Browse files Browse the repository at this point in the history
I think this is the most elegant place to add the app object so that it's always available, even on internal requests that don't pass through one of the providers.

The next issue that will surface is probably circular requests where service A requests data from B, B from C, and C from A.
  • Loading branch information
marshallswain committed Jan 23, 2016
1 parent 344bce1 commit b3475d1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 43 deletions.
88 changes: 46 additions & 42 deletions src/before.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
import { hooks as utils } from 'feathers-commons';
import { addHookMethod, processHooks } from './commons';

export default function(service) {
if(typeof service.mixin !== 'function') {
return;
}
export default function(app) {
return function(service){
if(typeof service.mixin !== 'function') {
return;
}

const methods = this.methods;
const old = service.before;
const mixin = {};
const methods = this.methods;
const old = service.before;
const mixin = {};

addHookMethod(service, 'before', methods);
addHookMethod(service, 'before', methods);

methods.forEach(method => {
if(typeof service[method] !== 'function') {
return;
}
methods.forEach(method => {
if(typeof service[method] !== 'function') {
return;
}

mixin[method] = function() {
const _super = this._super.bind(this);
const hookObject = utils.hookObject(method, 'before', arguments);
const hooks = this.__beforeHooks[method];

// Run all hooks
let promise = processHooks.call(this, hooks, hookObject);

// Then call the original method
return promise.then(hookObject => {
return new Promise((resolve, reject) => {
const args = utils.makeArguments(hookObject);

// We replace the callback with resolving the promise
args.splice(args.length - 1, 1, (error, result) => {
if(error) {
reject(error);
} else {
hookObject.result = result;
resolve(hookObject);
}
});
mixin[method] = function() {
const _super = this._super.bind(this);
const hookObject = utils.hookObject(method, 'before', arguments);
const hooks = this.__beforeHooks[method];

hookObject.app = app;

_super(... args);
// Run all hooks
let promise = processHooks.call(this, hooks, hookObject);

// Then call the original method
return promise.then(hookObject => {
return new Promise((resolve, reject) => {
const args = utils.makeArguments(hookObject);

// We replace the callback with resolving the promise
args.splice(args.length - 1, 1, (error, result) => {
if(error) {
reject(error);
} else {
hookObject.result = result;
resolve(hookObject);
}
});

_super(... args);
});
});
});
};
});
};
});

service.mixin(mixin);
service.mixin(mixin);

if(old) {
service.before(old);
}
if(old) {
service.before(old);
}
};
}
2 changes: 1 addition & 1 deletion src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import after from './after';

export default function() {
return function() {
this.mixins.push(before);
this.mixins.push(before(this));
this.mixins.push(after);
};
}
29 changes: 29 additions & 0 deletions test/before.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,35 @@ describe('.before hooks', () => {
});
});

it('contains the app object at hook.app', done => {
const someServiceConfig = {
before: {
create(hook, next) {
hook.data.appPresent = typeof hook.app === 'function';
assert.equal(hook.data.appPresent, true);
next(null, hook);
}
},

create(data, params, callback) {
callback(null, data);
}
};

const app = feathers().configure(hooks()).use('/some-service', someServiceConfig);
const someService = app.service('some-service');

someService.create({ some: 'thing' }, {}, (error, data) => {

assert.deepEqual(data, {
some: 'thing',
appPresent: true
}, 'App object was present');

done();
});
});

it('passes errors', done => {
const dummyService = {
before: {
Expand Down

0 comments on commit b3475d1

Please sign in to comment.