2022-11-04 00:55:07 +08:00
# before-after-hook
> asynchronous hooks for internal functionality
[![npm downloads ](https://img.shields.io/npm/dw/before-after-hook.svg )](https://www.npmjs.com/package/before-after-hook)
2023-10-26 04:27:07 +08:00
[![Test ](https://github.com/gr2m/before-after-hook/actions/workflows/test.yml/badge.svg )](https://github.com/gr2m/before-after-hook/actions/workflows/test.yml)
2022-11-04 00:55:07 +08:00
## Usage
### Singular hook
```js
// instantiate singular hook API
2023-10-26 04:27:07 +08:00
const hook = new Hook.Singular();
2022-11-04 00:55:07 +08:00
// Create a hook
2023-10-26 04:27:07 +08:00
function getData(options) {
2022-11-04 00:55:07 +08:00
return hook(fetchFromDatabase, options)
.then(handleData)
2023-10-26 04:27:07 +08:00
.catch(handleGetError);
2022-11-04 00:55:07 +08:00
}
// register before/error/after hooks.
// The methods can be async or return a promise
2023-10-26 04:27:07 +08:00
hook.before(beforeHook);
hook.error(errorHook);
hook.after(afterHook);
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
getData({ id: 123 });
2022-11-04 00:55:07 +08:00
```
### Hook collection
2023-10-26 04:27:07 +08:00
2022-11-04 00:55:07 +08:00
```js
// instantiate hook collection API
2023-10-26 04:27:07 +08:00
const hookCollection = new Hook.Collection();
2022-11-04 00:55:07 +08:00
// Create a hook
2023-10-26 04:27:07 +08:00
function getData(options) {
return hookCollection("get", fetchFromDatabase, options)
2022-11-04 00:55:07 +08:00
.then(handleData)
2023-10-26 04:27:07 +08:00
.catch(handleGetError);
2022-11-04 00:55:07 +08:00
}
// register before/error/after hooks.
// The methods can be async or return a promise
2023-10-26 04:27:07 +08:00
hookCollection.before("get", beforeHook);
hookCollection.error("get", errorHook);
hookCollection.after("get", afterHook);
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
getData({ id: 123 });
2022-11-04 00:55:07 +08:00
```
### Hook.Singular vs Hook.Collection
There's no fundamental difference between the `Hook.Singular` and `Hook.Collection` hooks except for the fact that a hook from a collection requires you to pass along the name. Therefore the following explanation applies to both code snippets as described above.
The methods are executed in the following order
1. `beforeHook`
2. `fetchFromDatabase`
3. `afterHook`
2023-10-26 04:27:07 +08:00
4. `handleData`
2022-11-04 00:55:07 +08:00
`beforeHook` can mutate `options` before it’ s passed to `fetchFromDatabase` .
If an error is thrown in `beforeHook` or `fetchFromDatabase` then `errorHook` is
called next.
If `afterHook` throws an error then `handleGetError` is called instead
2023-10-26 04:27:07 +08:00
of `handleData` .
2022-11-04 00:55:07 +08:00
If `errorHook` throws an error then `handleGetError` is called next, otherwise
2023-10-26 04:27:07 +08:00
`afterHook` and `handleData` .
2022-11-04 00:55:07 +08:00
You can also use `hook.wrap` to achieve the same thing as shown above (collection example):
```js
2023-10-26 04:27:07 +08:00
hookCollection.wrap("get", async (getData, options) => {
await beforeHook(options);
2022-11-04 00:55:07 +08:00
try {
2023-10-26 04:27:07 +08:00
const result = getData(options);
2022-11-04 00:55:07 +08:00
} catch (error) {
2023-10-26 04:27:07 +08:00
await errorHook(error, options);
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
await afterHook(result, options);
});
2022-11-04 00:55:07 +08:00
```
## Install
```
npm install before-after-hook
```
Or download [the latest `before-after-hook.min.js` ](https://github.com/gr2m/before-after-hook/releases/latest ).
## API
- [Singular Hook Constructor ](#singular-hook-api )
- [Hook Collection Constructor ](#hook-collection-api )
## Singular hook API
- [Singular constructor ](#singular-constructor )
- [hook.api ](#singular-api )
- [hook() ](#singular-api )
- [hook.before() ](#singular-api )
- [hook.error() ](#singular-api )
- [hook.after() ](#singular-api )
- [hook.wrap() ](#singular-api )
- [hook.remove() ](#singular-api )
### Singular constructor
The `Hook.Singular` constructor has no options and returns a `hook` instance with the
methods below:
```js
2023-10-26 04:27:07 +08:00
const hook = new Hook.Singular();
2022-11-04 00:55:07 +08:00
```
2023-10-26 04:27:07 +08:00
2022-11-04 00:55:07 +08:00
Using the singular hook is recommended for [TypeScript ](#typescript )
### Singular API
The singular hook is a reference to a single hook. This means that there's no need to pass along any identifier (such as a `name` as can be seen in the [Hook.Collection API ](#hookcollectionapi )).
The API of a singular hook is exactly the same as a collection hook and we therefore suggest you read the [Hook.Collection API ](#hookcollectionapi ) and leave out any use of the `name` argument. Just skip it like described in this example:
2023-10-26 04:27:07 +08:00
2022-11-04 00:55:07 +08:00
```js
2023-10-26 04:27:07 +08:00
const hook = new Hook.Singular();
2022-11-04 00:55:07 +08:00
// good
2023-10-26 04:27:07 +08:00
hook.before(beforeHook);
hook.after(afterHook);
hook(fetchFromDatabase, options);
2022-11-04 00:55:07 +08:00
// bad
2023-10-26 04:27:07 +08:00
hook.before("get", beforeHook);
hook.after("get", afterHook);
hook("get", fetchFromDatabase, options);
2022-11-04 00:55:07 +08:00
```
## Hook collection API
- [Collection constructor ](#collection-constructor )
2023-10-26 04:27:07 +08:00
- [hookCollection.api ](#hookcollectionapi )
- [hookCollection() ](#hookcollection )
- [hookCollection.before() ](#hookcollectionbefore )
- [hookCollection.error() ](#hookcollectionerror )
- [hookCollection.after() ](#hookcollectionafter )
- [hookCollection.wrap() ](#hookcollectionwrap )
- [hookCollection.remove() ](#hookcollectionremove )
2022-11-04 00:55:07 +08:00
### Collection constructor
The `Hook.Collection` constructor has no options and returns a `hookCollection` instance with the
methods below
```js
2023-10-26 04:27:07 +08:00
const hookCollection = new Hook.Collection();
2022-11-04 00:55:07 +08:00
```
### hookCollection.api
Use the `api` property to return the public API:
- [hookCollection.before() ](#hookcollectionbefore )
- [hookCollection.after() ](#hookcollectionafter )
- [hookCollection.error() ](#hookcollectionerror )
- [hookCollection.wrap() ](#hookcollectionwrap )
- [hookCollection.remove() ](#hookcollectionremove )
That way you don’ t need to expose the [hookCollection() ](#hookcollection ) method to consumers of your library
### hookCollection()
Invoke before and after hooks. Returns a promise.
```js
2023-10-26 04:27:07 +08:00
hookCollection(nameOrNames, method /*, options */);
2022-11-04 00:55:07 +08:00
```
< table >
< thead >
< tr >
< th > Argument< / th >
< th > Type< / th >
< th > Description< / th >
< th > Required< / th >
< / tr >
< / thead >
< tr >
< th align = "left" > < code > name< / code > < / th >
< td > String or Array of Strings< / td >
< td > Hook name, for example < code > 'save'< / code > . Or an array of names, see example below.< / td >
< td > Yes< / td >
< / tr >
< tr >
< th align = "left" > < code > method< / code > < / th >
< td > Function< / td >
< td > Callback to be executed after all before hooks finished execution successfully. < code > options< / code > is passed as first argument< / td >
< td > Yes< / td >
< / tr >
< tr >
< th align = "left" > < code > options< / code > < / th >
< td > Object< / td >
< td > Will be passed to all before hooks as reference, so they can mutate it< / td >
< td > No, defaults to empty object (< code > {}< / code > )< / td >
< / tr >
< / table >
Resolves with whatever `method` returns or resolves with.
Rejects with error that is thrown or rejected with by
1. Any of the before hooks, whichever rejects / throws first
2. `method`
3. Any of the after hooks, whichever rejects / throws first
Simple Example
```js
2023-10-26 04:27:07 +08:00
hookCollection(
"save",
function (record) {
return store.save(record);
},
record
);
2022-11-04 00:55:07 +08:00
// shorter: hookCollection('save', store.save, record)
2023-10-26 04:27:07 +08:00
hookCollection.before("save", function addTimestamps(record) {
const now = new Date().toISOString();
2022-11-04 00:55:07 +08:00
if (record.createdAt) {
2023-10-26 04:27:07 +08:00
record.updatedAt = now;
2022-11-04 00:55:07 +08:00
} else {
2023-10-26 04:27:07 +08:00
record.createdAt = now;
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
});
2022-11-04 00:55:07 +08:00
```
Example defining multiple hooks at once.
```js
2023-10-26 04:27:07 +08:00
hookCollection(
["add", "save"],
function (record) {
return store.save(record);
},
record
);
hookCollection.before("add", function addTimestamps(record) {
2022-11-04 00:55:07 +08:00
if (!record.type) {
2023-10-26 04:27:07 +08:00
throw new Error("type property is required");
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
});
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
hookCollection.before("save", function addTimestamps(record) {
2022-11-04 00:55:07 +08:00
if (!record.type) {
2023-10-26 04:27:07 +08:00
throw new Error("type property is required");
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
});
2022-11-04 00:55:07 +08:00
```
Defining multiple hooks is helpful if you have similar methods for which you want to define separate hooks, but also an additional hook that gets called for all at once. The example above is equal to this:
```js
2023-10-26 04:27:07 +08:00
hookCollection(
"add",
function (record) {
return hookCollection(
"save",
function (record) {
return store.save(record);
},
record
);
},
record
);
2022-11-04 00:55:07 +08:00
```
### hookCollection.before()
Add before hook for given name.
```js
2023-10-26 04:27:07 +08:00
hookCollection.before(name, method);
2022-11-04 00:55:07 +08:00
```
< table >
< thead >
< tr >
< th > Argument< / th >
< th > Type< / th >
< th > Description< / th >
< th > Required< / th >
< / tr >
< / thead >
< tr >
< th align = "left" > < code > name< / code > < / th >
< td > String< / td >
< td > Hook name, for example < code > 'save'< / code > < / td >
< td > Yes< / td >
< / tr >
< tr >
< th align = "left" > < code > method< / code > < / th >
< td > Function< / td >
< td >
Executed before the wrapped method. Called with the hook’ s
< code > options< / code > argument. Before hooks can mutate the passed options
before they are passed to the wrapped method.
< / td >
< td > Yes< / td >
< / tr >
< / table >
Example
```js
2023-10-26 04:27:07 +08:00
hookCollection.before("save", function validate(record) {
2022-11-04 00:55:07 +08:00
if (!record.name) {
2023-10-26 04:27:07 +08:00
throw new Error("name property is required");
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
});
2022-11-04 00:55:07 +08:00
```
### hookCollection.error()
Add error hook for given name.
```js
2023-10-26 04:27:07 +08:00
hookCollection.error(name, method);
2022-11-04 00:55:07 +08:00
```
< table >
< thead >
< tr >
< th > Argument< / th >
< th > Type< / th >
< th > Description< / th >
< th > Required< / th >
< / tr >
< / thead >
< tr >
< th align = "left" > < code > name< / code > < / th >
< td > String< / td >
< td > Hook name, for example < code > 'save'< / code > < / td >
< td > Yes< / td >
< / tr >
< tr >
< th align = "left" > < code > method< / code > < / th >
< td > Function< / td >
< td >
Executed when an error occurred in either the wrapped method or a
< code > before< / code > hook. Called with the thrown < code > error< / code >
and the hook’ s < code > options< / code > argument. The first < code > method< / code >
which does not throw an error will set the result that the after hook
methods will receive.
< / td >
< td > Yes< / td >
< / tr >
< / table >
Example
```js
2023-10-26 04:27:07 +08:00
hookCollection.error("save", function (error, options) {
if (error.ignore) return;
throw error;
});
2022-11-04 00:55:07 +08:00
```
### hookCollection.after()
Add after hook for given name.
```js
2023-10-26 04:27:07 +08:00
hookCollection.after(name, method);
2022-11-04 00:55:07 +08:00
```
< table >
< thead >
< tr >
< th > Argument< / th >
< th > Type< / th >
< th > Description< / th >
< th > Required< / th >
< / tr >
< / thead >
< tr >
< th align = "left" > < code > name< / code > < / th >
< td > String< / td >
< td > Hook name, for example < code > 'save'< / code > < / td >
< td > Yes< / td >
< / tr >
< tr >
< th align = "left" > < code > method< / code > < / th >
< td > Function< / td >
< td >
Executed after wrapped method. Called with what the wrapped method
resolves with the hook’ s < code > options< / code > argument.
< / td >
< td > Yes< / td >
< / tr >
< / table >
Example
```js
2023-10-26 04:27:07 +08:00
hookCollection.after("save", function (result, options) {
2022-11-04 00:55:07 +08:00
if (result.updatedAt) {
2023-10-26 04:27:07 +08:00
app.emit("update", result);
2022-11-04 00:55:07 +08:00
} else {
2023-10-26 04:27:07 +08:00
app.emit("create", result);
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
});
2022-11-04 00:55:07 +08:00
```
### hookCollection.wrap()
Add wrap hook for given name.
```js
2023-10-26 04:27:07 +08:00
hookCollection.wrap(name, method);
2022-11-04 00:55:07 +08:00
```
< table >
< thead >
< tr >
< th > Argument< / th >
< th > Type< / th >
< th > Description< / th >
< th > Required< / th >
< / tr >
< / thead >
< tr >
< th align = "left" > < code > name< / code > < / th >
< td > String< / td >
< td > Hook name, for example < code > 'save'< / code > < / td >
< td > Yes< / td >
< / tr >
< tr >
< th align = "left" > < code > method< / code > < / th >
< td > Function< / td >
< td >
Receives both the wrapped method and the passed options as arguments so it can add logic before and after the wrapped method, it can handle errors and even replace the wrapped method altogether
< / td >
< td > Yes< / td >
< / tr >
< / table >
Example
```js
2023-10-26 04:27:07 +08:00
hookCollection.wrap("save", async function (saveInDatabase, options) {
2022-11-04 00:55:07 +08:00
if (!record.name) {
2023-10-26 04:27:07 +08:00
throw new Error("name property is required");
2022-11-04 00:55:07 +08:00
}
try {
2023-10-26 04:27:07 +08:00
const result = await saveInDatabase(options);
2022-11-04 00:55:07 +08:00
if (result.updatedAt) {
2023-10-26 04:27:07 +08:00
app.emit("update", result);
2022-11-04 00:55:07 +08:00
} else {
2023-10-26 04:27:07 +08:00
app.emit("create", result);
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
return result;
2022-11-04 00:55:07 +08:00
} catch (error) {
2023-10-26 04:27:07 +08:00
if (error.ignore) return;
throw error;
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
});
2022-11-04 00:55:07 +08:00
```
See also: [Test mock example ](examples/test-mock-example.md )
### hookCollection.remove()
Removes hook for given name.
```js
2023-10-26 04:27:07 +08:00
hookCollection.remove(name, hookMethod);
2022-11-04 00:55:07 +08:00
```
< table >
< thead >
< tr >
< th > Argument< / th >
< th > Type< / th >
< th > Description< / th >
< th > Required< / th >
< / tr >
< / thead >
< tr >
< th align = "left" > < code > name< / code > < / th >
< td > String< / td >
< td > Hook name, for example < code > 'save'< / code > < / td >
< td > Yes< / td >
< / tr >
< tr >
< th align = "left" > < code > beforeHookMethod< / code > < / th >
< td > Function< / td >
< td >
Same function that was previously passed to < code > hookCollection.before()< / code > , < code > hookCollection.error()< / code > , < code > hookCollection.after()< / code > or < code > hookCollection.wrap()< / code >
< / td >
< td > Yes< / td >
< / tr >
< / table >
Example
```js
2023-10-26 04:27:07 +08:00
hookCollection.remove("save", validateRecord);
2022-11-04 00:55:07 +08:00
```
## TypeScript
2023-10-26 04:27:07 +08:00
This library contains type definitions for TypeScript.
### Type support for `Singular`:
2022-11-04 00:55:07 +08:00
```ts
2023-10-26 04:27:07 +08:00
import { Hook } from "before-after-hook";
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
type TOptions = { foo: string }; // type for options
type TResult = { bar: number }; // type for result
type TError = Error; // type for error
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
const hook = new Hook.Singular< TOptions , TResult , TError > ();
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
hook.before((options) => {
// `options.foo` has `string` type
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
// not allowed
options.foo = 42;
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
// allowed
options.foo = "Forty-Two";
});
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
const hookedMethod = hook(
(options) => {
// `options.foo` has `string` type
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
// not allowed, because it does not satisfy the `R` type
return { foo: 42 };
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
// allowed
return { bar: 42 };
},
{ foo: "Forty-Two" }
);
```
You can choose not to pass the types for options, result or error. So, these are completely valid:
```ts
const hook = new Hook.Singular< O , R > ();
const hook = new Hook.Singular< O > ();
const hook = new Hook.Singular();
```
In these cases, the omitted types will implicitly be `any` .
### Type support for `Collection`:
`Collection` also has strict type support. You can use it like this:
```ts
import { Hook } from "before-after-hook";
type HooksType = {
add: {
Options: { type: string };
Result: { id: number };
Error: Error;
};
save: {
Options: { type: string };
Result: { id: number };
};
read: {
Options: { id: number; foo: number };
};
destroy: {
Options: { id: number; foo: string };
};
};
const hooks = new Hook.Collection< HooksType > ();
hooks.before("destroy", (options) => {
// `options.id` has `number` type
});
hooks.error("add", (err, options) => {
// `options.type` has `string` type
// `err` is `instanceof Error`
});
hooks.error("save", (err, options) => {
// `options.type` has `string` type
// `err` has type `any`
});
hooks.after("save", (result, options) => {
// `options.type` has `string` type
// `result.id` has `number` type
});
```
You can choose not to pass the types altogether. In that case, everything will implicitly be `any` :
```ts
const hook = new Hook.Collection();
2022-11-04 00:55:07 +08:00
```
2023-10-26 04:27:07 +08:00
Alternative imports:
2022-11-04 00:55:07 +08:00
```ts
2023-10-26 04:27:07 +08:00
import { Singular, Collection } from "before-after-hook";
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
const hook = new Singular();
const hooks = new Collection();
2022-11-04 00:55:07 +08:00
```
## Upgrading to 1.4
2023-10-26 04:27:07 +08:00
Since version 1.4 the `Hook` constructor has been deprecated in favor of returning `Hook.Singular` in an upcoming breaking release.
2022-11-04 00:55:07 +08:00
Version 1.4 is still 100% backwards-compatible, but if you want to continue using hook collections, we recommend using the `Hook.Collection` constructor instead before the next release.
For even more details, check out [the PR ](https://github.com/gr2m/before-after-hook/pull/52 ).
## See also
If `before-after-hook` is not for you, have a look at one of these alternatives:
- https://github.com/keystonejs/grappling-hook
- https://github.com/sebelga/promised-hooks
- https://github.com/bnoguchi/hooks-js
- https://github.com/cb1kenobi/hook-emitter
## License
[Apache 2.0 ](LICENSE )