packagespy
Version:
A Node.js package to track and monitor npm package download statistics.
39 lines (34 loc) • 1.27 kB
JavaScript
const axios = require('axios');
async function getTotalDownloads(packageName) {
try {
const response = await axios.get(`https://api.npmjs.org/downloads/point/last-month/${packageName}`);
return response.data.downloads;
} catch (error) {
throw new Error(`Failed to fetch download statistics for ${packageName}`);
}
}
async function getPackagesByAuthor(authorName) {
try {
const response = await axios.get(`https://registry.npmjs.org/-/v1/search?author=${authorName}&size=1000`);
return response.data.objects.map(pkg => pkg.package.name);
} catch (error) {
throw new Error(`Failed to fetch packages by author ${authorName}`);
}
}
async function getDownloadStatsForPackages(authorName) {
try {
const packageNames = await getPackagesByAuthor(authorName);
const downloadStats = {};
for (const packageName of packageNames) {
downloadStats[packageName] = await getTotalDownloads(packageName);
}
return downloadStats;
} catch (error) {
throw new Error(`Failed to fetch download statistics for packages by author ${authorName}`);
}
}
module.exports = {
getTotalDownloads,
getPackagesByAuthor,
getDownloadStatsForPackages
};