Type Alias: HookHandler()<Args, CleanUpArgs>
ts
type HookHandler<Args, CleanUpArgs> = (
...args: Args
) =>
| void
| CleanupHandler<CleanUpArgs>
| Promise<void>
| Promise<CleanupHandler<CleanUpArgs>>;Shape of the hook handler function.
Hook handlers are executed when a specific event is triggered. They can optionally return a cleanup handler to perform cleanup operations later.
Type Parameters
| Type Parameter | Description |
|---|---|
Args extends any[] | The arguments passed to the hook handler |
CleanUpArgs extends any[] | The arguments that will be passed to the cleanup handler |
Parameters
| Parameter | Type |
|---|---|
...args | Args |
Returns
| void | CleanupHandler<CleanUpArgs> | Promise<void> | Promise<CleanupHandler<CleanUpArgs>>
Example
ts
// Simple hook handler
const hashPassword: HookHandler<[User], [Error | null]> = (user) => {
user.password = hash(user.password);
};
// Hook handler with cleanup
const writeTemp: HookHandler<[Model], [Error | null]> = async (model) => {
await fs.writeFile("./temp.json", JSON.stringify(model));
// Return cleanup handler
return async (error) => {
if (error) {
await fs.unlink("./temp.json");
}
};
};