Files
config-vault/app/api/v1/config/route.ts
T
2026-08-26 14:21:53 +03:00

63 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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: <app_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<string, string> = {}
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,
},
})
}