Skip to content

Instantly share code, notes, and snippets.

@rxw1
Forked from bouzuya/cycle-http-driver.js
Created February 9, 2016 23:51
Show Gist options
  • Save rxw1/7568cc2644148cac6af2 to your computer and use it in GitHub Desktop.
Save rxw1/7568cc2644148cac6af2 to your computer and use it in GitHub Desktop.
cycle-http-driver.js (node-fetch)
import fetch from 'node-fetch';
import Rx from 'rx';
function makeHTTPDriver() {
var requests = {};
return function(request$) {
return request$
.filter(request => {
if (typeof request === 'undefined' || request === null) return false;
const { id } = request;
if (typeof id === 'undefined' || id === null) return false;
if (requests.hasOwnProperty(id)) return false;
requests[id] = request;
return true;
})
.map(request => {
const { url, method, body, headers } = request;
const promise = fetch(url, { method, body, headers });
const response$ = Rx.Observable.fromPromise(promise);
return response$.map(response => ({ request, response }));
})
.mergeAll()
.share();
};
}
export default { makeHTTPDriver };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment