UNPKG

@knine-sdk/fetch

Version:
22 lines (19 loc) 918 B
import { __rest } from './node_modules/tslib/tslib.es6.js'; import invariant from 'tiny-invariant'; import { fetchWithFallbacks } from './fetchWithFallbacks.js'; import { getRPCUrls } from './providersUrls.js'; const fetchRPC = (chainId, options) => { const { providers = {}, urls = [] } = options, init = __rest(options, ["providers", "urls"]); const customUrls = urls.map((value) => { let url = value; if (typeof value === 'function') url = value(chainId); invariant(typeof url === 'string', 'URL should be a string'); return url; }); const providersUrls = getRPCUrls(chainId, providers); const combinedUrls = [...customUrls, ...providersUrls]; invariant(combinedUrls.length > 0, 'There are no API keys or URLs provided'); return fetchWithFallbacks(combinedUrls, Object.assign({ method: 'POST' }, init)); }; export { fetchRPC };