UNPKG

@exromany/lido-csm-sdk

Version:

[![GitHub license](https://img.shields.io/github/license/lidofinance/lido-csm-sdk?color=limegreen)](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [![Version npm](https://img.shields.io/npm/v/@lidofinance/lido-csm-sdk?label=version)](h

44 lines 1.31 kB
"use strict"; 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