js-middleware makes your JS codes as scalable as ReduxJS

Written by unbug | Published 2017/04/13
Tech Story Tags: functional-programming | middleware | middleware-pattern | redux-middleware

TLDRvia the TL;DR App

js-middleware

Powerful Javascript Middleware Pattern implementation, apply middleweares to any object. A painless solution to make codes as scalable and maintainable as ReduxJS and ExpressJS.

Links

Overview

Middleware functions are functions that have access to the target function and it’s arguments, and the target object and the next middleware function in the target function cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the function’s arguments.
  • End the target function.
  • Call the next middleware in the stack.

If the current middleware function does not end the target function cycle, it must call next() to pass control to the next middleware function. Otherwise, the target function will be left hanging.

Get started

  1. window.MiddlewareManager is available for browsers by include [dist/middleware.min.js](https://github.com/unbug/js-middleware/tree/master/dist) file in your HTML.

    <script src="middleware.min.js"></script>

  2. Or install the package

    npm install --save js-middleware

and import it in your files

import {MiddlewareManager} from 'js-middleware';

Usages

Basic

We define a Person class.

// the target objectclass Person {  // the target function  walk(step) {    this.step = step;  }    speak(word) {    this.word = word;  } }

Then we define a middleware function to print log.

// middleware for walk function const logger = target => next => (...args) => {    console.log(`walk start, steps: ${args[0]}.`);    const result = next(...args);    console.log(`walk end.`);    return result;  }

Now we apply the log function as a middleware to a Person instance.

// apply middleware to target object const p = new Person(); const middlewareManager = new MiddlewareManager(p); middlewareManager.use('walk', walk); p.walk(3);

Whenever a Person instance call it’s walk method, we’ll see logs from the looger middleware.

Middleware object

We can also apply a middleware object to a target object. Middleware object is an object that contains function’s name as same as the target object’s function name. Function’s name start or end with “_” will not be able to apply middleware.

const PersonMiddleware {  walk: target => next => step => {    console.log(`walk start, steps: step.`);    const result = next(step);    console.log(`walk end.`);    return result;  },  speak: target => next => word => {    word = 'this is a middleware trying to say: ' + word;    return next(word);  }}

 // apply middleware to target object const p = new Person(); const middlewareManager = new MiddlewareManager(p); middlewareManager.use(PersonMiddleware); p.walk(3); p.speak('hi');

middlewareMethods

Or we can use middlewareMethods to define function names for middleweare target within a class.

class PersonMiddleware {  constructor() {    /**     * Define function names for middleweare target.     * @type {Array}     */    this.middlewareMethods = ['walk', 'speak'];  }  log(text) {    console.log('Middleware log: ' + text);  }  walk(target) {    return next => step => {      this.log(`walk start, steps: step.`);      const result = next(step);      this.log(`walk end.`);      return result;    }  }  speak(target) {    return next => word => {      this.log('this is a middleware tring to say: ' + word);      return next(word);    }  }}

 // apply middleware to target object const p = new Person(); const middlewareManager = new MiddlewareManager(p); middlewareManager.use(new PersonMiddleware()) p.walk(3); p.speak('hi');

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!


Published by HackerNoon on 2017/04/13