const fetch = require('node-fetch')
const run = require('run-gen')
const downloadsUrl = `https://api.npmjs.org/downloads/point`
const registryUrl = `https://registry.npmjs.org`
function* downloads(pkg, period) {
const res = yield fetch(`${downloadsUrl}/${period}/${pkg}`)
const stats = JSON.parse(yield res.text())
return (stats && stats.downloads) || 'unknown'
}
function* info(pkg) {
const res = yield fetch(`${registryUrl}/${pkg}`)
const info = JSON.parse(yield res.text())
return {
name: info.name,
description: info.description,
}
}
function* stats(pkg, period = 'last-month') {
return Object.assign(
{},
yield* info(pkg),
{downloads: yield* downloads(pkg, period)}
)
}
run(stats('npm')).then(r => console.log(r), e => console.log(e))