@exromany/lido-csm-sdk
Version:
[](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [](h
44 lines • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchOneOf = exports.fetchWithFallback = exports.fetchJson = void 0;
const fetchJson = async (url, params, parse) => {
const response = await fetch(url, {
method: 'GET',
...params,
});
if (!response.ok) {
return undefined;
}
if (parse) {
const text = await response.text();
return parse(text);
}
return response.json();
};
exports.fetchJson = fetchJson;
const fetchWithFallback = async (urls, fetch) => {
for (const url of urls) {
if (!url)
continue;
try {
const result = await fetch(url);
if (result)
return result;
}
catch {
}
}
};
exports.fetchWithFallback = fetchWithFallback;
const fetchOneOf = async ({ urls, fetch, parse, validate, }) => {
return (0, exports.fetchWithFallback)(urls, async (url) => {
const fetchFunction = fetch ?? (async (url) => (0, exports.fetchJson)(url, undefined, parse));
const data = await fetchFunction(url);
if (data && (!validate || validate(data))) {
return data;
}
return null;
});
};
exports.fetchOneOf = fetchOneOf;
//# sourceMappingURL=fetch-json.js.map