Skip to content

Commit

Permalink
feat: Merge pull request #52 from hexaonelabs/dev
Browse files Browse the repository at this point in the history
refactor: only cache existing values
  • Loading branch information
FazioNico authored Jun 26, 2024
2 parents ffc291f + fccc668 commit c16289b
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/servcies/coingecko.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ export type TokenInfo = {

export class CoingeckoAPI {

static options?:RequestInit = process.env.NEXT_PUBLIC_APP_IS_PROD === 'true'
? {
static options:RequestInit|undefined = process.env.NEXT_PUBLIC_APP_IS_PROD === 'true' ?
{
headers: new Headers({
'x-cg-demo-api-key': process.env.NEXT_PUBLIC_APP_COINGECKO_APIKEY
})
}
: undefined;
})
}: undefined

/**
* Method to get Coingecko token id from symbol
Expand Down Expand Up @@ -125,10 +124,13 @@ export class CoingeckoAPI {
// remove duplicates
.filter((item, index, self) => index === self.findIndex((t) => t.time === item.time));
seriesData.set(interval, data);
localStorage.setItem(`hexa-lite-coingeeko/coin/${coinId}/market_chart?interval=${interval}`, JSON.stringify({
data,
timestamp: Date.now()
}));
// only save data if it is not empty
if (data.length > 0 ) {
localStorage.setItem(`hexa-lite-coingeeko/coin/${coinId}/market_chart?interval=${interval}`, JSON.stringify({
data,
timestamp: Date.now()
}));
}
}
}
return seriesData;
Expand All @@ -152,10 +154,12 @@ export class CoingeckoAPI {
// fetch data from coingecko
tokenInfo = await fetch(`https://api.coingecko.com/api/v3/coins/${tokenId}?market_data=true&community_data=true`, this.options)
.then((res) => res.json());
localStorage.setItem(`hexa-lite-coingeeko/coin/${tokenId}/info`, JSON.stringify({
data: tokenInfo,
timestamp: Date.now()
}));
if (tokenInfo) {
localStorage.setItem(`hexa-lite-coingeeko/coin/${tokenId}/info`, JSON.stringify({
data: tokenInfo,
timestamp: Date.now()
}));
}
}
return tokenInfo as TokenInfo;
}
Expand Down

0 comments on commit c16289b

Please sign in to comment.