import { supabase } from '@/lib/supabase'; import type { APIRoute } from 'astro'; interface CounterResponse { signaturesCount: number; } export async function getSignatureCount(): Promise { const { data: counter } = await supabase .from('unesco_petition_counter') .select('signatures_count') .single(); return counter?.signatures_count || 0; } export const GET: APIRoute = async () => { try { const signaturesCount = await getSignatureCount(); const response: CounterResponse = { signaturesCount, }; return new Response(JSON.stringify(response), { status: 200, headers: { 'Content-Type': 'application/json', 'Cache-Control': 'public, max-age=60', }, }); } catch (error) { return new Response( JSON.stringify({ signaturesCount: 0, error: 'Database is still counting total collected signatures! Please come back!', }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } };