mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2026-04-13 13:19:49 +08:00
offers(fix): migrate to new currency exchange API
This commit is contained in:
@@ -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('-', '.');
|
||||||
|
// TODO: Migrated API does not work with historical API yet, so we use latest for now.
|
||||||
|
const formattedDate = 'latest';
|
||||||
|
|
||||||
const url = [
|
return await fetch(
|
||||||
'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1',
|
`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@${formattedDate}/v1/${fromCurrency}.json`,
|
||||||
formattedDate,
|
)
|
||||||
'currencies',
|
|
||||||
fromCurrency,
|
|
||||||
toCurrency,
|
|
||||||
].join('/');
|
|
||||||
|
|
||||||
return await fetch(url + '.json')
|
|
||||||
.then((res) => res.json())
|
.then((res) => res.json())
|
||||||
.then((data) => value * data[toCurrency]);
|
.then((data) => value * data[toCurrency]);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user