Files
mnote/wolai-frontend/src/app/api/documents/content/route.ts
T

66 lines
1.9 KiB
TypeScript
Raw Normal View History

2026-01-10 10:35:21 +08:00
import { NextResponse } from "next/server";
import { createSupabaseRouteClient } from "@/lib/supabase/server";
2026-01-17 10:12:53 +08:00
import { isConvexEnabled } from "@/lib/convex/enabled";
import { getConvexHttpClient } from "@/lib/convex/server";
import { api } from "@/lib/convex/api";
import { requireAuthContext } from "@/lib/auth/authContext";
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-01-18 05:13:53 +08:00
const auth = await requireAuthContext();
2026-01-17 10:12:53 +08:00
const url = new URL(request.url);
const documentId = url.searchParams.get("documentId") ?? "";
if (!documentId) {
return NextResponse.json({ error: "缺少 documentId" }, { status: 400 });
}
const client = getConvexHttpClient();
const result = await client.query(api.documents.getContent, {
userId: auth.userId,
id: documentId,
});
if (!result) {
return NextResponse.json({ error: "页面不存在" }, { status: 404 });
}
return NextResponse.json({ content: result.content ?? null });
}
2026-01-10 10:35:21 +08:00
const supabase = await createSupabaseRouteClient();
const {
data: { session },
} = await supabase.auth.getSession();
if (!session) {
return NextResponse.json({ error: "未登录" }, { status: 401 });
}
const url = new URL(request.url);
const documentId = url.searchParams.get("documentId") ?? "";
if (!documentId) {
return NextResponse.json({ error: "缺少 documentId" }, { status: 400 });
}
const { data: document, error } = await supabase
.from("documents")
.select("id,content")
.eq("id", documentId)
.eq("user_id", session.user.id)
.single();
if (error) {
return NextResponse.json({ error: error.message }, { status: 400 });
}
if (!document) {
return NextResponse.json({ error: "页面不存在" }, { status: 404 });
}
return NextResponse.json({ content: document.content ?? null });
}