Gathering detailed insights and metrics for hook-fn
Gathering detailed insights and metrics for hook-fn
A library which provides pre-execution and post-execution hooks both as a decorator and as a higher-order function.
npm install hook-fn
Typescript
Module System
Node Version
NPM Version
65.7
Supply Chain
97.8
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (79.64%)
JavaScript (17.91%)
Shell (2.45%)
Total Downloads
7,188
Last Day
12
Last Week
45
Last Month
563
Last Year
5,920
6 Stars
27 Commits
2 Watching
1 Branches
2 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.6
Package Id
hook-fn@1.0.6
Unpacked Size
10.81 kB
Size
3.83 kB
File Count
8
NPM Version
10.2.3
Node Version
20.10.0
Publised On
27 Dec 2023
Cumulative downloads
Total Downloads
Last day
-29.4%
12
Compared to previous day
Last week
-27.4%
45
Compared to previous week
Last month
134.6%
563
Compared to previous month
Last year
597.3%
5,920
Compared to previous year
This is an implementation (usable both as a decorator and as a higher-order function) that can be used to hook into a function execution and allows you to do something before and after such execution.
It can be used both with sync
and async
functions.
You can install it by using the following command:
1npm install hook-fn
1import { Hook } from 'hook-fn'; 2 3class MyClass { 4 @Hook({ 5 before: ({context, args, target, propertyKey, descriptor}) => { 6 console.log('Before'); 7 }, 8 after: ({context, args, target, propertyKey, descriptor, result}) => { 9 console.log('After'); 10 }, 11 }) 12 public myMethod(): void { 13 console.log('Hello World!'); 14 } 15}
1import { hook } from 'hook-fn'; 2 3const testFn = function (str) { 4 return str; 5}; 6 7const mockFn = hook({ 8 before: ({context, args}) => { 9 console.log('Before'); 10 }, 11 after: ({context, args, result}) => { 12 console.log('After'); 13 } 14}); 15 16const mockedTestFn = mockFn(testFn); 17 18mockedTestFn('hello world');
When wrapping an async function with hook-fn, be aware that the after
hook is executed immediately after the function execution (without waiting for the Promise
fulfillment), so a Promise
will be passed to the result
argument, despite its resolution or rejection.
If you want to execute something after the async
function is resolved or rejected, you can use the then
or catch
methods of the returned promise.
1import { hook } from 'hook-fn'; 2 3const testFn = async function (str) { 4 return new Promise((resolve, reject) => { 5 setTimeout(() => { 6 resolve(str); 7 }, 1000); 8 }); 9}; 10 11const mockFn = hook({ 12 before: ({context, args}) => { 13 console.log('Before'); 14 }, 15 after: ({context, args, result}) => { 16 result.then((res) => { 17 console.log('After'); 18 }); 19 } 20}); 21 22const mockedTestFn = mockFn(testFn); 23 24mockedTestFn('hello world');
The decorator will provide two functions to be executed before and after the method to which it is applied.
It expects an object with the following properties:
before
: A function to be executed before the method to which it is applied. Defaults to a function that does nothing. It will be called with the following parameters:
context
: The context of the method to which it is appliedargs
: The arguments passed to the method to which it is appliedtarget
: The method to which it is appliedpropertyKey
: The name of the method to which it is applieddescriptor
: The descriptor of the method to which it is applied.after
: A function to be executed after the method to which it is applied. Defaults to a function that does nothing. It will be called with the following parameters:
context
: The context of the method to which it is appliedargs
: The arguments passed to the method to which it is appliedtarget
: The method to which it is appliedpropertyKey
: The name of the method to which it is applieddescriptor
: The descriptor of the method to which it is applied.result
: The result of the method to which it is applied. If the method is async
, it will be a Promise
.The higher-order function will provide two functions to be executed before and after the method to which it is applied.
It expects an object with the following properties:
before
: A function to be executed before the method to which it is applied. It will be called with the following parameters:
context
: The context of the method to which it is appliedargs
: The arguments passed to the method to which it is appliedafter
: A function to be executed after the method to which it is applied. It will be called with the following parameters:
context
: The context of the method to which it is appliedargs
: The arguments passed to the method to which it is appliedresult
: The result of the method to which it is applied. If the method is async
, it will be a Promise
.bind
in the Hook context
passed to the before
and after
functions?We could have used bind
to provide the context to the functions, but we prefered not to do so because it would have needed the developers using our library to know about the context of the method to which it is applied and the implications of using bind
in such a way.
You can run the tests by using the following command:
1npm test
No vulnerabilities found.
No security vulnerabilities found.