Files
jackwener__opencli/clis/npm/downloads.js
jakevin 55088bbb28 feat: 13 read adapters across 5 new sites + 4 extensions (round 3) (#1346)
New sites (8 commands):
- npm    : search / package / downloads (registry.npmjs.org + api.npmjs.org)
- pypi   : package / downloads (pypi.org + pypistats.org)
- crates : search / crate (crates.io)
- mdn    : search (developer.mozilla.org)
- nvd    : cve (services.nvd.nist.gov)

Extensions (5 commands; +1 dblp/author surfaced in index):
- hf            : spaces (Hugging Face Spaces by likes / created_at / last_modified)
- dblp          : venue (search dblp's venue registry by acronym/topic)
- coingecko     : derivatives (perpetual / futures markets, 24h volume)
- stackoverflow : related (related questions for a given question id)

All commands hit public unauthenticated endpoints (Strategy.PUBLIC, browser:false),
typed-fail-fast on bad inputs (no silent fallback / clamp), and round-trip listing
ids into their detail commands where applicable.

Audits (all green vs baseline):
- typed-error-lint        : 196 = 196 baseline, no new
- silent-column-drop      : 103 = 103 baseline, no new
- listing-id-pairing      : 13 advisory (was 12; +1 = dblp/venue with no
                            corresponding venue-detail command)

Doc coverage : 120/120 adapter dirs documented (+5 new doc pages, +4 updated)
Manifest     : 722 entries (was 709; +13 commands)

Live verified:
- npm search react / npm package react / npm downloads react --period last-week
- npm downloads react --period 2025-01-01:2025-01-05
- pypi package requests / pypi downloads requests --period recent / overall
- crates search tokio / crates crate serde
- mdn search fetch
- nvd cve CVE-2021-44228
- hf spaces --limit 3
- dblp venue ICLR
- coingecko derivatives --limit 3
- stackoverflow related 79935770 --limit 3
- typed-error sanity: invalid CVE id, bad npm name, bad --period
2026-05-06 13:14:41 +08:00

60 lines
2.5 KiB
JavaScript

// npm downloads — fetch download counts for a single npm package over a window.
//
// Hits `api.npmjs.org/downloads/range/<period>/<pkg>`. Default window is the
// last 7 days (one row per day). Use `--period last-month` for 30 days, or
// pass a custom `YYYY-MM-DD:YYYY-MM-DD` range.
import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, EmptyResultError } from '@jackwener/opencli/errors';
import { NPM_API, npmFetch, requirePackageName } from './utils.js';
const FIXED_PERIODS = new Set(['last-day', 'last-week', 'last-month', 'last-year']);
const RANGE_PATTERN = /^(\d{4}-\d{2}-\d{2}):(\d{4}-\d{2}-\d{2})$/;
function requirePeriod(value) {
const s = String(value ?? 'last-week').trim();
if (FIXED_PERIODS.has(s)) return s;
const m = RANGE_PATTERN.exec(s);
if (m) {
const [, start, end] = m;
if (new Date(start) > new Date(end)) {
throw new ArgumentError(`npm downloads period start ${start} is after end ${end}`);
}
return `${start}:${end}`;
}
throw new ArgumentError(
`npm downloads period "${value}" is invalid`,
'Use last-day / last-week (default) / last-month / last-year, or YYYY-MM-DD:YYYY-MM-DD.',
);
}
cli({
site: 'npm',
name: 'downloads',
access: 'read',
description: 'Daily download counts for an npm package over a window',
domain: 'api.npmjs.org',
strategy: Strategy.PUBLIC,
browser: false,
args: [
{ name: 'name', positional: true, required: true, help: 'npm package name (e.g. "react", "@vercel/og")' },
{ name: 'period', default: 'last-week', help: 'last-day / last-week / last-month / last-year, or YYYY-MM-DD:YYYY-MM-DD' },
],
columns: ['rank', 'package', 'day', 'downloads'],
func: async (args) => {
const name = requirePackageName(args.name);
const period = requirePeriod(args.period);
const url = `${NPM_API}/downloads/range/${period}/${name}`;
const body = await npmFetch(url, `npm downloads ${name}`);
const days = Array.isArray(body?.downloads) ? body.downloads : [];
if (!days.length) {
throw new EmptyResultError('npm downloads', `npm has no download stats for "${name}" in window ${period}.`);
}
return days.map((row, i) => ({
rank: i + 1,
package: String(body.package ?? name),
day: String(row.day ?? ''),
downloads: row.downloads != null ? Number(row.downloads) : null,
}));
},
});