api-chain

A light and easy to use interface for creating fluent, chainable javascript APIs in Node.js or PhantomJs


When performing many asynchronous operations in javascript, nested callbacks can become difficult to read and maintain. Api-chain is a very light and easy to use interface for creating a fluent synchronous style API for control flow of asynchronous javascript. It is packaged as a commonJS module through NPM but not only works with Node.js, but has been tested and works with PhantomJS as well.

Methods can pass values down the chain by invoking next with them, e.g. next(null, value). Node.js callback style methods will be wrapped automatically so as to pass the results down the chain.

npm install api-chain
// require api-chain module
var api = require('api-chain');

// define your api by passing methods to create
var fs = api.create({
    read: require('fs').readFile,
    toString: function (data, next) {
        next(null, data.toString());
    },
    view: function (contents) {
        console.log(contents);
    }
});

// example using the chainable 'fs' API as created above
fs.read('./index.js')
  .toString()
  .view();

For more examples look in the examples subdirectory.

nametypedefaultdescription
onErrorfnundefinedcalled when error is emitted unless error method is overwritten
throwErrorsbooltruewhether to throw unhandled errors (ignored if onError exists)
continueErrorsboolfalsewhether to resume execution of commands after errors occur

api.wait(n) - pause execution flow for n milliseconds

api.until(fn) - wait until callback fn returns true before continuing execution flow

api.chain(fn) - add callback fn to be executed next in the control flow stack.

  • callback signature: fn([arg1,] [arg2,] ... [arg n,] [next])

api.set(name, value, [immediate]) - set api object property name to value

  • if the immediate flag is set, the value will be set immediately rather than within the control flow

api.setOption(option, value) - set option to value

api.setOptions(options) - set several options at once based on options collection of key/value pairs

to test you will need mocha and chai installed. Then just cd to the directory api-chain is installed in and type

mocha

MIT Style License - see license.txt