# koa-middleware-hook

> low-level granular hooks for your koa middleware

Latest version **1.0.2** (published 2015-10-15) · 0 weekly downloads

## Install

```sh
npm install koa-middleware-hook
pnpm add koa-middleware-hook
yarn add koa-middleware-hook
bun add koa-middleware-hook
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2015-10-15 |
| First published | 2015-10-15 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Matthew Mueller |
| Maintainers | mattmueller |
| Keywords | hooks, middleware, koa |

## Links

- npm: https://www.npmjs.com/package/koa-middleware-hook
- Repository: https://github.com/koajs/middleware-hook
- Homepage: https://github.com/koajs/middleware-hook#readme
- Issues: https://github.com/koajs/middleware-hook/issues
- npm.io page: https://npm.io/package/koa-middleware-hook

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^2.2.0
- [koa-compose](https://npm.io/package/koa-compose.md) ^2.3.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2015-10-15
- 1.0.1 — 2015-10-15
- 1.0.0 — 2015-10-15

## README

# koa-middleware-hook

low-level hooks for your middleware. this is mostly an experiment,
but it lets you hook into the following spots:

  1. Downstream: Beginning of the middleware
  2. Downstream: Before yielding to the next middleware in the stack
  3. Upstream: Beginning on the way back up the stack
  4. Upstream: When the middleware finishs

useful for profiling your middleware, logging your middleware, timing your middleware, etc.

probably not suitable for production, though I haven't profiled it or anything.

## Install

```
npm install koa-middleware-hook
```

## Usage

##### `hook = Hook(function: marker, function: reduce)`

`marker` is a function called during each point in time where you'd return what you want to track. Here's an example:

```js
function marker () {
  return new Date()
}
```

`reduce` takes all 4 hooks and allows you to merge them together in some meaningful way. Something to keep in mind is that the innermost middleware will only execute 2 hooks:

```js
function reduce (middleware_name, downstream_start, downstream_end | upstream_end, upstream_start | null, upstream_end | null) {
  if (arguments.length === 3) {
    debug('%s: %sms', middleware_name, downstream_end - downstream_start)
  } else {
    debug('%s: %sms', middleware_name, (downstream_end - downstream_start) + (upstream_end - upstream_start))
  }
}
```

Here's how you use hook:

```
app.use(hook(bodyparser()))
```

## TODO

- solicit ideas for a better API
- some more usage examples

## License

MIT

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