RTSDA-Website/astro-church-website/src/pages/api/events.ts

40 lines
1.1 KiB
TypeScript

import type { APIRoute } from 'astro';
import { fetchEventsJson } from '../../lib/bindings.js';
export const GET: APIRoute = async ({ request }) => {
try {
const url = new URL(request.url);
const limit = url.searchParams.get('limit');
// Call church-core function directly via napi-rs
const eventsJson = fetchEventsJson();
const events = JSON.parse(eventsJson);
// Apply limit if specified
let filteredEvents = events;
if (limit) {
const limitNum = parseInt(limit, 10);
if (!isNaN(limitNum) && limitNum > 0) {
filteredEvents = events.slice(0, limitNum);
}
}
return new Response(JSON.stringify(filteredEvents), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'public, max-age=300' // 5 minute cache
}
});
} catch (error) {
console.error('Native API Route Error:', error);
return new Response(JSON.stringify({
error: 'Internal server error',
message: 'Failed to call church-core function'
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
};