feat: paginate chat history (#903)
This commit is contained in:
parent
235b0edb91
commit
a07a3aad5b
6 changed files with 346 additions and 212 deletions
|
|
@ -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 });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue