76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
|
|
type TreeRestoreResponse = {
|
|
requestId?: string;
|
|
traceId?: string;
|
|
};
|
|
|
|
type RestorePayload = {
|
|
documentId?: string | null;
|
|
workspaceId?: string | null;
|
|
};
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(request: Request) {
|
|
if (!isConvexEnabled()) {
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
|
}
|
|
|
|
try {
|
|
const { documentId, workspaceId }: RestorePayload = await request.json();
|
|
const normalizedDocumentId = typeof documentId === "string" ? documentId.trim() : "";
|
|
if (!normalizedDocumentId) {
|
|
return NextResponse.json({ error: "缺少 documentId" }, { status: 400 });
|
|
}
|
|
|
|
const upstreamUrl = new URL("/api/tree/commands", request.url);
|
|
const response = await fetch(upstreamUrl.toString(), {
|
|
method: "POST",
|
|
headers: new Headers({
|
|
"content-type": "application/json",
|
|
}),
|
|
body: JSON.stringify({
|
|
action: "restore",
|
|
documentId: normalizedDocumentId,
|
|
workspaceId: typeof workspaceId === "string" ? workspaceId.trim() || null : null,
|
|
}),
|
|
});
|
|
|
|
const payload = (await response.json().catch(() => null)) as
|
|
| TreeRestoreResponse
|
|
| { error?: string }
|
|
| null;
|
|
if (!response.ok) {
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
payload && typeof payload === "object" && "error" in payload && typeof payload.error === "string"
|
|
? payload.error
|
|
: "恢复失败,请稍后再试",
|
|
},
|
|
{ status: response.status },
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
meta: {
|
|
requestId: payload?.requestId,
|
|
traceId: payload?.traceId,
|
|
commandName: "tree.node.restore",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{
|
|
error: error instanceof Error ? error.message : "恢复失败,请稍后再试",
|
|
},
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|
|
|
|
export const runtime = "nodejs";
|