Download counts for react-router
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
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