https://github.com/brickifyjs/module-middleware
Apply middleware to any methods/functions
https://github.com/brickifyjs/module-middleware
brickifyjs function hook method middleware module
Last synced: about 12 hours ago
JSON representation
Apply middleware to any methods/functions
- Host: GitHub
- URL: https://github.com/brickifyjs/module-middleware
- Owner: brickifyjs
- License: mit
- Created: 2017-09-17T08:23:21.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-01-26T23:45:32.000Z (over 8 years ago)
- Last Synced: 2025-11-23T01:07:58.877Z (7 months ago)
- Topics: brickifyjs, function, hook, method, middleware, module
- Language: JavaScript
- Homepage: https://m-middleware.js.brickify.io
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Module Middleware
Apply middleware to any methods/functions
## Statistics
[](https://github.com/brickifyjs/module-middleware)
[](https://www.npmjs.com/package/@brickify/m-middleware)
## Social
[](https://github.com/brickifyjs/module-middleware)
[](https://github.com/brickifyjs/module-middleware)
[](https://github.com/brickifyjs/module-middleware)
[](https://gitter.im/brickifyjs/module-middleware)
## Project Health
[](https://travis-ci.org/brickifyjs/module-middleware)
[](https://codecov.io/gh/brickifyjs/module-middleware)
[](https://www.bithound.io/github/brickifyjs/module-middleware/master/dependencies/npm)
[](https://www.bithound.io/github/brickifyjs/module-middleware/master/dependencies/npm)
[](https://m-middleware.js.brickify.io)
## Install
```bash
$ npm install @brickify/m-middleware
```
## Usage
```js
'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
```js
// 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