增加API分页和轻量级查询支持,完善备份恢复功能

主要改动:
- API: 为 /api/pages 添加 lightweight 模式和分页支持
- 备份恢复: 增强 restore API 的错误处理和验证逻辑
- 工具函数: 新增 page-utils 辅助函数
- 会话管理: 优化 session 和 auth 相关逻辑
- 文档: 添加 CLAUDE.md 项目指南,包含编辑器配置注意事项

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-03 16:30:31 +08:00
parent 7ea9bd7599
commit ce439db449
8 changed files with 324 additions and 23 deletions
+45 -6
View File
@@ -1,22 +1,61 @@
import { NextResponse } from 'next/server';
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { requireApiAuth } from '@/lib/api-auth';
import { cleanupAccidentalStandaloneInlineCode, markdownToImportHtml } from '@/lib/markdown-import';
import { MAX_TITLE_LENGTH, safeParseTags, normalizeTags } from '@/lib/page-utils';
export async function GET() {
export async function GET(request: NextRequest) {
const authError = await requireApiAuth();
if (authError) return authError;
try {
const pages = await prisma.page.findMany({
orderBy: [{ order: 'asc' }, { createdAt: 'desc' }],
});
const searchParams = request.nextUrl.searchParams;
// lightweight 模式:不返回 content,用于侧边栏等场景
const lightweight = searchParams.get('lightweight') === 'true';
const page = parseInt(searchParams.get('page') || '0', 10);
const limit = parseInt(searchParams.get('limit') || '0', 10);
// 默认行为:返回所有页面(向后兼容)
const usePagination = limit > 0 && limit <= 1000 && page > 0;
if (!usePagination) {
const pages = await prisma.page.findMany({
orderBy: [{ order: 'asc' }, { createdAt: 'desc' }],
});
const parsedPages = pages.map((p) => ({
...p,
content: lightweight ? '' : p.content,
tags: safeParseTags(p.tags),
}));
return NextResponse.json(parsedPages);
}
// 分页查询
const skip = (page - 1) * limit;
const [pages, total] = await Promise.all([
prisma.page.findMany({
skip,
take: limit,
orderBy: [{ order: 'asc' }, { createdAt: 'desc' }],
}),
prisma.page.count(),
]);
const parsedPages = pages.map((p) => ({
...p,
content: lightweight ? '' : p.content,
tags: safeParseTags(p.tags),
}));
return NextResponse.json(parsedPages);
return NextResponse.json({
data: parsedPages,
pagination: {
page,
limit,
total,
totalPages: Math.ceil(total / limit),
},
});
} catch {
return NextResponse.json({ error: 'Error fetching pages' }, { status: 500 });
}