diff --git a/package.json b/package.json index 5f1359b..5d647e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@itheum/sdk-mx-data-nft", - "version": "3.8.0-alpha.11", + "version": "3.8.0-alpha.12", "description": "SDK for Itheum's Data NFT Technology on MultiversX Blockchain", "main": "out/index.js", "types": "out/index.d.js", diff --git a/src/common/utils.ts b/src/common/utils.ts index 4615108..4a52e23 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -611,16 +611,16 @@ export function getDataFromClientSessionCache(cacheKey: string) { const cacheObject = sessionCache[cacheKey]; if (!cacheObject) { - console.log('getDataFromClientSessionCache: not found'); + console.log(`getDataFromClientSessionCache: ${cacheKey} not found`); return false; } else { // did it expire? is so, delete it from the cache if (Date.now() - cacheObject.addedOn > cacheObject.expireAfter) { - console.log('getDataFromClientSessionCache: expired'); + console.log(`getDataFromClientSessionCache: ${cacheKey} expired`); delete sessionCache[cacheKey]; // remove it from cache as its expired return false; } else { - console.log('getDataFromClientSessionCache: available'); + console.log(`getDataFromClientSessionCache: ${cacheKey} available`); return cacheObject.payload; } } diff --git a/src/datanft.ts b/src/datanft.ts index 7ecfb08..40bbd04 100644 --- a/src/datanft.ts +++ b/src/datanft.ts @@ -178,10 +178,12 @@ export class DataNft implements DataNftType { * Each object should have a `nonce` property representing the token nonce. * An optional `tokenIdentifier` property can be provided to specify the token identifier. * If not provided, the default token identifier based on the {@link EnvironmentsEnum} + * @param clientCacheForMS optional. You can cache the response for this amount of MS if you want. Don't pass it in if you don't want caching (needs to be "undefined") * @returns An array of {@link DataNft} objects */ static async createManyFromApi( - tokens: { nonce: number; tokenIdentifier?: string }[] + tokens: { nonce: number; tokenIdentifier?: string }[], + clientCacheForMS?: number ): Promise { this.ensureNetworkConfigSet(); const identifiers = tokens.map(({ nonce, tokenIdentifier }) => @@ -190,6 +192,7 @@ export class DataNft implements DataNftType { nonce ) ); + if (identifiers.length > MAX_ITEMS) { throw new ErrTooManyItems(); } @@ -208,14 +211,24 @@ export class DataNft implements DataNftType { }`; // check if its in session cache + let useCache = typeof clientCacheForMS !== 'undefined'; let jsonDataPayload = null; - const getFromSessionCache = getDataFromClientSessionCache(fetchUrl); + const getFromSessionCache = useCache + ? getDataFromClientSessionCache(fetchUrl) + : false; if (!getFromSessionCache) { const response = await fetch(fetchUrl); checkStatus(response); jsonDataPayload = await response.json(); - setDataToClientSessionCache(fetchUrl, jsonDataPayload, 5 * 60 * 1000); + + if (useCache) { + setDataToClientSessionCache( + fetchUrl, + jsonDataPayload, + clientCacheForMS + ); + } } else { jsonDataPayload = getFromSessionCache; } @@ -292,10 +305,12 @@ export class DataNft implements DataNftType { * Returns an array of `DataNft` objects owned by the address * @param address the address to query * @param collections the collection identifiers to query. If not provided, the default collection identifier based on the {@link EnvironmentsEnum} + * @param clientCacheForMS optional. You can cache the response for this amount of MS if you want. Don't pass it in if you don't want caching (needs to be "undefined") */ static async ownedByAddress( address: string, - collections?: string[] + collections?: string[], + clientCacheForMS?: number ): Promise { this.ensureNetworkConfigSet(); @@ -303,15 +318,35 @@ export class DataNft implements DataNftType { collections?.join(',') || dataNftTokenIdentifier[this.env as EnvironmentsEnum]; - const res = await fetch( - `${this.apiConfiguration}/accounts/${address}/nfts?size=10000&collections=${identifiersMap}&withSupply=true` - ); + console.log('SDK debug: ownedByAddress api =', this.apiConfiguration); - checkStatus(res); + const fetchUrl = `${this.apiConfiguration}/accounts/${address}/nfts?size=10000&collections=${identifiersMap}&withSupply=true`; - const data = await res.json(); + // check if its in session cache + let useCache = typeof clientCacheForMS !== 'undefined'; + let jsonDataPayload = null; + const getFromSessionCache = useCache + ? getDataFromClientSessionCache(fetchUrl) + : false; + + if (!getFromSessionCache) { + const response = await fetch(fetchUrl); + checkStatus(response); + jsonDataPayload = await response.json(); + + if (useCache) { + setDataToClientSessionCache( + fetchUrl, + jsonDataPayload, + clientCacheForMS + ); + } + } else { + jsonDataPayload = getFromSessionCache; + } - const dataNfts: DataNft[] = this.createFromApiResponseOrBulk(data); + const dataNfts: DataNft[] = + this.createFromApiResponseOrBulk(jsonDataPayload); return dataNfts; }