This commit is contained in:
Flatlogic Bot 2025-05-12 17:06:50 +00:00
parent 8f190212ca
commit caff5271b8
5 changed files with 45 additions and 12 deletions

File diff suppressed because one or more lines are too long

View File

@ -104,7 +104,7 @@ app.use('/api/file', fileRoutes);
app.use('/api/pexels', pexelsRoutes);
app.enable('trust proxy');
app.use('/api/exchange', exchangeRoutes);
app.use('/api/weather', weatherRoutes);
app.use('/exchange', exchangeRoutes);
app.use(

View File

@ -5,13 +5,18 @@ const router = express.Router();
// GET /api/exchange?base=USD
router.get('/', async (req, res) => {
try {
const base = req.query.base || 'USD';
const response = await axios.get(
`https://api.exchangerate.host/latest?base=${encodeURIComponent(base)}&symbols=USD,EUR`
);
// Return only necessary data
const { base: responseBase, rates, date } = response.data;
res.json({ base: responseBase, rates, date });
const apiKey = process.env.EXCHANGE_API_KEY;
const baseCurrency = req.query.base || 'USD';
if (apiKey) {
const url = `https://api.apilayer.com/exchangerates_data/latest?base=${encodeURIComponent(baseCurrency)}&symbols=USD,EUR`;
const response = await axios.get(url, { headers: { apikey: apiKey } });
return res.json(response.data);
} else {
const url = `https://api.exchangerate.host/latest?base=${encodeURIComponent(baseCurrency)}&symbols=USD,EUR`;
const response = await axios.get(url);
return res.json(response.data);
}
} catch (error) {
console.error('Exchange route error:', error.message);
res.status(500).json({ error: 'Failed to fetch exchange rates' });

View File

@ -0,0 +1,28 @@
const express = require('express');
const axios = require('axios');
const router = express.Router();
// GET /api/exchange?base=USD
router.get('/', async (req, res) => {
try {
const apiKey = process.env.EXCHANGE_API_KEY;
const baseCurrency = req.query.base || 'USD';
if (apiKey) {
const url = `https://api.apilayer.com/exchangerates_data/latest?base=${encodeURIComponent(baseCurrency)}&symbols=USD,EUR`;
const response = await axios.get(url, { headers: { apikey: apiKey } });
return res.json(response.data);
}
const url = `https://api.exchangerate.host/latest?base=${encodeURIComponent(baseCurrency)}&symbols=USD,EUR`;
const response = await axios.get(url);
return res.json(response.data);
const response = await axios.get(url);
return res.json(response.data);
}
} catch (error) {
console.error('Exchange route error:', error.message);
res.status(500).json({ error: 'Failed to fetch exchange rates' });
}
});
module.exports = router;

View File

@ -15,8 +15,7 @@ export interface ExchangeData {
export const getExchangeRates = async (
base: string
): Promise<ExchangeData> => {
const response: AxiosResponse<ExchangeData> = await axios.get(
`/exchange?base=${encodeURIComponent(base)}`
);
const response: AxiosResponse<ExchangeData> = await axios.get(`/exchange?base=${encodeURIComponent(base)}`);
return response.data;
};