lucasfeliciano / restling

Restling is a lightweight Node.js module for building promise-based asynchronous HTTP requests.
MIT License
56 stars 5 forks source link

<img src="http://promisesaplus.com/assets/logo-small.png" alt="Promises/A+ logo" title="Promises/A+ 1.1 compliant" align="right" />

NPM

Build Status Dependency Status NPM version

Restling

Restling is a lightweight Node.js module for building promise-based asynchronous HTTP requests. Under the hood it uses restler to make HTTP calls and bluebird to transform it in promises.

Avoid callback hell

Working on an asynchronous environment you have probably seen this:

Traditional callback approach
User.logIn("user", "pass", {
  success: function(user) {
    query.find({
      success: function(results) {
        results[0].save({ key: value }, {
          success: function(result) {
            // the object was saved.
          }
        });
      }
    });
  }
});
With Restling
User.logIn("user", "pass").then(function(user) {
  return query.find();
}).then(function(results) {
  return results[0].save({ key: value });
}).then(function(result) {
  // the object was saved.
});

Installing

npm install restling

Basic Usage

var rest = require('restling');

rest.get('http://google.com').then(function(result){
  console.log(result.data);
}, function(error){
  console.log(error.message);
});

The result passed into the success callback is an object with two keys: data and response. Example: {'data': 3, 'response': res}

The error passed into the error callback is an instance of Error with the attributes below:

Example:

  {
    'message'   : 'getaddrinfo ENOTFOUND',
    'statusCode': 404,
    'response:' : res,
    'data'      : result.data
  }

Parallel Request Basic Usage

Run requests in parallel, without waiting until the previous requests has completed.

To make this we will use a myRequest object which contains two keys:

Example of myRequest object: {'url': 'http://path/to/api', options:{timeout: 5000}}

Passing an object

Each property must be a myRequest object that will be executed. The return is a promise with a value that is a object containing each property of the object passed with their respective value.

var rest = require('restling');
var obj  = {'google':{'url':'http://google.com'},
            'api':{'url':'http://some/rest/api'}}

rest.settleAsync(obj).then(function(result){
  // handle result here
  // result is {google: responseFromGoogle, api: responseFromApi}
},function(err){
  // handle error here
});

Passing an array

It is also possible pass an array of myRequest object. THe return is a promise with a value a array which each index contain it respective value in order.

var rest  = require('restling');
var array = [{'url':'http://google.com'},
             {'url':'http://some/rest/api'}]

rest.settleAsync(array).then(function(result){
  // handle results here
  // result is [responseFromGoogle, responseFromApi]
},function(err){
  // handle error here
});

Using the allAsync instead settleAsync

If you want to reject the return promise for the whole if one of the parallel request is rejected use allAsync instead of settleAsync.

Features

API

request(url, options)

Basic method to make a request of any type. The function returns a promise object:

get(url, options)

Create a GET request.

post(url, options)

Create a POST request.

put(url, options)

Create a PUT request.

del(url, options)

Create a DELETE request.

head(url, options)

Create a HEAD request.

patch(url, options)

Create a PATCH request.

json(url, data, options)

Send json data via GET method.

postJson(url, data, options)

Send json data via POST method.

putJson(url, data, options)

Send json data via PUT method.

settleAsync(requestObjects, callback)

Run the requestObjects array/object in parallel, without waiting until the previous request has completed.

Parsers

You can give any of these to the parsers option to specify how the response data is deserialized. In case of malformed content, parsers emit error event. Original data returned by server is stored in response.raw.

parsers.auto

Checks the content-type and then uses parsers.xml, parsers.json or parsers.yaml. If the content type isn't recognised it just returns the data untouched.

parsers.json, parsers.xml, parsers.yaml

All of these attempt to turn the response into a JavaScript object. In order to use the YAML and XML parsers you must have yaml and/or xml2js installed.

Options

Example usage

var rest = require('./restling');

var successCallback = function(result){
console.log('Data: ' + result.data);
console.log('Response: ' + result.response);
};

var errorCallback = function(error){
  console.log('Error: ' + error.message);
  if (error.response) {
    console.log('Response: ' + error.response);
  }
};

rest.get('http://google.com').then(successCallback, errorCallback);

// auto convert json to object
rest.get('http://twaud.io/api/v1/users/danwrong.json')
  .then(successCallback, errorCallback);

// auto convert xml to object
rest.get('http://twaud.io/api/v1/users/danwrong.xml')
  .then(successCallback, errorCallback);

rest.get('http://someslowdomain.com',{timeout: 10000})
  .then(successCallback, errorCallback);

rest.post('http://user:pass@service.com/action', {
  data: { id: 334 },
}).then(function(result) {
  if (result.response.statusCode == 201) {
    result.data;// you can get at the raw response like this...
  }
},
errorCallback);

// multipart request sending a 321567 byte long file using https
rest.post('https://twaud.io/api/v1/upload.json', {
  multipart: true,
  username: 'danwrong',
  password: 'wouldntyouliketoknow',
  data: {
    'sound[message]': 'hello from restling!',
    'sound[file]': rest.file('doug-e-fresh_the-show.mp3', null, 321567, null, 'audio/mpeg')
  }
}).then(successCallback, errorCallback);

// post JSON
var jsonData = { id: 334 };
rest.postJson('http://example.com/action', jsonData).then(successCallback, errorCallback);

// put JSON
var jsonData = { id: 334 };
rest.putJson('http://example.com/action', jsonData).then(successCallback, errorCallback);

TODO