Gathering detailed insights and metrics for @poppinss/hooks
Gathering detailed insights and metrics for @poppinss/hooks
Gathering detailed insights and metrics for @poppinss/hooks
Gathering detailed insights and metrics for @poppinss/hooks
npm install @poppinss/hooks
Update dependencies
Published on 14 Sept 2024
Update dependencies
Published on 28 Mar 2024
Updating dependencies
Published on 16 Dec 2023
Publish source maps and use TSC for generating types
Published on 06 Nov 2023
Breaking changes + Rewrite to ESM
Published on 14 Oct 2023
Add option to run hooks in reverse order
Published on 09 Oct 2023
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
14 Stars
87 Commits
3 Forks
5 Watching
2 Branches
4 Contributors
Updated on 14 Sept 2024
TypeScript (99.68%)
JavaScript (0.32%)
Cumulative downloads
Total Downloads
Last day
3.2%
9,486
Compared to previous day
Last week
7.9%
53,571
Compared to previous week
Last month
1.8%
222,151
Compared to previous month
Last year
51.5%
2,394,971
Compared to previous year
A simple yet effective implementation for executing hooks around an event.
This package is a zero-dependency implementation for running lifecycle hooks around an event. Following are some of the notable features.
Install the package from the npm packages registry.
1npm i @poppinss/hooks 2 3# yarn lovers 4yarn add @poppinss/hooks
And import the Hooks
class as follows.
1import Hooks from '@poppinss/hooks' 2 3const hooks = new Hooks() 4 5hooks.add('saving', function () { 6 console.log('called') 7}) 8 9// Execute hooks using runner 10await hooks.runner('saving').run()
The hooks are defined using the hooks.add
method. The method accepts the event name and a callback function to execute.
1const hooks = new Hooks() 2 3hooks.add('saving', function () { 4 console.log('called') 5})
You can also define hook as an object with the name
and the handle
method property. This is usually helpful when you want to specify a custom name for the hook, or re-use the same handle method multiple times.
1const hooks = new Hooks() 2 3function handleSave() {} 4 5hooks.add('saving', { name: 'beforeSave', handle: handleSave }) 6hooks.add('creating', { name: 'beforeCreate', handle: handleSave })
The handle
method receives the first argument as the event name, followed by the rest of the arguments supplied during runtime.
You can execute hooks using the Hooks Runner. You can create a new runner instance by calling the hooks.runner
method and passing the event name for which you want to execute hooks.
1const hooks = new Hooks() 2 3const runner = hooks.runner('saving') 4await runner.run()
To run hooks in the reverse order, you can use the runner.runReverse
method.
1const hooks = new Hooks() 2 3const runner = hooks.runner('saving') 4await runner.runReverse()
You can pass one or more arguments to the runner.run
method, which the runner will share with the hook callbacks. For example:
1const hooks = new Hooks() 2 3hooks.add('saving', function (model, transaction) {}) 4 5const runner = hooks.runner('saving') 6await runner.run(model, transaction)
Cleanup functions allow hooks to clean up after themselves after the main action finishes successfully or with an error. Let's consider a real-world example of saving a model to the database.
saving
hooks.saving
hooks writes some files to the disk.Following is how you can express that with cleanup functions.
1hooks.add('saving', function () { 2 await fs.writeFile() 3 4 // Return the cleanup function 5 return (error) => { 6 // In case of an error, remove the file 7 if (error) { 8 await fs.unlink() 9 } 10 } 11})
The code responsible for issuing the insert query should run hooks as follows.
1const runner = hooks.runner('saving') 2 3try { 4 await runner.run(model) 5 await model.save() 6} catch (error) { 7 // Perform cleanup and pass error 8 await runner.cleanup(error) 9 throw error 10} 11 12// Perform cleanup in case of success as well 13await runner.cleanup()
Note: The
runner.cleanup
method is idempotent. Therefore you can call it multiple times, yet it will run the underlying cleanup methods only once.
You can exclude certain hook handlers from executing using the without
method.
In the following example, we run hooks without executing the generateDefaultAvatar
hook handler. As you can notice, you can specify the function name as a string.
1hooks.add('saving', function hashPassword() {}) 2hooks.add('saving', function generateDefaultAvatar() {}) 3 4await hooks.runner('saving').without(['generateDefaultAvatar']).run()
You can also specify the types of supported events and their arguments well in advance as follows.
The first step is to define a type for all the events.
1type Events = { 2 saving: [ 3 [BaseModel], // for hook handler 4 [error: Error | null, BaseModel], // for cleanup function 5 ] 6 finding: [ 7 [QueryBuilder], // for hook handler 8 [error: Error | null, QueryBuilder], // for cleanup function 9 ] 10}
And then pass it as a generic to the Hooks
class.
1const hooks = new Hooks<Events>()
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More