1.0.4 • Published 7 years ago

gatesjs v1.0.4

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

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 status 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.

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago