UNPKG

query-registry

Version:

Query the npm registry for packuments, manifests, packages and download counts

24 lines (21 loc) 1.22 kB
import urlJoin from "url-join"; import * as z from "zod"; import { assertValidPackageName } from "./assert-valid-package-name.js"; import { fetchData } from "./fetch-data.js"; import { PackageDownloads } from "./get-package-downloads.js"; import { npmRegistryDownloadsApiUrl } from "./npm-registry.js"; export const BulkPackageDownloads = z.record(z.string(), z.union([z.null(), PackageDownloads])); /** `getBulkPackageDownloads` returns the total number of downloads for the given packages in the given time period. @param names - list of package names; the npm registry does not support scoped packages and handles a maximum of 128 packages at a time @param period - {@link DownloadPeriod | time period} in which downloads happened; the npm registry limits bulk data to the last 365 days @param registry - URL of the registry downloads API (default: npm registry downloads API) @see {@link BulkPackageDownloads} */ export async function getBulkPackageDownloads(names, period, registry = npmRegistryDownloadsApiUrl) { for (const name of names) { assertValidPackageName(name); } return await fetchData(BulkPackageDownloads, urlJoin(registry, `/downloads/point/${period}/${names.join(",")}`)); }