import { NextRequest, NextResponse } from 'next/server' /** * Proxy API route for elections endpoint * Forwards requests to the backend API */ export async function GET(request: NextRequest) { try { const searchParams = request.nextUrl.searchParams const backendUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000' // Build the backend URL with query parameters const url = new URL('/api/elections', backendUrl) // Copy all query parameters from the incoming request searchParams.forEach((value, key) => { url.searchParams.append(key, value) }) // Get the authorization header if present const authHeader = request.headers.get('authorization') const headers: HeadersInit = { 'Content-Type': 'application/json', } if (authHeader) { headers['Authorization'] = authHeader } // Forward the request to the backend const response = await fetch(url.toString(), { method: 'GET', headers, }) const data = await response.json() // Return the response with the same status code return NextResponse.json(data, { status: response.status }) } catch (error) { console.error('Error proxying elections request:', error) return NextResponse.json( { detail: 'Error proxying request to backend' }, { status: 500 } ) } }