Last active
June 19, 2019 09:25
-
-
Save nadeesha/1ec3d8dcc61d17ebb03d6c37c569a64f to your computer and use it in GitHub Desktop.
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
import { encaseP, node, encase, map, chain } from "fluture"; | |
const readFileF = filePath => node(done => fs.readFile(filePath, "utf8", done)); | |
const fetchF = url => encaseP(url => fetch(url)); | |
const jsonParseF = encase(jsonString => JSON.parse(jsonString)); | |
const getPackageDownloads = (npmPackageName, onSuccess, onFailure) => { | |
readFileF("package.json") | |
.pipe(chain(jsonParseF)) // shorthand for chain(jsonString => jsonParseF(jsonString)) | |
.pipe(map(package => package.name)) | |
.pipe( | |
chain(packageName => fetchF(`/api/v1/package-metadata/${packageName}`)) | |
) | |
.pipe(map(jsonParseF)) | |
.pipe(map(response => response.data.metadata.downloadCount)) | |
.pipe( | |
fork(error => onFailure(error), downloadCount => onSuccess(downloadCount)) | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment