|
|
@@ -354,65 +354,13 @@ export default {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // Rates endpoint - get all rates from Notion database
|
|
|
- if (url.pathname === '/rates') {
|
|
|
- const base = url.searchParams.get('base') || 'USD';
|
|
|
-
|
|
|
- try {
|
|
|
- const ratesData = await getRatesFromNotion(env);
|
|
|
-
|
|
|
- // If base is USD, return as-is
|
|
|
- if (base.toUpperCase() === 'USD') {
|
|
|
- return new Response(
|
|
|
- JSON.stringify(ratesData, null, 2),
|
|
|
- { headers: { 'Content-Type': 'application/json' } }
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- // Otherwise, recalculate rates for the new base
|
|
|
- const baseRate = ratesData.rates[base.toUpperCase()];
|
|
|
- if (!baseRate) {
|
|
|
- return new Response(
|
|
|
- JSON.stringify({ error: `Unknown currency: ${base}` }),
|
|
|
- { status: 400, headers: { 'Content-Type': 'application/json' } }
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- const convertedRates: Record<string, number> = {};
|
|
|
- for (const [currency, rate] of Object.entries(ratesData.rates)) {
|
|
|
- convertedRates[currency] = rate / baseRate;
|
|
|
- }
|
|
|
- convertedRates['USD'] = 1 / baseRate;
|
|
|
-
|
|
|
- return new Response(
|
|
|
- JSON.stringify(
|
|
|
- {
|
|
|
- base: base.toUpperCase(),
|
|
|
- timestamp: ratesData.timestamp,
|
|
|
- rates: convertedRates,
|
|
|
- },
|
|
|
- null,
|
|
|
- 2
|
|
|
- ),
|
|
|
- { headers: { 'Content-Type': 'application/json' } }
|
|
|
- );
|
|
|
- } catch (error) {
|
|
|
- const errorMessage = error instanceof Error ? error.message : String(error);
|
|
|
- return new Response(
|
|
|
- JSON.stringify({ error: errorMessage }),
|
|
|
- { status: 500, headers: { 'Content-Type': 'application/json' } }
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// Home page
|
|
|
return new Response(
|
|
|
`Notion Exchange Rate Worker
|
|
|
|
|
|
API Endpoints:
|
|
|
- GET /convert?from=USD&to=CNY&amount=100 - Convert currency (data from Notion)
|
|
|
- GET /rates?base=USD - Get exchange rates (data from Notion)
|
|
|
- GET /sync - Sync rates from OXR to Notion`,
|
|
|
+ GET /convert?from=USD&to=CNY&amount=100 - Convert currency
|
|
|
+ GET /sync - Sync rates to Notion`,
|
|
|
{ headers: { 'Content-Type': 'text/plain' } }
|
|
|
);
|
|
|
},
|