1.1.0 • Published 6 years ago

@brickify/m-middleware v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Module Middleware

Apply middleware to any methods/functions

Statistics

Github All Releases npm

Social

GitHub forks GitHub stars GitHub watchers Gitter

Project Health

Travis branch Codecov bitHound bitHound Website

Install

$ npm install @brickify/m-middleware

Usage

'use strict';

// Import lib
var lib = require('@brickify/m-middleware');

// Assign methods
var mw = lib.middleware;
var disable = lib.disable;
var enable = lib.enable;
var toggle = lib.toggle;
var remove = lib.remove;

// Namespace method
var ns = {
  foo: function (arg0) {
    return arg0 + 1;
  }
};

// First middleware
function foo(next, arg0) {
  return next(arg0 + 1);
}

// Attach NS to process for later
process.ns = ns;


// Attach middleware, Can be attached by an object path without context
mw('ns.foo', foo);
// Run method
ns.foo(1);
// output: 3

// Attach middleware, Can be attached by an object path with context
mw('ns.foo', foo, {
   context: ns
});
// Run method
ns.foo(1);
// output: 4  

// Attach middleware, Can be a attached to a method
ns.foo = mw(ns.foo, foo);
// Run method
ns.foo(1);
// output: 5  

// Attach middleware, Can have a specific index
ns.foo = mw(ns.foo, foo, {
   index: 6
});
// Run method
ns.foo(1);
// output: 6

// Attach middleware, Can have a fewer index to be at first
ns.foo = mw(ns.foo, foo, {
   index: -1
});
// Run method
ns.foo(1);
// output: 7

// Attach middleware, Can have a bigger index to be at last
ns.foo = mw(ns.foo, foo, {
   index: 20
});
// Run method
ns.foo(1);
// output: 8 

// Attach middleware, Can be overrided if already exist
ns.foo = mw(ns.foo, foo, {
   index: 0,
   override: true
});
// Run method
ns.foo(1);
// output: 8 

// Attach middleware, Can be disabled on attached
ns.foo = mw(ns.foo, foo, {
   disabled: true
});
// Run method
ns.foo(1);
// output: 8 

// Attached middleware, Can be programmatically disabled
disable(ns.foo, foo);
// Run method
ns.foo(1);
// output: 7 

// Attached middleware, Can be programmatically enabled
enable(ns.foo, foo);
// Run method
ns.foo(1);
// output: 8

// Attached middleware, Can be programmatically toggled
toggle(ns.foo, foo);
// Run method
ns.foo(1);
// output: 7 

// Attached middleware, Can be programmatically removed
remove(ns.foo, foo); // Remove disabled one... because of same function assigned previously
remove(ns.foo, foo);
// Run method
ns.foo(1);
// output: 6 

Imports

// Import all methods
var lib = require('@brickify/m-middleware');

// Import enable method
var enable = require('@brickify/m-middleware/enable');

// Import disable method
var disable = require('@brickify/m-middleware/disable');

// Import toggle method
var toggle = require('@brickify/m-middleware/toggle');

// Import remove method
var remove = require('@brickify/m-middleware/remove');

TODO

  • Add JSDoc, comment and Code Signature