Gathering detailed insights and metrics for @poppinss/hooks
Gathering detailed insights and metrics for @poppinss/hooks
npm install @poppinss/hooks
Typescript
Module System
Min. Node Version
Node Version
NPM Version
98.7
Supply Chain
100
Quality
84.4
Maintenance
100
Vulnerability
100
License
Update dependencies
Updated on Dec 27, 2024
Update dependencies
Updated on Sep 14, 2024
Update dependencies
Updated on Mar 28, 2024
Updating dependencies
Updated on Dec 16, 2023
Publish source maps and use TSC for generating types
Updated on Nov 06, 2023
Breaking changes + Rewrite to ESM
Updated on Oct 14, 2023
TypeScript (99.68%)
JavaScript (0.32%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
5,624,130
Last Day
8,779
Last Week
60,624
Last Month
246,753
Last Year
2,531,275
MIT License
14 Stars
90 Commits
3 Forks
5 Watchers
2 Branches
4 Contributors
Updated on Dec 27, 2024
Latest Version
7.2.5
Package Id
@poppinss/hooks@7.2.5
Unpacked Size
17.53 kB
Size
5.88 kB
File Count
9
NPM Version
10.8.2
Node Version
20.18.1
Published on
Dec 27, 2024
Cumulative downloads
Total Downloads
Last Day
-9.4%
8,779
Compared to previous day
Last Week
9.2%
60,624
Compared to previous week
Last Month
32.9%
246,753
Compared to previous month
Last Year
45.3%
2,531,275
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
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
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 2025-02-10
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