sing-ui/app/api/pipelines/[id]/route.ts

64 lines
1.8 KiB
TypeScript

import { deletePipeline, getPipeline, updatePipeline } from "@/lib/db";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
type Ctx = { params: Promise<{ id: string }> };
function normalizeStreams(raw: unknown): string {
if (typeof raw === "string") return raw;
return JSON.stringify(raw ?? []);
}
export async function GET(_request: Request, { params }: Ctx) {
try {
const { id } = await params;
const pipeline = getPipeline(Number(id));
if (!pipeline) {
return Response.json({ error: "pipeline not found" }, { status: 404 });
}
return Response.json(pipeline);
} catch (err) {
return Response.json({ error: (err as Error).message }, { status: 500 });
}
}
export async function PUT(request: Request, { params }: Ctx) {
try {
const { id } = await params;
const body = await request.json();
const patch: Record<string, unknown> = {};
for (const key of [
"name",
"source_conn_id",
"target_conn_id",
"schema_sync",
"mode",
]) {
if (body[key] !== undefined) patch[key] = body[key];
}
if (body.streams !== undefined) {
patch.streams = normalizeStreams(body.streams);
}
const pipeline = updatePipeline(Number(id), patch);
if (!pipeline) {
return Response.json({ error: "pipeline not found" }, { status: 404 });
}
return Response.json(pipeline);
} catch (err) {
return Response.json({ error: (err as Error).message }, { status: 500 });
}
}
export async function DELETE(_request: Request, { params }: Ctx) {
try {
const { id } = await params;
if (!deletePipeline(Number(id))) {
return Response.json({ error: "pipeline not found" }, { status: 404 });
}
return Response.json({ ok: true });
} catch (err) {
return Response.json({ error: (err as Error).message }, { status: 500 });
}
}