JSPM

  • Created
  • Published
  • Downloads 32138
  • Score
    100M100P100Q152589F
  • License MIT

Useful hooks for use with Feathersjs services.

Package Exports

  • feathers-hooks-common
  • feathers-hooks-common/lib/index

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (feathers-hooks-common) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

feathers-hooks-common

Useful hooks for use with Feathersjs services.

Build Status Coverage Status

Code Example

(1) Add created at timestamp.

const lib = require('feathers-hooks-common');
module.exports.before = {
  create: [ lib.setCreatedAt({ as: 'createdAt' }) ] // added to hook.data
};

(2) Add or update the updated at timestamp.

module.exports.before = {
  create: [ lib.setUpdatedAt({ as: 'updatedAt' }) ], // added to hook.data
  update: [ lib.setUpdatedAt({ as: 'updatedAt' }) ], // added to hook.data.$set
  patch: [ lib.setUpdatedAt({ as: 'updatedAt' }) ]
};

(3) Normalize URL slug, e.g. https://.../stores/:storeid/..., between feathers-socketio, feathers-rest and raw HTTP transports.

module.exports.before = {
  create: [ lib.setSlug('storeid') ], // slug value at hook.params.query.storeid
  update: [ lib.setSlug('storeid') ],
  patch: [ lib.setSlug('storeid') ]
};
module.exports.after = {
  create: [ lib.setSlug('storeid') ] // slug value at hook.params.query.storeid
};

(4) Display current info about the hook to console.

module.exports.after = {
  create: [ lib.setUpdatedAt('step 1') ]
  // * step 1
  // type: before, method: create
  // data: { name: 'Joe Doe' }
  // query: { sex: 'm' }
  // result: { assigned: true }
};

(5) Wrapper for require('feathers-authentication').hooks.restrictToRoles.

const authorizer = lib.restrictToRoles([], 'authorizedRoles', false, 'userId');
module.exports.before = {
  create: [ authorizer(['purchasing', 'receiving']) ]
};

Motivation

Feathers services can be developed faster if the hooks you need are at hand.

This package provides some commonly needed hooks.

Installation

Install Nodejs.

Run npm install feathers-hooks-common --save in your project folder.

/src on GitHub contains the ES6 source. It will run on Node 6+ without transpiling.

API Reference

Each file fully documents its module's API.

Tests

npm test to run tests.

npm run cover to run tests plus coverage.

Contributors

License

MIT. See LICENSE.