-
-
Save rxw1/7568cc2644148cac6af2 to your computer and use it in GitHub Desktop.
cycle-http-driver.js (node-fetch)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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