1 | import { DownloadPeriod } from '../types/download-period';
|
2 | import { DailyRegistryDownloads } from '../types/downloads';
|
3 | import { fetchDownloadsFromRegistry } from '../utils/fetch-downloads-from-registry';
|
4 | import { normalizeRawDownloadPeriod } from '../utils/normalize-download-period';
|
5 |
|
6 | /**
|
7 | * `getDailyRegistryDownloads` returns the number of downloads for all registry packages
|
8 | * for each day in a given time period.
|
9 | *
|
10 | * @param period - time period in which downloads happened (default: `last-week`)
|
11 | * @param registryDownloadsAPI - URL of the registry's downloads API (default: npm registry)
|
12 | * @param cached - accept cached responses (default: `true`)
|
13 | *
|
14 | * @example
|
15 | * Get the day by day weekly downloads for the npm registry:
|
16 | *
|
17 | * ```typescript
|
18 | * import { getDailyRegistryDownloads } from 'query-registry';
|
19 | *
|
20 | * (async () => {
|
21 | * const downloads = await getDailyRegistryDownloads();
|
22 | *
|
23 | * // Output: 'number'
|
24 | * console.log(typeof downloads.downloads[0].downloads);
|
25 | * })();
|
26 | * ```
|
27 | *
|
28 | * @example
|
29 | * Get the day by day monthly downloads for the npm registry:
|
30 | *
|
31 | * ```typescript
|
32 | * import { getDailyRegistryDownloads } from 'query-registry';
|
33 | *
|
34 | * (async () => {
|
35 | * const downloads = await getDailyRegistryDownloads({ period: 'last-month' });
|
36 | *
|
37 | * // Output: 'number'
|
38 | * console.log(typeof downloads.downloads[0].downloads);
|
39 | * })();
|
40 | * ```
|
41 | *
|
42 | * @see {@link DailyRegistryDownloads}
|
43 | * @see {@link DownloadPeriod}
|
44 | * @see {@link npmRegistryDownloadsAPI}
|
45 | * @see {@link https://github.com/npm/registry/blob/master/docs/download-counts.md#ranges}
|
46 | */
|
47 | export async function getDailyRegistryDownloads({
|
48 | period: rawDownloadPeriod,
|
49 | registryDownloadsAPI,
|
50 | cached,
|
51 | }: {
|
52 | period?: DownloadPeriod;
|
53 | registryDownloadsAPI?: string;
|
54 | cached?: boolean;
|
55 | } = {}): Promise<DailyRegistryDownloads> {
|
56 | const period = normalizeRawDownloadPeriod({ rawDownloadPeriod });
|
57 | const endpoint = `/downloads/range/${period}`;
|
58 | return fetchDownloadsFromRegistry({
|
59 | endpoint,
|
60 | registryDownloadsAPI,
|
61 | cached,
|
62 | });
|
63 | }
|