Skip to content

Commit 90e56e4

Browse files
committed
offers(fix): migrate to new currency exchange API
1 parent f1bf1b6 commit 90e56e4

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

apps/portal/src/utils/offers/currency/currencyExchange.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ export const convert = async (
88
) => {
99
fromCurrency = fromCurrency.trim().toLowerCase();
1010
toCurrency = toCurrency.trim().toLowerCase();
11-
const url = [
12-
'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies',
13-
fromCurrency,
14-
toCurrency,
15-
].join('/');
1611

17-
return await fetch(url + '.json')
12+
return await fetch(
13+
`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${fromCurrency}.json`,
14+
)
1815
.then((res) => res.json())
19-
.then((data) => value * data[toCurrency]);
16+
.then((data) => value * data[fromCurrency][toCurrency]);
2017
};
21-
// https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@{apiVersion}/{date}/{endpoint}
2218

2319
export const convertWithDate = async (
2420
value: number,
@@ -33,18 +29,15 @@ export const convertWithDate = async (
3329
fromCurrency = fromCurrency.trim().toLowerCase();
3430
toCurrency = toCurrency.trim().toLowerCase();
3531

36-
// Format date to YYYY-MM-DD
37-
const formattedDate = date.toJSON().substring(0, 10);
38-
39-
const url = [
40-
'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1',
41-
formattedDate,
42-
'currencies',
43-
fromCurrency,
44-
toCurrency,
45-
].join('/');
32+
// https://github.com/fawazahmed0/exchange-api
33+
// Format date to YYYY.M.D
34+
// const formattedDate = date.toJSON().substring(0, 10).replaceAll('-', '.');
35+
// TODO: Migrated API does not work with historical API yet, so we use latest for now.
36+
const formattedDate = 'latest';
4637

47-
return await fetch(url + '.json')
38+
return await fetch(
39+
`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@${formattedDate}/v1/${fromCurrency}.json`,
40+
)
4841
.then((res) => res.json())
4942
.then((data) => value * data[toCurrency]);
5043
};

0 commit comments

Comments
 (0)