UNPKG

1.67 kBPlain TextView Raw
1import { RawAbbreviatedPackument } from '../types/raw-abbreviated-packument';
2import { assertValidPackageName } from '../utils/assert-valid-package-name';
3import { fetchFromRegistry } from '../utils/fetch-from-registry';
4
5/**
6 * `getRawAbbreviatedPackument` returns the abbreviated packument (package document)
7 * containing only the metadata necessary to install a package present on the registry.
8 *
9 * Note: the abbreviated packument is returned as retrieved from the registry.
10 *
11 * @param name - package name
12 * @param registry - URL of the registry (default: npm registry)
13 * @param mirrors - URLs of the registry mirrors (default: npm registry mirrors)
14 * @param cached - accept cached responses (default: `true`)
15 *
16 * @example
17 * Get the abbreviated packument for package `query-registry` from the npm registry:
18 *
19 * ```typescript
20 * import { getRawAbbreviatedPackument } from 'query-registry';
21 *
22 * (async () => {
23 * const packument = await getRawAbbreviatedPackument({ name: 'query-registry' });
24 *
25 * // Output: 'query-registry'
26 * console.log(packument.name);
27 * })();
28 * ```
29 *
30 * @see {@link RawAbbreviatedPackument}
31 * @see {@link npmRegistry}
32 * @see {@link npmRegistryMirrors}
33 */
34export async function getRawAbbreviatedPackument({
35 name,
36 registry,
37 mirrors,
38 cached,
39}: {
40 name: string;
41 registry?: string;
42 mirrors?: string[];
43 cached?: boolean;
44}): Promise<RawAbbreviatedPackument> {
45 assertValidPackageName({ name });
46
47 const endpoint = `/${name}`;
48 const headers = { Accept: 'application/vnd.npm.install-v1+json' };
49 return fetchFromRegistry({ endpoint, headers, registry, mirrors, cached });
50}