route.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { getCollector, listCollectorNames } from "@pipeline/collect";
  3. import { loadConfig } from "@/lib/config";
  4. export async function POST(request: NextRequest) {
  5. const body = await request.json();
  6. const { source, args } = body as { source: string; args?: Record<string, string> };
  7. if (!source) {
  8. return NextResponse.json({ error: "source is required" }, { status: 400 });
  9. }
  10. const available = listCollectorNames();
  11. if (!available.includes(source)) {
  12. return NextResponse.json(
  13. { error: `Unknown source: ${source}. Available: ${available.join(", ")}` },
  14. { status: 400 }
  15. );
  16. }
  17. const config = loadConfig();
  18. const collectorConfig = config?.collect?.[source];
  19. const collector = getCollector(source, collectorConfig);
  20. try {
  21. const result = await collector.collect(args ? { args } : undefined);
  22. const content =
  23. result.type === "json" ? JSON.stringify(result.content, null, 2) : result.content;
  24. return NextResponse.json({ content, type: result.type });
  25. } catch (err: any) {
  26. return NextResponse.json({ error: err.message }, { status: 500 });
  27. }
  28. }