Skip to content

Commit

Permalink
feat: add getDailyPackageDownloads
Browse files Browse the repository at this point in the history
  • Loading branch information
velut committed Apr 13, 2024
1 parent 2aaf870 commit 0e795c8
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/get-daily-package-downloads.ts
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}`));
};

0 comments on commit 0e795c8

Please sign in to comment.