chatbot-template/app/(chat)/api/history/route.ts

38 lines
975 B
TypeScript
Raw Normal View History

2024-11-14 12:16:05 -05:00
import { auth } from '@/app/(auth)/auth';
2025-04-03 00:28:36 -07:00
import { NextRequest } from 'next/server';
2024-11-15 10:13:21 -05:00
import { getChatsByUserId } from '@/lib/db/queries';
2024-10-11 18:00:22 +05:30
2025-04-03 00:28:36 -07:00
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 },
);
}
2024-10-11 18:00:22 +05:30
const session = await auth();
2025-04-03 00:28:36 -07:00
if (!session?.user?.id) {
2024-11-14 12:16:05 -05:00
return Response.json('Unauthorized!', { status: 401 });
2024-10-11 18:00:22 +05:30
}
2025-04-03 00:28:36 -07:00
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 });
}
2024-10-11 18:00:22 +05:30
}