npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

decanat-request

0.3.1 • Public • Published

Request Build Status

XHR wrapper, with Node-style callbacks.

var Request = require('decanat-request');
// instantiate
var request = new Request('post', 'http://example.com/');
 
request
    .data({ firstname: 'Anna', lastname: 'Sedokova' })
    .send(callback);
 
function callback(err, data, status) {
    if (err) throw err;
    //    
}

Installation

Install with component:

$ component install decanat/request

Install with npm:

$ npm install decanat-request

API

Headers

req.header('X-CSRF-Token', 't0ken');
req.header({ 'X-Hello': 'Moto' });
 
req.headers; // { 'x-csrf-token': 't0ken', 'x-hello': 'moto', ... }

Data

var req = Request('post', 'http://example.com/bangs');
 
req
    .data('She', 'looks')
    .data('like=no%20one&in=history')
    .data({ 'She': 'stings', 'like': 'every girl' });
 
req.body; // '{"She":"stings","like":"every girl","in":"history"}'

Body will be attached to querystring on GET and HEAD requests. So if previous request was initialized with 'get' as method, we'd have:

req.body; // null
req.url; // 'http://example.com/bangs?She=stings&like=every%20girl&in=history'

Callbacks

Callback function can be supplied as argument, when calling .send method:

req.send(function(err, res, data){
    if (err) throw err;
    // body..
});

Or using .complete, which saves callback to be invoked when XHR request will be loaded, or immediately, if it's alread loaded.

// `fn1`, `fn2`, .. are callback functions
req
    .complete(fn1)
    .complete(fn2);
 
req.send(fn3);
 

Testing

To test with PhantomJS, run:

$ make serve &
$ make test

Forebears

License

The MIT License (MIT)

install

npm i decanat-request

Downloadsweekly downloads

1

version

0.3.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability