Skip to content
Before and after service method call hooks for easy authorization and processing
JavaScript
Find file
Latest commit 7de0417 @daffl daffl 1.5.0

README.md

Feathers Hooks

Build Status

Middleware for Feathers service methods

Documentation

Please refer to the Feathers hooks documentation for more details on:

  • The philosophy behind hooks
  • How you can use hooks
  • How you can chain hooks using Promises
  • Params that are available in hooks
  • Hooks that are bundled with feathers and feathers plugins

Quick start

feathers-hooks allows you to register composable middleware functions before or after a Feathers service method executes. This makes it easy to decouple things like authorization and pre- or post processing from your service logic.

To install from npm, run:

$ npm install feathers-hooks --save

Then, to use the plugin in your Feathers app:

const feathers = require('feathers');
const hooks = require('feathers-hooks');

const app = feathers().configure(hooks());

Then, you can register a hook for a service:

// User service
const service = require('feathers-memory');

module.exports = function(){
  const app = this;

  let myHook = function(options) {
    return function(hook) {
      console.log('My custom hook ran!');
    }
  }

  // Initialize our service
  app.use('/users', service());

  // Get our initialize service to that we can bind hooks
  const userService = app.service('/users');

  // Set up our before hook
  userService.before({
    find: [ myHook() ]
  });
}

Examples

The repository contains the following examples:

  • authorization.js - A simple demo showing how to use hooks for authorization (and post-processing the results) where the user is set via a ?user=username query parameter.
  • timestamp.js - A demo that adds a createdAt and updatedAt timestamp when creating or updating a Todo using hooks.

Changelog

1.4.0

  • Fixes bug where events were getting dispatched before all the after hooks finished.

1.3.0

  • adding toLowerCase hook
  • remove hook now only runs if hook.params is set

1.2.0

  • remove hook now supports a callback function to conditionally run it.

1.1.0

  • Moving bundled hooks top level

1.0.0

  • Make app available inside the hook object (#34)
  • Added remove and disable common hook (#33)
  • Hooks use promises and promise chains (#29)

0.6.0

  • Prevent next from being called multiple times (#27)
  • Migrate to ES6 and new plugin infrastructure (#26)

0.5.0

  • Make sure hooks and service methods keep their context (#17)
  • Refactoring to fix hook execution order and all-hooks (#16)
  • Arrays of Hooks are running in reverse order (#13)
  • Before all and after all hooks (#11)
  • Better check for .makeArguments id (#15)
  • Remove Feathers peer dependency (#12)

0.4.0

  • Allows hooks to be chained in an array (#2)

0.3.0

  • Allows hooks to return a promise (#3, #4)

0.2.0

  • API change to use hook objects instead of function parameters (#1)

0.1.0

  • Initial release

Author

License

Copyright (c) 2014 David Luecke

Licensed under the MIT license.

Something went wrong with that request. Please try again.