Files
frontend/apps/dc/app/api/rag-authority/file/route.ts
T

40 lines
1.7 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { attestRegisteredFile, FileRegistryError } from "@/server/file-registry-attestation";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
function failure(status: number, code: string): NextResponse {
return NextResponse.json({ code }, {
status, headers: { "Cache-Control": "no-store" },
});
}
function field(value: unknown): string {
return typeof value === "string" ? value.trim() : "";
}
/** Return only registry fields re-read with the caller's dc-service JWT. */
export async function POST(request: NextRequest): Promise<NextResponse> {
try {
if (Number(request.headers.get("content-length") ?? 0) > 4096) {
return failure(413, "FILE_AUTHORITY_REQUEST_TOO_LARGE");
}
const body: unknown = await request.json();
if (!body || typeof body !== "object" || Array.isArray(body)) {
return failure(400, "FILE_AUTHORITY_REQUEST_INVALID");
}
const input = body as Record<string, unknown>;
const fileSn = field(input.fileSn);
const projectId = field(input.projectId);
const clsfSystmSn = field(input.clsfSystmSn);
if (!fileSn) return failure(400, "FILE_AUTHORITY_REQUEST_INVALID");
const record = await attestRegisteredFile(request, { fileSn, projectId, clsfSystmSn }, { requireHeader: true });
return NextResponse.json(record, { headers: { "Cache-Control": "no-store" } });
} catch (error) {
if (error instanceof FileRegistryError) return failure(error.status, error.code);
if (error instanceof SyntaxError) return failure(400, "FILE_AUTHORITY_REQUEST_INVALID");
return failure(502, "FILE_AUTHORITY_UNAVAILABLE");
}
}