UNPKG

@1hive/connect-core

Version:

Access and interact with Aragon Organizations and their apps.

43 lines 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ipfsResolver = void 0; const errors_1 = require("../errors"); const cache_store_1 = require("./cache-store"); function ipfsResolver(urlTemplate, cache = 0) { const cacheStore = cache === 0 ? null : cache_store_1.createCacheStore(cache); return { async json(cid, path) { const url = await this.url(cid, path); const fetchJson = async () => { let response; let data; try { response = await fetch(url); } catch (_) { throw new errors_1.ErrorConnection(`Couldn’t fetch ${url}.`); } try { data = await response.json(); } catch (_) { throw new errors_1.ErrorUnexpectedResult(`Couldn’t parse the result of ${url} as JSON.`); } return data; }; return (cacheStore === null || cacheStore === void 0 ? void 0 : cacheStore.get(url, fetchJson)) || fetchJson(); }, async url(cid, path) { const url = urlTemplate.replace(/\{cid\}/, cid); if (!path) { return url.replace(/\{path\}/, ''); } if (!path.startsWith('/')) { path = `/${path}`; } return url.replace(/\{path\}/, path); }, }; } exports.ipfsResolver = ipfsResolver; //# sourceMappingURL=ipfs.js.map