# feathers-hooks-utils

> Utility library for writing Feathersjs hooks.

Latest version **0.2.0** (published 2016-08-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install feathers-hooks-utils
pnpm add feathers-hooks-utils
yarn add feathers-hooks-utils
bun add feathers-hooks-utils
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2016-08-20 |
| First published | 2016-07-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >4.2.4 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | John |
| Maintainers | eddystop |
| Keywords | feathers, feathersjs, hooks |

## Links

- npm: https://www.npmjs.com/package/feathers-hooks-utils
- Repository: https://github.com/eddyystop/feathers-hooks-utils
- Homepage: https://github.com/eddyystop/feathers-hooks-utils#readme
- Issues: https://github.com/eddyystop/feathers-hooks-utils/issues
- npm.io page: https://npm.io/package/feathers-hooks-utils

## Dependencies (1)

- [feathers-authentication](https://npm.io/package/feathers-authentication.md) 0.7.9

## Alternatives

- [@fortawesome/react-fontawesome](https://npm.io/package/@fortawesome/react-fontawesome.md) — 2.2M weekly downloads
- [roboto-fontface](https://npm.io/package/roboto-fontface.md) — 196.0K weekly downloads
- [@react-native-vector-icons/common](https://npm.io/package/@react-native-vector-icons/common.md) — 150.4K weekly downloads
- [stockfish](https://npm.io/package/stockfish.md) — 27.0K weekly downloads
- [@procore/core-icons](https://npm.io/package/@procore/core-icons.md) — 4.6K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2016-08-20
- 0.1.3 — 2016-08-20
- 0.1.2 — 2016-07-14
- 0.1.1 — 2016-07-12
- 0.1.0 — 2016-07-08
- 0.0.4 — 2016-07-07
- 0.0.3 — 2016-07-07
- 0.0.1 — 2016-07-06

## README

## feathers-hooks-utils
Utility library for writing [Feathersjs](http://feathersjs.com/) hooks.

[![Build Status](https://travis-ci.org/eddyystop/feathers-hooks-utils.svg?branch=master)](https://travis-ci.org/eddyystop/feathers-hooks-utils)
[![Coverage Status](https://coveralls.io/repos/github/eddyystop/feathers-hooks-utils/badge.svg?branch=master)](https://coveralls.io/github/eddyystop/feathers-hooks-utils?branch=master)

**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

```javascript
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']);
    ...
  ],
};
```

```javascript
// 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' }),
]
*/
```

```javascript
// 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
      ...
    },
  ],
};
```

```javascript
// 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
    },
  ],
};
```

```javascript
// 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](http://docs.feathersjs.com/hooks/readme.html)
if you use [Feathers](http://feathersjs.com/).

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](https://nodejs.org/en/).

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

You can then require the utilities.

```javascript
// 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:

```javascript
// 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

- [eddyystop](https://github.com/eddyystop)

## License

MIT. See LICENSE.

---
_Source: https://npm.io/package/feathers-hooks-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
