offers(fix): migrate to new currency exchange API

pull/637/head
Yangshun 9 months ago
parent f1bf1b61ec
commit 90e56e4ea2

@ -8,17 +8,13 @@ export const convert = async (
) => { ) => {
fromCurrency = fromCurrency.trim().toLowerCase(); fromCurrency = fromCurrency.trim().toLowerCase();
toCurrency = toCurrency.trim().toLowerCase(); toCurrency = toCurrency.trim().toLowerCase();
const url = [
'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies',
fromCurrency,
toCurrency,
].join('/');
return await fetch(url + '.json') return await fetch(
`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${fromCurrency}.json`,
)
.then((res) => res.json()) .then((res) => res.json())
.then((data) => value * data[toCurrency]); .then((data) => value * data[fromCurrency][toCurrency]);
}; };
// https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@{apiVersion}/{date}/{endpoint}
export const convertWithDate = async ( export const convertWithDate = async (
value: number, value: number,
@ -33,18 +29,15 @@ export const convertWithDate = async (
fromCurrency = fromCurrency.trim().toLowerCase(); fromCurrency = fromCurrency.trim().toLowerCase();
toCurrency = toCurrency.trim().toLowerCase(); toCurrency = toCurrency.trim().toLowerCase();
// Format date to YYYY-MM-DD // https://github.com/fawazahmed0/exchange-api
const formattedDate = date.toJSON().substring(0, 10); // Format date to YYYY.M.D
// const formattedDate = date.toJSON().substring(0, 10).replaceAll('-', '.');
const url = [ // TODO: Migrated API does not work with historical API yet, so we use latest for now.
'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1', const formattedDate = 'latest';
formattedDate,
'currencies',
fromCurrency,
toCurrency,
].join('/');
return await fetch(url + '.json') return await fetch(
`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@${formattedDate}/v1/${fromCurrency}.json`,
)
.then((res) => res.json()) .then((res) => res.json())
.then((data) => value * data[toCurrency]); .then((data) => value * data[toCurrency]);
}; };

Loading…
Cancel
Save