Skip to content

Instantly share code, notes, and snippets.

@mjackson
Last active November 11, 2022 05:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjackson/6bdc34dd600f4085f32e0cb8e0643dd1 to your computer and use it in GitHub Desktop.
Save mjackson/6bdc34dd600f4085f32e0cb8e0643dd1 to your computer and use it in GitHub Desktop.
Download counts for react-router
const npmApiUrl = "https://api.npmjs.org/downloads/point";
async function fetchDownloadCounts(packageName, year) {
let range = `${year}-01-01:${year}-12-31`;
let response = await fetch(`${npmApiUrl}/${range}/${packageName}`);
return (await response.json()).downloads;
}
async function getDownloads(startYear, endYear = startYear) {
if (endYear < startYear) {
throw new Error(
`End year (${endYear}) must be before start year (${startYear})`
);
}
let years = [];
for (let year = startYear; year <= endYear; year += 1) {
years.push(year);
}
let downloadCounts = await Promise.all(
years.map(year => fetchDownloadCounts("react-router", year))
);
let totalDownloads = downloadCounts.reduce((memo, n) => memo + n);
return totalDownloads.toLocaleString();
}
getDownloads(2015, 2022).then(downloads => console.log(downloads));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment