Skip to content

JacksonTian/hookx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hookx

Hook(x) with Power.

NPM version npm download build status codecov

Installation

Install it with npm first.

$ npm install hookx --save

Usage

Require it first.

const { hook, before, after } = require('hookx');

You should have an async function defined like this:

var query = async function (sql) {
  // TODO
};

Hook it with before and after business logic.

var query = hook(query, async function (sql) {
  console.time(sql);
}, async function (sql) {
   console.timeEnd(sql);
});
await query(sql); // call the hooked async function like origin

Hook with before business logic.

var counter = 0;
var query = before(query, async function (sql) {
  counter++;
});
await query(sql);

Hook with after business logic.

var counter = 0;
var query = after(query, async function (sql) {
  counter--;
});
await query(sql);

License

The MIT license.