File size: 3,111 Bytes
6111b2b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import { NextRequest, NextResponse } from "next/server";
import { CORS_HEADERS, handleCorsOptions } from "@/shared/utils/cors";
import { buildErrorBody } from "@omniroute/open-sse/utils/error";
import { listPlugins } from "@/lib/db/plugins";
import { pluginManager } from "@/lib/plugins/manager";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { z } from "zod";
export async function OPTIONS() {
return handleCorsOptions();
}
/**
* GET /api/plugins — List all installed plugins
*/
const StatusSchema = z.enum(["installed", "active", "inactive", "error"]).optional();
export async function GET(request: NextRequest) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
const url = new URL(request.url);
const statusResult = StatusSchema.safeParse(url.searchParams.get("status"));
if (!statusResult.success) {
return NextResponse.json(
{ error: "Invalid status value", details: statusResult.error.issues },
{ status: 400, headers: CORS_HEADERS }
);
}
try {
const plugins = listPlugins(statusResult.data || undefined);
return NextResponse.json({ plugins: plugins.map(formatPlugin) }, { headers: CORS_HEADERS });
} catch (err: unknown) {
console.error("[plugins] Failed to list plugins:", err);
return NextResponse.json(buildErrorBody(500, "Failed to list plugins"), {
status: 500,
headers: CORS_HEADERS,
});
}
}
/**
* POST /api/plugins — Install a plugin from a local path
*/
export async function POST(request: NextRequest) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
const body = await request.json();
const schema = z.object({
path: z.string().min(1).regex(/^\/[^]*$/, "Path must be absolute").refine(
(p) => !p.includes("\0") && !p.includes(".."),
"Path must not contain traversal patterns or null bytes"
),
});
const parsed = schema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
{ error: "Invalid request", details: parsed.error.issues },
{ status: 400, headers: CORS_HEADERS }
);
}
try {
const plugin = await pluginManager.install(parsed.data.path);
return NextResponse.json(
{ plugin: formatPlugin(plugin) },
{ status: 201, headers: CORS_HEADERS }
);
} catch (err: unknown) {
console.error("[plugins] Failed to install plugin:", err);
return NextResponse.json(buildErrorBody(400, "Failed to install plugin"), {
status: 400,
headers: CORS_HEADERS,
});
}
}
function formatPlugin(row: any) {
return {
id: row.id,
name: row.name,
version: row.version,
description: row.description,
author: row.author,
status: row.status,
enabled: row.enabled === 1,
hooks: JSON.parse(row.hooks || "[]"),
permissions: JSON.parse(row.permissions || "[]"),
installedAt: row.installedAt,
updatedAt: row.updatedAt,
activatedAt: row.activatedAt,
};
}
|