npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

feathers-hooks-utils

v0.2.0

Published

Utility library for writing Feathersjs hooks.

Downloads

119

Readme

feathers-hooks-utils

Utility library for writing Feathersjs hooks.

Build Status Coverage Status

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');

// Check we are running as a before hook performing an update or patch method.
exports.before = {
  create: [
    utils.checkContext(hook, 'before', ['update', 'patch']);
    ...
  ],
};
// Support conditional inclusion of hooks.
// Check user authentication with 1 line of code.
const populateOwnerId = false;

exports.before = {
  create: concatHooks([ // Flatten hooks
    utils.restrictToAuthenticated, // Ensure user is authenticated. Note its not a fcn call.
    populateOwnerId && hooks.associateCurrentUser({ as: 'ownerId' }), // Conditional inclusion
    hooks.associateCurrentUser({ as: 'createdById' }),
  ]),
};

/* Same result as
create: [
  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.
exports.before: {
  patch: [
    (hook) => {
      const data = utils.get(hook); // from hook.data.$set
      ...
    },
  ],
};
exports.after: {
  update: [
    (hook) => {
      const data = utils.get(hook); // from hook.result
      ...
    },
  ],
};
// Set hook data in `hook.data`, `hook.data.$set` or `hook.result` depending on the context.
exports.before: {
  create: [
    (hook) => {
      ...
      utils.set(hook, 'age', 30); // into hook.data
    },
  ],
};
exports.after: {
  create: [
    (hook) => {
      ...
      utils.set(hook, 'readAt', new Date()); // 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. 
exports.before: {
  create: [
    (hook) => {
      ...
      utils.setAll(hook, sanitizedData); // into hook.data
    },
  ],
};
exports.after: {
  create: [
    (hook) => {
      ...
      utils.set(hook, replacementData); // 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.

// ES5
const utils = require('feathers-hooks-utils');
const checkContext = utils.checkContext;
// or ES6
import { checkContext } from 'feathers-hooks-utils';

You can require individual utilities if you want to reduce (a little bit of) code:

// ES5
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.