65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
import {
|
|
assertDocumentId,
|
|
assertTitle,
|
|
buildDocumentBridgeContext,
|
|
buildDocumentCommandEnvelope,
|
|
documentBridgeErrorResponse,
|
|
} from "@/lib/documents/bridge";
|
|
import {
|
|
executeMetadataBridgeCommand,
|
|
type DocumentTitleUpdatePayload,
|
|
} from "@/lib/documents/metadata-command-adapter";
|
|
|
|
interface RenamePayload {
|
|
documentId: string;
|
|
workspaceId?: string | null;
|
|
title: string;
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
if (isConvexEnabled()) {
|
|
try {
|
|
const { documentId, workspaceId, title }: RenamePayload = await request.json();
|
|
const normalizedDocumentId = assertDocumentId(documentId);
|
|
const normalizedTitle = assertTitle(title);
|
|
const normalizedWorkspaceId = workspaceId?.trim() || null;
|
|
const bridgeContext = await buildDocumentBridgeContext({ request, workspaceId: normalizedWorkspaceId });
|
|
const envelope = buildDocumentCommandEnvelope({
|
|
name: "documents.title.update",
|
|
payload: {
|
|
documentId: normalizedDocumentId,
|
|
workspaceId: normalizedWorkspaceId,
|
|
title: normalizedTitle,
|
|
} satisfies DocumentTitleUpdatePayload,
|
|
context: bridgeContext,
|
|
target: {
|
|
workspaceId: normalizedWorkspaceId,
|
|
pageId: normalizedDocumentId,
|
|
},
|
|
});
|
|
const result = await executeMetadataBridgeCommand({
|
|
context: bridgeContext,
|
|
envelope,
|
|
});
|
|
|
|
return NextResponse.json({
|
|
ok: true,
|
|
meta: {
|
|
requestId: result.requestId,
|
|
traceId: result.traceId,
|
|
commandId: result.commandId,
|
|
commandName: result.commandName,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
return documentBridgeErrorResponse(error);
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
|
}
|
|
|
|
export const runtime = "nodejs";
|