Skip to content

Commit

Permalink
feature: make data nft createManyFromApi and ownedByAddress to be opt…
Browse files Browse the repository at this point in the history
… into cache module
  • Loading branch information
newbreedofgeek committed Nov 14, 2024
1 parent 901eee1 commit 1f3b02c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
55 changes: 45 additions & 10 deletions src/datanft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataNft[]> {
this.ensureNetworkConfigSet();
const identifiers = tokens.map(({ nonce, tokenIdentifier }) =>
Expand All @@ -190,6 +192,7 @@ export class DataNft implements DataNftType {
nonce
)
);

if (identifiers.length > MAX_ITEMS) {
throw new ErrTooManyItems();
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -292,26 +305,48 @@ 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<DataNft[]> {
this.ensureNetworkConfigSet();

const identifiersMap =
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;
}

Expand Down

0 comments on commit 1f3b02c

Please sign in to comment.