UNPKG

query-registry

Version:

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

25 lines (22 loc) 1.06 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 { RegistryDownloads } from "./get-registry-downloads.js"; import { npmRegistryDownloadsApiUrl } from "./npm-registry.js"; export const PackageDownloads = z.object({ ...RegistryDownloads.shape, /** Package name. */ package: z.string(), }); /** `getPackageDownloads` returns the total number of downloads for a package in the given time period. @param name - package name @param period - {@link DownloadPeriod | time period} in which downloads happened; the npm registry limits data to the last 18 months @param registry - URL of the registry downloads API (default: npm registry downloads API) @see {@link PackageDownloads} */ export async function getPackageDownloads(name, period, registry = npmRegistryDownloadsApiUrl) { assertValidPackageName(name); return await fetchData(PackageDownloads, urlJoin(registry, `/downloads/point/${period}/${name}`)); }