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 { buildErrorBody } from "@omniroute/open-sse/utils/error";
import { type NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { getPluginByName, updatePluginConfig } from "@/lib/db/plugins";
import { CORS_HEADERS, handleCorsOptions } from "@/shared/utils/cors";
export async function OPTIONS() {
return handleCorsOptions();
}
/**
* GET /api/plugins/[name]/config — Get plugin configuration
*/
export async function GET(request: NextRequest, { params }: { params: Promise<{ name: string }> }) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
const { name } = await params;
const plugin = getPluginByName(name);
if (!plugin) {
return NextResponse.json(buildErrorBody(404, `Plugin '${name}' not found`), {
status: 404,
headers: CORS_HEADERS,
});
}
return NextResponse.json(
{
config: JSON.parse(plugin.config || "{}"),
configSchema: JSON.parse(plugin.configSchema || "{}"),
},
{ headers: CORS_HEADERS }
);
}
/**
* PUT /api/plugins/[name]/config — Update plugin configuration
*/
export async function PUT(request: NextRequest, { params }: { params: Promise<{ name: string }> }) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
const { name } = await params;
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json(buildErrorBody(400, "Invalid JSON body"), {
status: 400,
headers: CORS_HEADERS,
});
}
const schema = z.object({
config: z.record(z.string(), z.unknown()),
});
const parsed = schema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(buildErrorBody(400, "Invalid request"), {
status: 400,
headers: CORS_HEADERS,
});
}
const plugin = getPluginByName(name);
if (!plugin) {
return NextResponse.json(buildErrorBody(404, `Plugin '${name}' not found`), {
status: 404,
headers: CORS_HEADERS,
});
}
// Validate config values against configSchema if defined
const configSchema = JSON.parse(plugin.configSchema || "{}");
if (Object.keys(configSchema).length > 0) {
for (const [key, value] of Object.entries(parsed.data.config)) {
const field = configSchema[key];
if (!field) continue; // Allow extra keys
if (field.type === "number" && typeof value === "number") {
if (field.min !== undefined && value < field.min) {
return NextResponse.json(buildErrorBody(400, `Config '${key}' must be >= ${field.min}`), {
status: 400,
headers: CORS_HEADERS,
});
}
if (field.max !== undefined && value > field.max) {
return NextResponse.json(buildErrorBody(400, `Config '${key}' must be <= ${field.max}`), {
status: 400,
headers: CORS_HEADERS,
});
}
}
if (field.type === "select" && field.enum && !field.enum.includes(String(value))) {
return NextResponse.json(
buildErrorBody(400, `Config '${key}' must be one of: ${field.enum.join(", ")}`),
{ status: 400, headers: CORS_HEADERS }
);
}
}
}
updatePluginConfig(name, parsed.data.config);
return NextResponse.json(
{ success: true, config: parsed.data.config },
{ headers: CORS_HEADERS }
);
}