Skip to content

Apply to all functions #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions dist/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ exports.MiddlewareManager = void 0;

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

Expand Down Expand Up @@ -141,7 +145,19 @@ function compose() {
* middlewareManager.use('walk', logger);
* p.walk(3);
*
* Whenever a Person instance call it's walk method, we'll see logs from the looger middleware.
* Whenever a Person instance call it's walk method, we'll see logs from the logger middleware.
*
* ## Apply to all
*
* Take the same example as above but do not specify the walk method instead pass null.
* // apply middleware to target object
* const p = new Person();
* const middlewareManager = new MiddlewareManager(p);
* middlewareManager.use(null, logger);
* p.walk(3);
* p.speak('hello');
*
* Whenever a Person instance calls any of it's methods, we'll see logs from the logger middleware.
*
* ## Middleware object
* We can also apply a middleware object to a target object.
Expand Down Expand Up @@ -209,9 +225,7 @@ function compose() {
*/


var MiddlewareManager =
/*#__PURE__*/
function () {
var MiddlewareManager = /*#__PURE__*/function () {
/**
* @param {object} target The target object.
* @param {...object} middlewareObjects Middleware objects.
Expand Down Expand Up @@ -270,7 +284,9 @@ function () {
}

middlewares.forEach(function (middleware) {
return typeof middleware === 'function' && _this._methodMiddlewares[methodName].push(middleware(_this._target));
if (typeof middleware === 'function') {
_this._methodMiddlewares[methodName].push(middleware(_this._target, methodName));
}
});
this._target[methodName] = compose.apply(void 0, _toConsumableArray(this._methodMiddlewares[methodName]))(method.bind(this._target));
}
Expand All @@ -280,7 +296,8 @@ function () {
* Apply (register) middleware functions to the target function or apply (register) middleware objects.
* If the first argument is a middleware object, the rest arguments must be middleware objects.
*
* @param {string|object} methodName String for target function name, object for a middleware object.
* @param {string|object|null} methodName String for target function name, object for a middleware object,
* null will apply the middlewares to all methods on the target.
* @param {...function|...object} middlewares The middleware chain to be applied.
* @return {object} this
*/
Expand All @@ -294,7 +311,7 @@ function () {
middlewares[_key4 - 1] = arguments[_key4];
}

if (_typeof(methodName) === 'object') {
if (methodName !== null && _typeof(methodName) === 'object') {
Array.prototype.slice.call(arguments).forEach(function (arg) {
// A middleware object can specify target functions within middlewareMethods (Array).
// e.g. obj.middlewareMethods = ['method1', 'method2'];
Expand All @@ -303,6 +320,14 @@ function () {
typeof arg[key] === 'function' && _this2._methodIsValid(key) && _this2._applyToMethod(key, arg[key].bind(arg));
});
});
} else if (methodName === null) {
var proto = Object.getPrototypeOf(this._target);
var methodNames = Object.getOwnPropertyNames(proto).filter(function (key) {
return typeof proto[key] === 'function' && key !== 'constructor';
});
methodNames.forEach(function (methodName) {
return _this2._applyToMethod.apply(_this2, [methodName].concat(middlewares));
});
} else {
this._applyToMethod.apply(this, [methodName].concat(middlewares));
}
Expand Down
2 changes: 1 addition & 1 deletion dist/middleware.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ If the first argument is a middleware object, the rest arguments must be middlew

| Param | Type | Description |
| --- | --- | --- |
| methodName | <code>string</code> \| <code>object</code> | String for target function name, object for a middleware object. |
| methodName | <code>string</code> \| <code>object</code> \| <code>null</code> | String for target function name, object for a middleware object, null will apply the middlewares to all methods on the target. |
| ...middlewares | <code>function</code> \| <code>object</code> | The middleware chain to be applied. |

<a name="compose"></a>
Expand Down
22 changes: 19 additions & 3 deletions docs/html/MiddlewareManager.html
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,19 @@ <h5>Example</h5>
middlewareManager.use(&#x27;walk&#x27;, logger);
p.walk(3);

Whenever a Person instance call it&#x27;s walk method, we&#x27;ll see logs from the looger middleware.
Whenever a Person instance call it&#x27;s walk method, we&#x27;ll see logs from the logger middleware.

## Apply to all

Take the same example as above but do not specify the walk method instead pass null.
// apply middleware to target object
const p = new Person();
const middlewareManager = new MiddlewareManager(p);
middlewareManager.use(null, logger);
p.walk(3);
p.speak(&#x27;hello&#x27;);

Whenever a Person instance calls any of it&#x27;s methods, we&#x27;ll see logs from the logger middleware.

## Middleware object
We can also apply a middleware object to a target object.
Expand Down Expand Up @@ -383,6 +395,9 @@ <h5>Parameters:</h5>
|

<span class="param-type">object</span>
|

<span class="param-type">null</span>



Expand All @@ -401,7 +416,8 @@ <h5>Parameters:</h5>



<td class="description last"><p>String for target function name, object for a middleware object.</p></td>
<td class="description last"><p>String for target function name, object for a middleware object,
null will apply the middlewares to all methods on the target.</p></td>
</tr>


Expand Down Expand Up @@ -576,7 +592,7 @@ <h4 class="modal-title">Search results</h4>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>

on 2020-02-10
on 2020-05-20

using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
Expand Down
2 changes: 1 addition & 1 deletion docs/html/MiddlewareManager_MiddlewareManager.html
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ <h4 class="modal-title">Search results</h4>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>

on 2020-02-10
on 2020-05-20

using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
Expand Down
2 changes: 1 addition & 1 deletion docs/html/classes.list.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ <h4 class="modal-title">Search results</h4>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>

on 2020-02-10
on 2020-05-20

using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
Expand Down
2 changes: 1 addition & 1 deletion docs/html/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h4 class="modal-title">Search results</h4>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>

on 2020-02-10
on 2020-05-20

using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
Expand Down
2 changes: 1 addition & 1 deletion docs/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ <h4 class="modal-title">Search results</h4>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>

on 2020-02-10
on 2020-05-20

using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
Expand Down
Loading