query-registry
Version:
Query the npm registry for packuments, manifests, packages and download counts
64 lines (60 loc) • 1.85 kB
JavaScript
import { fetchDownloadsFromRegistry } from '../utils/fetch-downloads-from-registry.esm.js';
import { normalizeRawDownloadPeriod } from '../utils/normalize-download-period.esm.js';
/**
* `getRegistryDownloads` returns the number of downloads for all registry packages
* in a given time period.
*
* @param period - time period in which downloads happened (default: `last-week`)
* @param registryDownloadsAPI - URL of the registry's downloads API (default: npm registry)
* @param cached - accept cached responses (default: `true`)
*
* @example
* Get the weekly downloads for the npm registry:
*
* ```typescript
* import { getRegistryDownloads } from 'query-registry';
*
* (async () => {
* const downloads = await getRegistryDownloads();
*
* // Output: 'number'
* console.log(typeof downloads.downloads);
* })();
* ```
*
* @example
* Get the monthly downloads for the npm registry:
*
* ```typescript
* import { getRegistryDownloads } from 'query-registry';
*
* (async () => {
* const downloads = await getRegistryDownloads({ period: 'last-month' });
*
* // Output: 'number'
* console.log(typeof downloads.downloads);
* })();
* ```
*
* @see {@link RegistryDownloads}
* @see {@link DownloadPeriod}
* @see {@link npmRegistryDownloadsAPI}
* @see {@link https://github.com/npm/registry/blob/master/docs/download-counts.md#point-values}
*/
async function getRegistryDownloads({
period: rawDownloadPeriod,
registryDownloadsAPI,
cached
} = {}) {
const period = normalizeRawDownloadPeriod({
rawDownloadPeriod
});
const endpoint = `/downloads/point/${period}`;
return fetchDownloadsFromRegistry({
endpoint,
registryDownloadsAPI,
cached
});
}
export { getRegistryDownloads };
//# sourceMappingURL=get-registry-downloads.esm.js.map