@itheum/sdk-mx-data-nft
Advanced tools
Comparing version 3.8.0-alpha.2 to 3.8.0-alpha.3
@@ -59,1 +59,3 @@ import BigNumber from 'bignumber.js'; | ||
export declare function checkStatus(response: Response): void; | ||
export declare function getDataFromClientSessionCache(cacheKey: string): any; | ||
export declare function setDataToClientSessionCache(cacheKey: string, jsonData: any, ttlInMs?: number): boolean; |
@@ -24,2 +24,4 @@ "use strict"; | ||
exports.checkStatus = checkStatus; | ||
exports.getDataFromClientSessionCache = getDataFromClientSessionCache; | ||
exports.setDataToClientSessionCache = setDataToClientSessionCache; | ||
const bignumber_js_1 = __importDefault(require("bignumber.js")); | ||
@@ -462,2 +464,37 @@ const datanft_1 = require("../datanft"); | ||
} | ||
/* | ||
Simple Caching Module helps throttle frequently used calls to RPC that fetch data | ||
this helps speed up the client side app and also reduces calls to the RPC | ||
we allow consumer to set a custom TTL in MS for how long data is stored in cache | ||
*/ | ||
const sessionCache = {}; | ||
function getDataFromClientSessionCache(cacheKey) { | ||
const cacheObject = sessionCache[cacheKey]; | ||
if (!cacheObject) { | ||
console.log('getDataFromClientSessionCache: not found'); | ||
return false; | ||
} | ||
else { | ||
// did it expire? is so, delete it from the cache | ||
if (cacheObject.addedOn - Date.now() > cacheObject.expireAfter) { | ||
console.log('getDataFromClientSessionCache: expired'); | ||
delete sessionCache[cacheKey]; // remove it from cache as its expired | ||
return false; | ||
} | ||
else { | ||
console.log('getDataFromClientSessionCache: available'); | ||
return cacheObject.payload; | ||
} | ||
} | ||
} | ||
function setDataToClientSessionCache(cacheKey, jsonData, ttlInMs) { | ||
const howManyMsToCacheFor = ttlInMs || 120000; // 120000 is 2 min default TTL | ||
sessionCache[cacheKey] = { | ||
payload: jsonData, | ||
addedOn: Date.now(), | ||
expireAfter: howManyMsToCacheFor | ||
}; | ||
console.log('setDataToClientSessionCache: cached for ms ', howManyMsToCacheFor); | ||
return true; | ||
} | ||
//# sourceMappingURL=utils.js.map |
@@ -10,13 +10,22 @@ "use strict"; | ||
})(EnvironmentsEnum || (exports.EnvironmentsEnum = EnvironmentsEnum = {})); | ||
// note that in all rpc check methods below we check if window === 'undefined' as this is need for tests to pass | ||
const devnetNetworkConfig = { | ||
chainID: 'D', | ||
networkProvider: 'https://devnet-api.multiversx.com' | ||
networkProvider: typeof window === 'undefined' | ||
? 'https://devnet-api.multiversx.com' | ||
: window.ITH_GLOBAL_MVX_RPC_API_SESSION || | ||
'https://devnet-api.multiversx.com' | ||
}; | ||
const mainnetNetworkConfig = { | ||
chainID: '1', | ||
networkProvider: 'https://api.multiversx.com' | ||
networkProvider: typeof window === 'undefined' | ||
? 'https://api.multiversx.com' | ||
: window.ITH_GLOBAL_MVX_RPC_API_SESSION || 'https://api.multiversx.com' | ||
}; | ||
const testnetNetworkConfig = { | ||
chainID: 'T', | ||
networkProvider: 'https://testnet-api.multiversx.com' | ||
networkProvider: typeof window === 'undefined' | ||
? 'https://testnet-api.multiversx.com' | ||
: window.ITH_GLOBAL_MVX_RPC_API_SESSION || | ||
'https://testnet-api.multiversx.com' | ||
}; | ||
@@ -54,5 +63,13 @@ exports.itheumTokenIdentifier = { | ||
exports.apiConfiguration = { | ||
devnet: 'https://devnet-api.multiversx.com', | ||
mainnet: 'https://api.multiversx.com', | ||
testnet: 'https://testnet-api.multiversx.com' | ||
devnet: typeof window === 'undefined' | ||
? 'https://devnet-api.multiversx.com' | ||
: window.ITH_GLOBAL_MVX_RPC_API_SESSION || | ||
'https://devnet-api.multiversx.com', | ||
mainnet: typeof window === 'undefined' | ||
? 'https://api.multiversx.com' | ||
: window.ITH_GLOBAL_MVX_RPC_API_SESSION || 'https://api.multiversx.com', | ||
testnet: typeof window === 'undefined' | ||
? 'https://testnet-api.multiversx.com' | ||
: window.ITH_GLOBAL_MVX_RPC_API_SESSION || | ||
'https://testnet-api.multiversx.com' | ||
}; | ||
@@ -59,0 +76,0 @@ exports.networkConfiguration = { |
@@ -103,5 +103,20 @@ "use strict"; | ||
} | ||
const response = await fetch(`${this.apiConfiguration}/nfts?identifiers=${identifiers.join(',')}&withSupply=true&size=${identifiers.length}`); | ||
(0, utils_1.checkStatus)(response); | ||
const data = await response.json(); | ||
// lets not make the call if not needed | ||
if (identifiers.length === 0) { | ||
return []; | ||
} | ||
const fetchUrl = `${this.apiConfiguration}/nfts?identifiers=${identifiers.join(',')}&withSupply=true&size=${identifiers.length}`; | ||
// check if its in session cache | ||
let jsonDataPayload = null; | ||
const getFromSessionCache = (0, utils_1.getDataFromClientSessionCache)(fetchUrl); | ||
if (!getFromSessionCache) { | ||
const response = await fetch(fetchUrl); | ||
(0, utils_1.checkStatus)(response); | ||
jsonDataPayload = await response.json(); | ||
(0, utils_1.setDataToClientSessionCache)(fetchUrl, jsonDataPayload, 5 * 60 * 1000); | ||
} | ||
else { | ||
jsonDataPayload = getFromSessionCache; | ||
} | ||
const data = jsonDataPayload; | ||
try { | ||
@@ -108,0 +123,0 @@ const dataNfts = data.map((value) => (0, utils_1.parseDataNft)(value)); |
{ | ||
"name": "@itheum/sdk-mx-data-nft", | ||
"version": "3.8.0-alpha.2", | ||
"version": "3.8.0-alpha.3", | ||
"description": "SDK for Itheum's Data NFT Technology on MultiversX Blockchain", | ||
@@ -5,0 +5,0 @@ "main": "out/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
618121
11884