Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
50 lines · 1.5 KB
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
import type { APIRoute } from "astro";
import { z } from "zod";
import { withErrorHandler } from "@/lib/errors";
import { badRequest, forbidden, parseBody, success, unauthorized } from "@/lib/api";
import { pluginManager } from "@/lib/plugins";

const loadSchema = z.object({
  pluginPath: z.string().min(1),
});

export const GET: APIRoute = withErrorHandler(async ({ locals }) => {
  const user = locals.user;
  if (!user) return unauthorized();
  if (!user.isAdmin) return forbidden();

  const plugins = pluginManager.getAllPlugins();
  const states = pluginManager.getPluginStates();
  const stateMap = new Map(states.map((state) => [state.name, state]));

  return success({
    health: pluginManager.getPluginHealth(),
    plugins: plugins.map((plugin) => ({
      ...plugin,
      runtime: stateMap.get(plugin.name) || null,
      config: pluginManager.getPluginConfig(plugin.name),
    })),
  });
});

export const POST: APIRoute = withErrorHandler(async ({ locals, request }) => {
  const user = locals.user;
  if (!user) return unauthorized();
  if (!user.isAdmin) return forbidden();

  const parsed = await parseBody(request, loadSchema);
  if ("error" in parsed) return parsed.error;

  try {
    await pluginManager.loadPlugin(parsed.data.pluginPath);
  } catch (error) {
    return badRequest(error instanceof Error ? error.message : "Failed to load plugin");
  }

  return success({
    loaded: true,
    pluginPath: parsed.data.pluginPath,
    health: pluginManager.getPluginHealth(),
  });
});