Skip to content

Commit

Permalink
v3.7.0: adds local caching (for 24h)
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Oct 9, 2023
1 parent c1aa3eb commit 6edc497
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
25 changes: 22 additions & 3 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,14 @@ class Currency {
return window._LocalCurrencyCode || timezoneToCurrency[timeZone || Currency.timeZone()] || 'USD'
}

static getCacheKey(currency) {
const now = new Date();
const year = now.getUTCFullYear();
const month = now.getUTCMonth();
const day = now.getUTCDate();
return `@depay/local-currency/v3.6.1/rates/${currency}/${year}-${month}-${day}`
}

static async rate({ from, to }) {
if(to == undefined) { to = Currency.getCode(); }
let fromToUsd = await Currency.fromUSD({ amount: 1, code: from });
Expand All @@ -448,13 +456,24 @@ class Currency {

static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone });
let rate = await fetch('https://public.depay.com/currencies/' + currency.code)
const cacheKey = Currency.getCacheKey(currency.code);
let cachedValue = localStorage.getItem(cacheKey);
let rate;
if(cachedValue) {
rate = cachedValue;
} else {
rate = await fetch('https://public.depay.com/currencies/' + currency.code)
.then((response) => response.json())
.then((data) => parseFloat(data))
.catch(()=>{
.then((data) => {
let value = parseFloat(data);
localStorage.setItem(cacheKey, value);
return value
})
.catch((e)=>{
currency.code = "USD";
return 1
});
}
currency.amount = currency.amount * rate;
return currency
}
Expand Down
25 changes: 22 additions & 3 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@
return window._LocalCurrencyCode || timezoneToCurrency[timeZone || Currency.timeZone()] || 'USD'
}

static getCacheKey(currency) {
const now = new Date();
const year = now.getUTCFullYear();
const month = now.getUTCMonth();
const day = now.getUTCDate();
return `@depay/local-currency/v3.6.1/rates/${currency}/${year}-${month}-${day}`
}

static async rate({ from, to }) {
if(to == undefined) { to = Currency.getCode(); }
let fromToUsd = await Currency.fromUSD({ amount: 1, code: from });
Expand All @@ -454,13 +462,24 @@

static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone });
let rate = await fetch('https://public.depay.com/currencies/' + currency.code)
const cacheKey = Currency.getCacheKey(currency.code);
let cachedValue = localStorage.getItem(cacheKey);
let rate;
if(cachedValue) {
rate = cachedValue;
} else {
rate = await fetch('https://public.depay.com/currencies/' + currency.code)
.then((response) => response.json())
.then((data) => parseFloat(data))
.catch(()=>{
.then((data) => {
let value = parseFloat(data);
localStorage.setItem(cacheKey, value);
return value
})
.catch((e)=>{
currency.code = "USD";
return 1
});
}
currency.amount = currency.amount * rate;
return currency
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/local-currency",
"moduleName": "LocalCurrency",
"version": "3.6.1",
"version": "3.7.0",
"description": "JavaScript library that detects user's local currency and provides functionalities to convert between multiple currencies.",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down
25 changes: 22 additions & 3 deletions src/Currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class Currency {
return window._LocalCurrencyCode || timezoneToCurrency[timeZone || Currency.timeZone()] || 'USD'
}

static getCacheKey(currency) {
const now = new Date()
const year = now.getUTCFullYear()
const month = now.getUTCMonth()
const day = now.getUTCDate()
return `@depay/local-currency/v3.6.1/rates/${currency}/${year}-${month}-${day}`
}

static async rate({ from, to }) {
if(to == undefined) { to = Currency.getCode() }
let fromToUsd = await Currency.fromUSD({ amount: 1, code: from })
Expand All @@ -23,13 +31,24 @@ class Currency {

static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone })
let rate = await fetch('https://public.depay.com/currencies/' + currency.code)
const cacheKey = Currency.getCacheKey(currency.code)
let cachedValue = localStorage.getItem(cacheKey)
let rate
if(cachedValue) {
rate = cachedValue
} else {
rate = await fetch('https://public.depay.com/currencies/' + currency.code)
.then((response) => response.json())
.then((data) => parseFloat(data))
.catch(()=>{
.then((data) => {
let value = parseFloat(data)
localStorage.setItem(cacheKey, value)
return value
})
.catch((e)=>{
currency.code = "USD"
return 1
})
}
currency.amount = currency.amount * rate
return currency
}
Expand Down

0 comments on commit 6edc497

Please sign in to comment.