# express-conditional-tree-middleware

> A module that helps to create a tree of middlewares and apply strategies on each node (logical operators) in order to build recursively from bottom to top of the tree the result.

Latest version **1.0.4** (published 2016-11-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-conditional-tree-middleware
pnpm add express-conditional-tree-middleware
yarn add express-conditional-tree-middleware
bun add express-conditional-tree-middleware
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2016-11-26 |
| First published | 2016-11-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Tsaganos Apostolos |
| Maintainers | devforce |
| Keywords | expressjs, middleware, conditional, tree, asynchronous |

## Links

- npm: https://www.npmjs.com/package/express-conditional-tree-middleware
- Repository: https://github.com/Dev-Force/express-conditional-tree-middleware
- Homepage: https://github.com/Dev-Force/express-conditional-tree-middleware#readme
- Issues: https://github.com/Dev-Force/express-conditional-tree-middleware/issues
- npm.io page: https://npm.io/package/express-conditional-tree-middleware

## Dependencies (1)

- [bluebird](https://npm.io/package/bluebird.md) ^3.4.6

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2016-11-26
- 1.0.3 — 2016-11-26
- 1.0.2 — 2016-11-25
- 1.0.1 — 2016-11-25
- 1.0.0 — 2016-11-20

## README

# 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


### Usage Example

```javascript
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
* Composition


### License

MIT

---
_Source: https://npm.io/package/express-conditional-tree-middleware · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
