-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<typeof DailyPackageDownloads>; | ||
|
||
/** | ||
`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<DailyPackageDownloads> => { | ||
assertValidPackageName(name); | ||
return fetchData(DailyPackageDownloads, urlJoin(registry, `/downloads/range/${period}/${name}`)); | ||
}; |