1 | import { SearchCriteria } from '../types/search-criteria';
|
2 | import { SearchResults } from '../types/search-results';
|
3 | import { fetchFromRegistry } from '../utils/fetch-from-registry';
|
4 | import { normalizeRawSearchCriteria } from '../utils/normalize-raw-search-criteria';
|
5 |
|
6 | /**
|
7 | * `searchPackages` returns the packages corresponding to a given query.
|
8 | *
|
9 | * @param query - one or more search criteria
|
10 | * @param registry - URL of the registry (default: npm registry)
|
11 | * @param mirrors - URLs of the registry mirrors (default: npm registry mirrors)
|
12 | * @param cached - accept cached responses (default: `true`)
|
13 | *
|
14 | * @example
|
15 | * Get the search results for text query `query-registry` from the npm registry:
|
16 | *
|
17 | * ```typescript
|
18 | * import { searchPackages } from 'query-registry';
|
19 | *
|
20 | * (async () => {
|
21 | * const results = await searchPackages({ query: { text: 'query-registry' } });
|
22 | *
|
23 | * // Output: 'query-registry'
|
24 | * console.log(results.objects[0].package.name);
|
25 | * })();
|
26 | * ```
|
27 | *
|
28 | * @see {@link SearchResults}
|
29 | * @see {@link SearchCriteria}
|
30 | * @see {@link npmRegistry}
|
31 | * @see {@link npmRegistryMirrors}
|
32 | */
|
33 | export async function searchPackages({
|
34 | query: rawSearchCriteria,
|
35 | registry,
|
36 | mirrors,
|
37 | cached,
|
38 | }: {
|
39 | query: SearchCriteria;
|
40 | registry?: string;
|
41 | mirrors?: string[];
|
42 | cached?: boolean;
|
43 | }): Promise<SearchResults> {
|
44 | const endpoint = '/-/v1/search';
|
45 | const query = normalizeRawSearchCriteria({ rawSearchCriteria });
|
46 | return fetchFromRegistry({ endpoint, query, registry, mirrors, cached });
|
47 | }
|