import { NextResponse } from "next/server"; import { getConfig, saveDBConfig, saveAIConfig, saveExportConfig, type DBConfig, type AIConfig, type ExportConfig } from "@/lib/config"; // GET /api/config - 获取完整配置 export async function GET() { try { const config = getConfig(); return NextResponse.json({ success: true, data: config, }); } catch (error) { return NextResponse.json( { success: false, error: "读取配置失败" }, { status: 500 } ); } } // POST /api/config - 保存配置 // Body 格式: { db?: DBConfig, ai?: AIConfig, export?: ExportConfig } export async function POST(request: Request) { try { const body = await request.json(); const { db, ai, export: exportConfig } = body; if (db) { saveDBConfig(db); } if (ai) { saveAIConfig(ai); } if (exportConfig) { saveExportConfig(exportConfig); } // 如果同时发送了完整配置 if (!db && !ai && !exportConfig && body.db === undefined && body.ai === undefined && body.export === undefined) { // 可能是旧的扁平格式,尝试兼容 if (body.apiKey !== undefined || body.baseUrl !== undefined || body.model !== undefined) { saveAIConfig({ apiKey: body.apiKey, baseUrl: body.baseUrl, model: body.model, }); } } return NextResponse.json({ success: true, message: "配置已保存", }); } catch (error) { return NextResponse.json( { success: false, error: "保存失败" }, { status: 500 } ); } }