query-registry
Version:
Query the npm registry for packuments, manifests, packages and download counts
26 lines (23 loc) • 1.11 kB
JavaScript
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 { DailyRegistryDownloads } from "./get-daily-registry-downloads.js";
import { npmRegistryDownloadsApiUrl } from "./npm-registry.js";
export const DailyPackageDownloads = z.object({
...DailyRegistryDownloads.shape,
/** Package name. */
package: z.string(),
});
/**
`getDailyPackageDownloads` returns the total number of downloads for each day
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 DailyPackageDownloads}
*/
export async function getDailyPackageDownloads(name, period, registry = npmRegistryDownloadsApiUrl) {
assertValidPackageName(name);
return await fetchData(DailyPackageDownloads, urlJoin(registry, `/downloads/range/${period}/${name}`));
}