From 90e56e4ea2064d35562f9ae65d73b68dfed7d35e Mon Sep 17 00:00:00 2001 From: Yangshun Date: Sat, 2 Mar 2024 07:49:32 +0800 Subject: [PATCH] offers(fix): migrate to new currency exchange API --- .../utils/offers/currency/currencyExchange.ts | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/apps/portal/src/utils/offers/currency/currencyExchange.ts b/apps/portal/src/utils/offers/currency/currencyExchange.ts index 34845d4a..d5b510b7 100644 --- a/apps/portal/src/utils/offers/currency/currencyExchange.ts +++ b/apps/portal/src/utils/offers/currency/currencyExchange.ts @@ -8,17 +8,13 @@ export const convert = async ( ) => { fromCurrency = fromCurrency.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((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 ( value: number, @@ -33,18 +29,15 @@ export const convertWithDate = async ( fromCurrency = fromCurrency.trim().toLowerCase(); toCurrency = toCurrency.trim().toLowerCase(); - // Format date to YYYY-MM-DD - const formattedDate = date.toJSON().substring(0, 10); - - const url = [ - 'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1', - formattedDate, - 'currencies', - fromCurrency, - toCurrency, - ].join('/'); + // https://github.com/fawazahmed0/exchange-api + // 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'; - 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((data) => value * data[toCurrency]); };