mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
55088bbb28
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
73 lines
2.5 KiB
JavaScript
73 lines
2.5 KiB
JavaScript
// Shared helpers for the crates.io adapters.
|
|
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
|
|
export const CRATES_BASE = 'https://crates.io';
|
|
const UA = 'opencli-crates-adapter (+https://github.com/jackwener/opencli)';
|
|
|
|
// crates.io crate names: 1-64 chars, ascii letters/digits/-_, must start with a letter.
|
|
const CRATE_NAME = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
|
|
export function requireString(value, label) {
|
|
const s = String(value ?? '').trim();
|
|
if (!s) throw new ArgumentError(`crates ${label} cannot be empty`);
|
|
return s;
|
|
}
|
|
|
|
export function requireCrateName(value) {
|
|
const s = String(value ?? '').trim();
|
|
if (!s) throw new ArgumentError('crates crate name is required (e.g. "serde", "tokio")');
|
|
if (!CRATE_NAME.test(s)) {
|
|
throw new ArgumentError(
|
|
`crates crate name "${value}" is not a valid crates.io name`,
|
|
'Names start with an ASCII letter, then 0-63 chars of letters / digits / "_-".',
|
|
);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
|
|
const raw = value ?? defaultValue;
|
|
const n = typeof raw === 'number' ? raw : Number(raw);
|
|
if (!Number.isInteger(n) || n <= 0) {
|
|
throw new ArgumentError(`crates ${label} must be a positive integer`);
|
|
}
|
|
if (n > maxValue) {
|
|
throw new ArgumentError(`crates ${label} must be <= ${maxValue}`);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
export async function cratesFetch(url, label) {
|
|
let resp;
|
|
try {
|
|
// crates.io requires a descriptive User-Agent per https://crates.io/data-access
|
|
resp = await fetch(url, { headers: { 'user-agent': UA, accept: 'application/json' } });
|
|
}
|
|
catch (err) {
|
|
throw new CommandExecutionError(
|
|
`${label} request failed: ${err?.message ?? err}`,
|
|
'Check that crates.io is reachable from this network.',
|
|
);
|
|
}
|
|
if (resp.status === 404) {
|
|
throw new EmptyResultError(label, `crates.io returned 404 for ${url}.`);
|
|
}
|
|
if (resp.status === 429) {
|
|
throw new CommandExecutionError(
|
|
`${label} returned HTTP 429 (rate limited)`,
|
|
'crates.io rate-limits unauthenticated traffic; wait a few seconds and retry.',
|
|
);
|
|
}
|
|
if (!resp.ok) {
|
|
throw new CommandExecutionError(`${label} returned HTTP ${resp.status}`);
|
|
}
|
|
let body;
|
|
try {
|
|
body = await resp.json();
|
|
}
|
|
catch (err) {
|
|
throw new CommandExecutionError(`${label} returned malformed JSON: ${err?.message ?? err}`);
|
|
}
|
|
return body;
|
|
}
|