@1hive/connect-core
Version:
Access and interact with Aragon Organizations and their apps.
39 lines • 1.44 kB
JavaScript
import { ErrorConnection, ErrorUnexpectedResult } from '../errors';
import { createCacheStore } from './cache-store';
export function ipfsResolver(urlTemplate, cache = 0) {
const cacheStore = cache === 0 ? null : 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 ErrorConnection(`Couldn’t fetch ${url}.`);
}
try {
data = await response.json();
}
catch (_) {
throw new 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);
},
};
}
//# sourceMappingURL=ipfs.js.map