h00k

1.1.2 • Public • Published

h00k

Hook-in any function to be executed before any function gets executed.

Install

$ npm install --save h00k

Usage

Variant 1

Without next():

var mongodb = require('mongodb');
var h00k = require('h00k');

h00k.before(mongodb.Collection, 'insert', function() {
  console.log('before doing any insert');

  // The 'real' insert automatically gets executed.
});

With next(): for async stuff in the before.

var mongodb = require('mongodb');
var h00k = require('h00k');

h00k.before(mongodb.Collection, 'insert', function(next) {
  console.log('before doing any insert');

  // The 'real' insert will get executed when next is called.
  // This next() should be called inside the callback of any asynchronous code
  // which we want to know done before the 'real' insert gets executed.
  next();
});

With next() and parameters of the 'real' method

var mongodb = require('mongodb');
var h00k = require('h00k');

h00k.before(mongodb.Collection, 'insert', function(next, query) {
  console.log('the query going to be executed will be:', query);

  // The 'real' insert will get executed when next is called.
  // This next() should be called inside the callback of any asynchronous code
  // which we want to know done before the 'real' insert gets executed.
  next();
});

Without next() but with parameters of the 'real' method

var mongodb = require('mongodb');
var h00k = require('h00k');

h00k.before(mongodb.Collection, 'insert', function(query) {
  console.log('the query going to be executed will be:', query);
});
Variant 2

Any Function's execution can also be prepended with a hook using the following method:

var mongodb = require('mongodb');
require('h00k'); // or var h00k = require('h00k'); but not necessary in this variant.

mongodb.Collection.before('insert', function(next, query) {
  console.log('the query going to be executed will be:', query);

  next();
});

API

h00k.before(root, 'func', function([next], *parameters) {})

  • Execute function before executing func on root.
  • function's parameters accept a next-function to be called inside an async operation to execute the real-func.
  • function's parameter also accept the real-func's original parameters.

Function.before('func', function([next], *parameters) {})

  • Same as above, but a few bits 💥 shorter.

Related

License

MIT © Linus Gubenis

Package Sidebar

Install

npm i h00k

Weekly Downloads

1

Version

1.1.2

License

MIT

Last publish

Collaborators

  • elgubenis