feat(api): REST-Endpunkte fuer Projekte, Notizen, Verbindungen und Sync
Alles, was die Oberflaeche kann, ist damit auch scriptbar: GET POST /api/projects GET PATCH DELETE /api/projects/:id POST /api/projects/:id/sync GET POST /api/projects/:id/notes PATCH DELETE /api/notes/:id GET POST /api/connections PATCH DELETE /api/connections/:id POST /api/connections/:id/check GET /api/connections/:id/repos GET /api/health Details: - Jede Eingabe laeuft durch Zod; Feldfehler kommen als Objekt zurueck, damit Formulare sie direkt anzeigen koennen. - Beim Anlegen werden Owner und Repo-Name aus der URL abgeleitet, falls nicht explizit angegeben. - Ein leeres Token-Feld beim Bearbeiten einer Verbindung laesst das gespeicherte Token unveraendert; null entfernt es. - Verbindungsantworten enthalten nie das Chiffrat, nur einen Hinweis wie "ghp_...9f3a". - /api/health prueft die Datenbank und dient dem Docker-Healthcheck. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { encryptSecret, tokenHint } from "@/lib/crypto";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { normalizeBaseUrl } from "@/lib/git/http";
|
||||
import { connectionSafeSelect } from "@/lib/projects";
|
||||
import { connectionCreateSchema, formatZodError } from "@/lib/validation";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/** Verbindungen ohne Token-Chiffrat – das verlaesst den Server nie. */
|
||||
const safeSelect = {
|
||||
...connectionSafeSelect,
|
||||
_count: { select: { projects: true } },
|
||||
};
|
||||
|
||||
export async function GET() {
|
||||
const connections = await prisma.gitConnection.findMany({
|
||||
select: safeSelect,
|
||||
orderBy: { name: "asc" },
|
||||
});
|
||||
return NextResponse.json({ connections });
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Ungueltiger JSON-Body" }, { status: 400 });
|
||||
}
|
||||
|
||||
const parsed = connectionCreateSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: "Validierung fehlgeschlagen", fields: formatZodError(parsed.error) },
|
||||
{ status: 422 },
|
||||
);
|
||||
}
|
||||
|
||||
const { name, provider, baseUrl, username, token } = parsed.data;
|
||||
|
||||
const duplicate = await prisma.gitConnection.findUnique({ where: { name } });
|
||||
if (duplicate) {
|
||||
return NextResponse.json(
|
||||
{ error: "Es gibt bereits eine Verbindung mit diesem Namen.", fields: { name: "Name bereits vergeben" } },
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
|
||||
const connection = await prisma.gitConnection.create({
|
||||
data: {
|
||||
name,
|
||||
provider,
|
||||
baseUrl: normalizeBaseUrl(baseUrl),
|
||||
username: username ?? null,
|
||||
tokenCipher: token ? encryptSecret(token) : null,
|
||||
tokenHint: token ? tokenHint(token) : null,
|
||||
},
|
||||
select: safeSelect,
|
||||
});
|
||||
|
||||
return NextResponse.json({ connection }, { status: 201 });
|
||||
}
|
||||
Reference in New Issue
Block a user