UNPKG

@effectai/sdk

Version:

Effect Network Javscript/Typescript SDK (for [https://effect.network](https://effect.network))

58 lines 2.19 kB
export var IpfsContentFormat; (function (IpfsContentFormat) { IpfsContentFormat["FormData"] = "formdata"; IpfsContentFormat["ArrayBuffer"] = "arraybuffer"; IpfsContentFormat["Blob"] = "blob"; IpfsContentFormat["Text"] = "text"; IpfsContentFormat["JSON"] = "json"; })(IpfsContentFormat || (IpfsContentFormat = {})); export const getIpfsResource = async ({ client, hash, ipfsContentForm = IpfsContentFormat.JSON, }) => { try { const { ipfs } = client.network.config; const { ipfsCacheDurationInMs } = client.options; const cache = client.cache; const cacheKey = `${hash}-${ipfsContentForm}`; if (ipfsCacheDurationInMs) { // Create a cache key const cacheKey = `${hash}-${ipfsContentForm}`; const cachedItem = (await cache.get(cacheKey)); // If we have the response cached, return it if (cachedItem && Date.now() < cachedItem.timestamp + ipfsCacheDurationInMs) { return cachedItem.data; } } const data = await client.fetchProvider.fetch(`${ipfs.ipfsEndpoint}/ipfs/${hash}`); let result; // Return the IPFS content in the format you want switch (ipfsContentForm) { case IpfsContentFormat.FormData: result = data.formData(); break; case IpfsContentFormat.ArrayBuffer: result = data.arrayBuffer(); break; case IpfsContentFormat.Blob: result = data.blob(); break; case IpfsContentFormat.Text: result = data.text(); break; case IpfsContentFormat.JSON: result = await data.json(); break; default: result = data; break; } // After we got the result, cache it if (ipfsCacheDurationInMs) { cache.set(cacheKey, { data: result, timestamp: Date.now() }); } return result; } catch (error) { console.error(error); } }; //# sourceMappingURL=getIpfsResource.js.map