Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dove Support #623

Closed
wants to merge 14 commits into from
Closed
38 changes: 35 additions & 3 deletions lib/services/combine.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@

const { processHooks } = require('@feathersjs/commons').hooks;

module.exports = function (...serviceHooks) {
const isHookObject = function (hookObject) {
return typeof hookObject === 'object' &&
typeof hookObject.method === 'string' &&
typeof hookObject.type === 'string';
};

return function (context) {
return processHooks.call(this, serviceHooks, context);
let hookObject = context;

const updateCurrentHook = (current) => {
// Either use the returned hook object or the current
// hook object from the chain if the hook returned undefined
if (current) {
if (!isHookObject(current)) {
throw new Error(`${hookObject.type} hook for '${hookObject.method}' method returned invalid hook object`);
}

hookObject = current;
}

return hookObject;
};
// Go through all hooks and chain them into our promise
const promise = serviceHooks.reduce((current, fn) => {
// @ts-ignore
const hook = fn.bind(this);

// Use the returned hook object or the old one
return current.then((currentHook) => hook(currentHook)).then(updateCurrentHook);
}, Promise.resolve(hookObject));

return promise.then(() => hookObject).catch(error => {
// Add the hook information to any errors
error.hook = hookObject;
throw error;
});
};
};
Loading