Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { isAuthenticated } from "@/shared/utils/apiAuth";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { databaseSettingsSchema } from "@/shared/validation/settingsSchemas";
import { getDatabaseSettings, updateDatabaseSettings } from "@/lib/db/databaseSettings";
import { redactCacheSecrets } from "@/lib/db/cacheSecrets";
import type { UserDatabaseSettings } from "@/lib/db/databaseSettings";
function publicDatabaseSettings() {
const settings = getDatabaseSettings();
return { ...settings, cache: redactCacheSecrets(settings.cache) };
}
const databaseSettingsPatchSchema = z
.object({
logs: databaseSettingsSchema.shape.logs.partial(),
backup: databaseSettingsSchema.shape.backup.partial(),
cache: databaseSettingsSchema.shape.cache.partial(),
retention: databaseSettingsSchema.shape.retention.partial(),
aggregation: databaseSettingsSchema.shape.aggregation.partial(),
optimization: databaseSettingsSchema.shape.optimization.partial(),
})
.partial()
.strict();
export async function GET(request: NextRequest) {
if (!(await isAuthenticated(request))) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
try {
return NextResponse.json(publicDatabaseSettings());
} catch (error) {
console.error("Error getting database settings:", error);
return NextResponse.json({ error: "Failed to load database settings" }, { status: 500 });
}
}
export async function PATCH(request: NextRequest) {
if (!(await isAuthenticated(request))) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
try {
const rawBody = await request.json();
const validation = validateBody(databaseSettingsPatchSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
updateDatabaseSettings(validation.data as Partial<UserDatabaseSettings>);
// Return merged settings (GET response pattern)
return NextResponse.json(publicDatabaseSettings());
} catch (error) {
console.error("Error updating database settings:", error);
return NextResponse.json({ error: "Failed to update database settings" }, { status: 500 });
}
}
export async function PUT(request: NextRequest) {
return PATCH(request);
}