import { NextRequest, NextResponse } from 'next/server' import sql from '@/lib/db' /** * GET /api/v1/config?env=production * * Mobile app bu endpoint'i çağırır. * Header: X-Api-Key: * Query: env = development | staging | production (default: production) * * Response: { "KEY": "value", "ANOTHER_KEY": "value2", ... } */ export async function GET(req: NextRequest) { const apiKey = req.headers.get('x-api-key') ?? req.headers.get('authorization')?.replace('Bearer ', '') if (!apiKey) { return NextResponse.json( { error: 'Missing API key. Pass X-Api-Key header.' }, { status: 401 } ) } const env = req.nextUrl.searchParams.get('env') ?? 'production' const validEnvs = ['development', 'staging', 'production'] if (!validEnvs.includes(env)) { return NextResponse.json( { error: `Invalid env. Must be one of: ${validEnvs.join(', ')}` }, { status: 400 } ) } // Find app by API key const [app] = await sql` SELECT id, name FROM apps WHERE api_key = ${apiKey} LIMIT 1 ` if (!app) { return NextResponse.json({ error: 'Invalid API key' }, { status: 401 }) } // Fetch config entries for this app + environment in one join const entries = await sql` SELECT ce.key, ce.value FROM config_entries ce JOIN environments e ON e.id = ce.environment_id WHERE e.app_id = ${app.id} AND e.name = ${env} ` const config: Record = {} for (const entry of entries) { config[entry.key] = entry.value } return NextResponse.json(config, { headers: { 'Cache-Control': 'no-store, no-cache, must-revalidate', 'X-App': app.name, 'X-Env': env, }, }) }