UNPKG

944 BJavaScriptView Raw
1import { npmRegistry, npmRegistryMirrors } from '../data/registries.esm.js';
2import { fetch } from './fetch.esm.js';
3import { log } from './log.esm.js';
4import urlJoin from 'url-join';
5
6async function fetchFromRegistry({
7 endpoint,
8 headers,
9 query,
10 registry = npmRegistry,
11 mirrors = npmRegistryMirrors,
12 cached
13}) {
14 const urls = [registry, ...mirrors].map(base => urlJoin(base, endpoint, query ? `?${query}` : ''));
15 let lastError;
16
17 for (const url of urls) {
18 try {
19 const json = await fetch({
20 url,
21 headers,
22 cached
23 });
24 return json;
25 } catch (err) {
26 // Keep last fetch error
27 lastError = err;
28 }
29 }
30
31 log('fetchFromRegistry: cannot retrieve data from registry or mirrors: %O', {
32 endpoint,
33 headers,
34 query,
35 registry,
36 mirrors,
37 lastError
38 });
39 throw lastError;
40}
41
42export { fetchFromRegistry };
43//# sourceMappingURL=fetch-from-registry.esm.js.map