diff --git a/src/get-daily-package-downloads.ts b/src/get-daily-package-downloads.ts new file mode 100644 index 0000000..45dffcf --- /dev/null +++ b/src/get-daily-package-downloads.ts @@ -0,0 +1,38 @@ +import urlJoin from "url-join"; +import { z } from "zod"; +import { assertValidPackageName } from "./assert-valid-package-name"; +import { DownloadPeriod } from "./download-period"; +import { fetchData } from "./fetch-data"; +import { DailyRegistryDownloads } from "./get-daily-registry-downloads"; +import { npmRegistryDownloadsApiUrl } from "./npm-registry"; + +export const DailyPackageDownloads = DailyRegistryDownloads.extend({ + /** Package name. */ + package: z.string(), +}); + +/** +`DailyPackageDownloads` describes the total number of downloads for each day +for a package in a given time period. +@see {@link https://github.com/npm/registry/blob/master/docs/download-counts.md#ranges} +*/ +export type DailyPackageDownloads = z.infer; + +/** +`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 +@param registry - URL of the registry downloads API (default: npm registry downloads API) + +@see {@link DailyPackageDownloads} +*/ +export const getDailyPackageDownloads = async ( + name: string, + period: DownloadPeriod, + registry = npmRegistryDownloadsApiUrl, +): Promise => { + assertValidPackageName(name); + return fetchData(DailyPackageDownloads, urlJoin(registry, `/downloads/range/${period}/${name}`)); +};