2.0.1 • Published 7 years ago

simple-chain v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

Node SimpleChain

ES6 node.js module for creating validation chains.

Installation

npm install simple-chain

Usage

const
    SimpleChain        = require('simple-chain'),
    MyMiddlewareChain  = new SimpleChain();

function pay(name,amount) {
    MyMiddlewareChain.run('pay',{
        to              : name,
        amount          : amount
    })
    .then((data) => {
        // Success! Do whatever needed
        console.log(`Ok, instead of US$${amount} will send US$${data.amount} (tax incl.) to ${data.to}`);
    })
    .catch((err) => {
        // Oh oh... something went wrong in the validation chain
        console.log(`Cannot send US$${amount} to ${name}. Error: ${err}`);
    });
}

// Validators

function plusTaxes(data) {
    if (data.amount > 50) return Promise.reject(`There's a maximum of US$ 50 per payment.`);
    data.amount *= 1.07; // add taxes

    return data;
}

MyMiddlewareChain.middleware('pay',plusTaxes);

pay('Martin',30);
// => Ok, instead of US$30 will send US$32.1 (tax incl.) to Martin

pay('Martin',52);
// => Cannot send US$52 to Martin. Error: There's a maximum of US$ 50 per payment.

Methods

  • SimpleChain.run ( String scope , Object data ) ⇒ Promise
  • SimpleChain.middleware ( String scope , Function middleware ) ⇒ SimpleChain
  • SimpleChain.middleware ( String scope , Number priority , Function middleware ) ⇒ SimpleChain
  • SimpleChain.removeMiddleware ( String scope , Function middleware ) ⇒ SimpleChain
  • SimpleChain.removeMiddleware ( String scope , Number priority , Function middleware ) ⇒ SimpleChain

License

The MIT License (MIT)

Copyright (c) 2016 Martín Rafael González

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2.0.1

7 years ago

2.0.0

7 years ago

1.0.0

7 years ago