feathers-hooks-utils
Utility library for writing Feathersjs hooks.
DEPRECATED. Goodbye, adiós, au revoir, auf Wiedersehen, zàijiàn.
A modified version of this repo has been moved into feathersjs/feathers-hooks-common/utils, and you should use that instead. However there are breaking differences.
Code Example
const utils = ; // Check we are running as a before hook performing an update or patch method.exportsbefore = create: utils; ... ;
// Support conditional inclusion of hooks.// Check user authentication with 1 line of code.const populateOwnerId = false; exportsbefore = create: ; /* Same result ascreate: [ auth.verifyToken(), auth.populateUser(), auth.restrictToAuthenticated() hooks.associateCurrentUser({ as: 'createdById' }),]*/
// Get hook data from `hook.data`, `hook.data.$set` or `hook.result` depending on the context.exportsbefore: patch: { const data = utils; // from hook.data.$set ... } ;exportsafter: update: { const data = utils; // from hook.result ... } ;
// Set hook data in `hook.data`, `hook.data.$set` or `hook.result` depending on the context.exportsbefore: create: { ... utils; // into hook.data } ;exportsafter: create: { ... utils; // into hook.result } ;
// Replace all hook data in `hook.data`, `hook.data.$set` or `hook.result` depending on the context.// This might be used, for example, to replace the original hook data after it has been sanitized. exportsbefore: create: { ... utils; // into hook.data } ;exportsafter: create: { ... utils; // into hook.result } ;
Motivation
You will be writing hooks if you use Feathers.
This library delivers some of the common functions you want to perform, and its modularity should make your hooks easier to understand.
Installation
Install Nodejs.
Run npm install feathers-hooks-utils --save
in your project folder.
You can then require the utilities.
// ES5const utils = ;const checkContext = utilscheckContext;// or ES6;
You can require individual utilities if you want to reduce (a little bit of) code:
// ES5const checkContext = ;
/src
on GitHub contains the ES6 source. It will run on Node 6+ without transpiling.
API Reference
Each utility is fully documented in its source file.
Tests
npm test
to run tests.
npm run cover
to run tests plus coverage.
Contributors
License
MIT. See LICENSE.