Skip to content

Commit

Permalink
v3.2.2: fallback to USD if conversion fails
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Mar 7, 2022
1 parent 7cbe1fa commit ad9177e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
6 changes: 5 additions & 1 deletion dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,11 @@ class Currency {
let currency = new Currency({ amount, code, timeZone });
let rate = await fetch('https://public.depay.fi/currencies/' + currency.code)
.then((response) => response.json())
.then((data) => parseFloat(data));
.then((data) => parseFloat(data))
.catch(()=>{
currency.code = "USD";
return 1
});
currency.amount = currency.amount * rate;
return currency
}
Expand Down
6 changes: 5 additions & 1 deletion dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,11 @@
let currency = new Currency({ amount, code, timeZone });
let rate = await fetch('https://public.depay.fi/currencies/' + currency.code)
.then((response) => response.json())
.then((data) => parseFloat(data));
.then((data) => parseFloat(data))
.catch(()=>{
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.2.1",
"version": "3.2.2",
"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
4 changes: 4 additions & 0 deletions src/Currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Currency {
let rate = await fetch('https://public.depay.fi/currencies/' + currency.code)
.then((response) => response.json())
.then((data) => parseFloat(data))
.catch(()=>{
currency.code = "USD"
return 1
})
currency.amount = currency.amount * rate
return currency
}
Expand Down
19 changes: 17 additions & 2 deletions tests/units/Currency.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,29 @@ describe('Currency', () => {
})

it('converts currency via API', async ()=> {
let currency = await Currency.fromUSD({ amount: 20, timeZone: 'Europe/Berlin', apiKey: 'Test123' })
let currency = await Currency.fromUSD({ amount: 20, timeZone: 'Europe/Berlin' })
expect(currency.toString()).toEqual('€106.42')
})

it('converts currency via API also for given code', async ()=> {
let currency = await Currency.fromUSD({ amount: 20, code: 'EUR', apiKey: 'Test123' })
let currency = await Currency.fromUSD({ amount: 20, code: 'EUR' })
expect(currency.toString()).toEqual('€106.42')
})

describe('api unreachable', ()=>{
beforeEach(()=>{
fetchMock.get({
url: 'https://public.depay.fi/currencies/EUR',
overwriteRoutes: true
}, 500
)
})

it('leaves it in USD and does not convert it', async ()=> {
let currency = await Currency.fromUSD({ amount: 20, timeZone: 'Europe/Berlin' })
expect(currency.toString()).toEqual('$20.00')
})
})
})

describe('set code explicitly', ()=> {
Expand Down

0 comments on commit ad9177e

Please sign in to comment.