Code Monkey home page Code Monkey logo

express-conditional-tree-middleware's Introduction

Express Conditional Tree Middleware

Hi, this tool was made in order for me to be able to combine different and asynchronous middleware using different conditions but also combine those conditions with other middleware and other conditions.

I am dedicated to create scalable and easy to use tools, so this is one of them and I thought it would be nice if I could share it with you.

TL;DR

Create a tree of middleware nodes that may either be middlewares or combination of middlewares.

Note: The tree is parsed in a DFS fashion.

Important Note: And Chainers are executed sequentially. OrChainers are using the Promise.any and as a result they are executed in parallel. I want to implement them soon. If anyone wants to contribute please go ahead :)

Usage Example

import { conditionalMiddleware, MiddlewareChainer, OrMiddlewareStrategy, AndMiddlewareStrategy } from 'express-conditional-tree-middleware';

// Every Node class must have a applyMiddleware method in order to be used by a chainer

class IPLimitMiddlewareNode {    

    applyMiddleware(app) {
        return (req, res, next) => {
            return new Promise((resolve, reject) => {
                if(app.get('myIPs').indexOf(req.ip) > -1) return resolve();
                else reject();
            });
        };
    }

}

class AdminMiddlewareNode {

    applyMiddleware(app) {
        return (req, res, next) => {
            return new Promise((resolve, reject) => {
                if(req.body.admin === true) return resolve();
                else reject();
            });
        };
    }

}

class TokenVerificationMiddlewareNode {

    applyMiddleware(app) {
        return (req, res, next) => {
            return new Promise((resolve, reject) => {
                if(token.verify()) return resolve();
                reject();
            });
        }
    }
    
}


let myRouter = express.Router();

// Create two new strategies
let orChainer = new MiddlewareChainer(new OrMiddlewareStrategy());
let andChainer = new MiddlewareChainer(new AndMiddlewareStrategy());

// Create the tree
andChainer.add(new TokenVerificationMiddlewareNode());
andChainer.add(new AdminMiddlewareNode());
orChainer.add(andChainer);
orChainer.add(new IPLimitMiddlewareNode());

//
//               Tree Structure
//
//                     Or
//                   /   \
//                  /     \
//                 And    IpLimit 
//               /    \
//              /      \
// TokenVerification   Admin
//

myRouter.use(
  // conditionalMiddleware functions take as arguments the express app, a router that will apply 
  // the middleware tree, the root element of the tree and a callback error function
  conditionalMiddleware(app, myRouter, orChainer, function(res) {
      res.status(401).json({status: 'Unauthorized'});
  })
)

Design Patterns Used

  • Strategy
  • Composite

License

MIT

express-conditional-tree-middleware's People

Contributors

dev-force avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

express-conditional-tree-middleware's Issues

Info

Hi,

Is the library still in use? Is there any issue?

Whats the current status?

Regards

Julio

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.