@exromany/lido-csm-sdk
Version:
[](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [](h
40 lines • 1.09 kB
JavaScript
export const fetchJson = async (url, params, parse) => {
const response = await fetch(url, {
method: 'GET',
...params,
});
if (!response.ok) {
// TODO: throw error ???
return undefined;
}
if (parse) {
const text = await response.text();
return parse(text);
}
return response.json();
};
export const fetchWithFallback = async (urls, fetch) => {
for (const url of urls) {
if (!url)
continue;
try {
const result = await fetch(url);
if (result)
return result;
}
catch {
/* noop */
}
}
};
export const fetchOneOf = async ({ urls, fetch, parse, validate, }) => {
return fetchWithFallback(urls, async (url) => {
const fetchFunction = fetch ?? (async (url) => fetchJson(url, undefined, parse));
const data = await fetchFunction(url);
if (data && (!validate || validate(data))) {
return data;
}
return null;
});
};
//# sourceMappingURL=fetch-json.js.map