feat: paginate chat history (#903)

This commit is contained in:
Jeremy 2025-04-03 00:28:36 -07:00 committed by GitHub
parent 235b0edb91
commit a07a3aad5b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 346 additions and 212 deletions

View file

@ -1,14 +1,37 @@
import { auth } from '@/app/(auth)/auth';
import { NextRequest } from 'next/server';
import { getChatsByUserId } from '@/lib/db/queries';
export async function GET() {
export async function GET(request: NextRequest) {
const { searchParams } = request.nextUrl;
const limit = parseInt(searchParams.get('limit') || '10');
const startingAfter = searchParams.get('starting_after');
const endingBefore = searchParams.get('ending_before');
if (startingAfter && endingBefore) {
return Response.json(
'Only one of starting_after or ending_before can be provided!',
{ status: 400 },
);
}
const session = await auth();
if (!session || !session.user) {
if (!session?.user?.id) {
return Response.json('Unauthorized!', { status: 401 });
}
// biome-ignore lint: Forbidden non-null assertion.
const chats = await getChatsByUserId({ id: session.user.id! });
return Response.json(chats);
try {
const chats = await getChatsByUserId({
id: session.user.id,
limit,
startingAfter,
endingBefore,
});
return Response.json(chats);
} catch (_) {
return Response.json('Failed to fetch chats!', { status: 500 });
}
}