0.0.3 • Published 8 years ago

debate v0.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

☯ debate ☯


debate is a promised based web service client. The goal for this project is to provide several client types but initially REST is the only supported protocol.


Documentation


debate.rest.get( URL, options )

debate.rest.head( URL, options )

debate.rest.post( URL, options, body )

debate.rest.put( URL, options, body )

debate.rest.del( URL, options )

debate.rest.custom( URL, method, options, body )

Ouput


All API methods output an object containing

  • request - The complete request object
  • response - The complete response object
  • code - The HTTP status code
  • rawData - The unparsed/unformatted response data
  • data - The parsed/formatted data

Formatters


A an additional formatters option can be added to the options hash. The key should be the content-type and its value should be a function that takes rawData as its only argument and returns the formatted data/object. By default a formatter for application/json is added if not specified.


// get request client.rest.get('http://localhost:8080/api/resource').then(function(res) { console.log(res.status, res.data); });

// post request var body = { name: 'John Doe' };

client.rest.post('http://localhost:8080/api/resource', null, body).then(function(res) { console.log(res.status, res.data); });

<br>
## Example with Custom formatter
---
```js
// require debate
var client = require('debate');

// create custom options
var options = {
    formatters: {
        'application/json+custom': function(data) {
            try {
                var parsed = JSON.parse(data);
                delete parsed.id;
                return parsed;
            }
            catch(err) {
                return data;
            }
        }
    }
};

// get request
client.rest.get('http://localhost:8080/api/resource', options).then(function(res) {
    console.log(res.status, res.data);
});

Nodeclipse is free open-source project that grows with your contributions.