1.0.1 • Published 8 years ago

node-rx-http v1.0.1

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

node-rx-http

NPM

Build Status codecov Greenkeeper badge

A simple RxJS wrapper for the node http module

Quick Start

$ npm install --save node-rx-http

Usage

var RxHttp = require('node-rx-http');

RxHttp.get('http://personatestuser.org/email')
  .filter(x => x.statusCode === 200)
  .map(x => JSON.parse(x.body))
  .map(x => ({email : x.email, password: x.pass}));

Compare this to the normal way of interacting with the native http module

//Taken from https://davidwalsh.name/nodejs-http-request
return http.get({
    host: 'personatestuser.org',
    path: '/email'
}, function(response) {
    // Continuously update stream with data
    var body = '';
    response.on('data', function(d) {
        body += d;
    });
    response.on('end', function() {

        // Data reception is done, do whatever with it!
        var parsed = JSON.parse(body);
        callback({
            email: parsed.email,
             password: parsed.pass
        });
    });
});

This also lets us interface nicely with other request libraries. Say we are changing tools and we need to strangle out one library for another.

With RxJS this is a breeze, because the source is decoupled from the stream.

var rp = require('request-promise');
var Rx = require('rxjs');
var RxHttp = require('node-rx-http');

var source = RxHttp.get('http://personatestuser.org/email');
//Or
var source = Rx.Observable.fromPromise(rp('http://personatestuser.org/email'));

//Processing - either source will work
source
  .filter(x => x.statusCode === 200)
  .map(x => JSON.parse(x.body))
  .map(x => ({email : x.email, password: x.pass}))
  .subscribe(x => doLogin(x.email, x.password));