Skip to content

Instantly share code, notes, and snippets.

@nadeesha
Last active June 19, 2019 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadeesha/1ec3d8dcc61d17ebb03d6c37c569a64f to your computer and use it in GitHub Desktop.
Save nadeesha/1ec3d8dcc61d17ebb03d6c37c569a64f to your computer and use it in GitHub Desktop.
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