2026-01-10 10:35:21 +08:00
|
|
|
import { NextResponse } from "next/server";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
2026-01-18 19:01:31 +08:00
|
|
|
import { getAuthedConvexClient } from "@/lib/convex/route";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { api } from "@/lib/convex/api";
|
2026-04-14 13:22:29 +08:00
|
|
|
import {
|
|
|
|
|
assertDocumentId,
|
|
|
|
|
buildDocumentBridgeContext,
|
|
|
|
|
buildDocumentQueryEnvelope,
|
|
|
|
|
documentBridgeErrorResponse,
|
|
|
|
|
} from "@/lib/documents/bridge";
|
2026-01-10 10:35:21 +08:00
|
|
|
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
|
|
|
|
export async function GET(request: Request) {
|
2026-01-17 10:12:53 +08:00
|
|
|
if (isConvexEnabled()) {
|
2026-04-14 13:22:29 +08:00
|
|
|
try {
|
|
|
|
|
const url = new URL(request.url);
|
|
|
|
|
const documentId = assertDocumentId(url.searchParams.get("documentId"));
|
|
|
|
|
const workspaceId = url.searchParams.get("workspaceId")?.trim() || null;
|
|
|
|
|
const { client } = await getAuthedConvexClient();
|
|
|
|
|
const bridgeContext = await buildDocumentBridgeContext({ request, workspaceId });
|
|
|
|
|
const envelope = buildDocumentQueryEnvelope({
|
|
|
|
|
name: "documents.content.get",
|
|
|
|
|
payload: { documentId, workspaceId },
|
|
|
|
|
});
|
2026-01-17 10:12:53 +08:00
|
|
|
|
2026-04-14 13:22:29 +08:00
|
|
|
const result = await client.query(api.documents.getContent, {
|
|
|
|
|
id: documentId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
error: "页面不存在",
|
|
|
|
|
meta: {
|
|
|
|
|
requestId: bridgeContext.requestId,
|
|
|
|
|
traceId: bridgeContext.traceId,
|
|
|
|
|
queryName: envelope.name,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ status: 404 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
content: result.content ?? null,
|
|
|
|
|
meta: {
|
|
|
|
|
requestId: bridgeContext.requestId,
|
|
|
|
|
traceId: bridgeContext.traceId,
|
|
|
|
|
queryName: envelope.name,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return documentBridgeErrorResponse(error);
|
2026-01-17 10:12:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2026-01-10 10:35:21 +08:00
|
|
|
}
|