Created
April 14, 2017 05:34
-
-
Save matfournier/0ccd9fba2d8c8cc2d286415ce2178350 to your computer and use it in GitHub Desktop.
fetch.js using flutures
This file contains hidden or 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
const fetch = require('node-fetch') | |
const Future = require('fluture') | |
const fs = require('fs') | |
const BASEURL = 'https://api.github.com/licenses' | |
const headers = { | |
headers: { | |
'Accept': 'application/vnd.github.drax-preview+json' | |
} | |
} | |
// fetchf :: Url -> Future Error Response | |
const fetchf = Future.fromPromise2(fetch) | |
// writeLicense :: (File Path String, Contents String) -> Future Error Success and Unit | |
const writeLicense = (file, contents) => | |
Future.node(done => fs.writeFile(file, contents, done)) | |
// getLicenseList :: (Url, Options) -> Future Error Json([licensepaths]) | |
const getLicenseList = fetchf(BASEURL, headers) | |
.chain(res => Future.fromPromise(_ => res.json(), 0)) | |
const getLicense = json => | |
fetchf(BASEURL + '/' + json.key, headers) | |
.chain(res => Future.fromPromise(_ => res.json(), 0)) | |
.chain(json => json['key'] ? Future.of(json) : Future.reject('Invalid JSON Response: Invalid Key')) | |
.chain(json => writeLicense(__dirname + '/licenses/' + json.key, json.body)) | |
getLicenseList | |
.chain(x => Future.of(x.map(getLicense))) | |
.chain(Future.parallel(20)) | |
.fork(console.error, _ => console.log('Fetched licenses')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment