Code Monkey home page Code Monkey logo

gatesjs's Introduction

Gates.js

Build Status

Set specific callbacks for specific responses.

Usage

Install gates.js module:

$ npm install gatesjs --save

Include module in your project:

const gates = require("gatesjs");

Set up your gates in main request callback. Using set method pass response's code value:

new gates().set( response.statusCode )

Each gate function needs two parameters: array with condition rules and callback function. The first array's element is an expected status code (required, in example below 200), the second one is an optional expression (e.g. foo === true):

request("http://example.com/foo.json", (error, response, body) => {

  body = JSON.parse( body ); // => { foo: true }

  new gates().set( response.code )

       .gate([ 200 , body.foo === true ], () => console.log( "Hello 200 callback and true foo!" ) );

});

If there's a possibility the condition could not be met, set default callback using default method:

 .default( () => console.log( "Hello default callback!" ) );

For any kind of response codes or any expression use asterisk * argument:

 .gate([ "*", data.foo === false ], () => console.log( "Hello callback for whatever status and falsy foo!" ) )
 .gate([ 404, "*" ], () =>  console.log( "Hello callback for 404 status and whatever!" ) );

When you clone the repository check for more examples by launching $ node index.js after $ npm install.

License

MIT.

gatesjs's People

Contributors

piotrkabacinski avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

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.