# call-hooks

> Function for adding before/after/call/arguments/result hooks to another function.

Latest version **2.0.2** (published 2025-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install call-hooks
pnpm add call-hooks
yarn add call-hooks
bun add call-hooks
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2025-01-25 |
| First published | 2019-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Maintainers | fabiospampinato |
| Keywords | call, hooks, function, override, intercept |

## Links

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

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

- 2.0.2 (latest) — 2025-01-25
- 2.0.1 — 2024-04-04
- 2.0.0 — 2022-04-06
- 1.0.2 — 2020-04-12
- 1.0.1 — 2019-05-23
- 1.0.0 — 2019-05-23

## README

# Call Hooks

Function for adding before/after/call/args/result hooks to another function.

It supports both regular and async functions, even if they may throw.

## Install

```sh
npm install call-hooks
```

## Usage

This function has the following interface:

```ts
function callHooks ( fn: Function, hooks: Hooks ): Function;
```

These are the supported hooks:

```ts
{
  args: ( args: any[] ) => any[],
  before: ( args: any[] ) => void,
  call: ( args: any[] ) => Result,
  result: ( args: any[], result: any ) => any,
  after: ( args: any[], result: any | Error ) => void
}
```

## Hooks

Hooks are called in the following order:

##### Args

The `args` hook is the first one called, its returned value will be the actual arguments object used:

```ts
import hooks from 'call-hooks';
import fn from './my_fn';

const fnWithHooks = hooks ( fn, {
  args ( args ) {
    if ( args.length === 1 ) return args; // Not overriding the arguments
    return [...args, true]; // Overriding the arguments
  }
});
```

##### Before

The `before` hook is called before the wrapped function will be executed:

```ts
import hooks from 'call-hooks';
import fn from './my_fn';

const fnWithHooks = hooks ( fn, {
  before ( args ) {
    console.log ( 'Called with arguments:', args );
  }
});
```

##### Call

The `call` hook is called instead of the wrapped function:

```ts
import hooks from 'call-hooks';
import fn from './my_fn';

const fnWithHooks = hooks ( fn, {
  call ( args ) {
    if ( args.length === 0 ) return null; // Overriding the function
    return fn.apply ( this, args ); // Not overriding the function
  }
});
```

##### Result

The `result` hook is called after the wrapped function has been executed, its returned value will be the actual return value:

```ts
import hooks from 'call-hooks';
import fn from './my_fn';

const fnWithHooks = hooks ( fn, {
  result ( args, result ) {
    if ( isNaN ( result ) ) return 0; // Overriding the returned value
    return result; // Not overriding the returned value
  }
});
```

##### After

The `after` hook is the last one called:

```ts
import hooks from 'call-hooks';
import fn from './my_fn';

const fnWithHooks = hooks ( fn, {
  after ( args, result ) {
    console.log ( 'Returned result:', result );
  }
});
```

## License

MIT © Fabio Spampinato

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