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 = require('feathers-hooks-utils');
exports.before = {
create: [
utils.checkContext(hook, 'before', ['update', 'patch']);
...
],
};
const populateOwnerId = false;
exports.before = {
create: concatHooks([
utils.restrictToAuthenticated,
populateOwnerId && hooks.associateCurrentUser({ as: 'ownerId' }),
hooks.associateCurrentUser({ as: 'createdById' }),
]),
};
exports.before: {
patch: [
(hook) => {
const data = utils.get(hook);
...
},
],
};
exports.after: {
update: [
(hook) => {
const data = utils.get(hook);
...
},
],
};
exports.before: {
create: [
(hook) => {
...
utils.set(hook, 'age', 30);
},
],
};
exports.after: {
create: [
(hook) => {
...
utils.set(hook, 'readAt', new Date());
},
],
};
exports.before: {
create: [
(hook) => {
...
utils.setAll(hook, sanitizedData);
},
],
};
exports.after: {
create: [
(hook) => {
...
utils.set(hook, replacementData);
},
],
};
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.
const utils = require('feathers-hooks-utils');
const checkContext = utils.checkContext;
import { checkContext } from 'feathers-hooks-utils';
You can require individual utilities if you want to reduce (a little bit of) code:
const checkContext = require('feathers-hooks-utils/lib/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.