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

35 lines
938 B
TypeScript
Raw Normal View History

2024-11-14 12:16:05 -05:00
import { auth } from '@/app/(auth)/auth';
2025-05-13 19:01:28 -07:00
import type { NextRequest } from 'next/server';
2024-11-15 10:13:21 -05:00
import { getChatsByUserId } from '@/lib/db/queries';
2025-05-13 19:01:28 -07:00
import { ChatSDKError } from '@/lib/errors';
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;
2025-05-13 19:01:28 -07:00
const limit = Number.parseInt(searchParams.get('limit') || '10');
2025-04-03 00:28:36 -07:00
const startingAfter = searchParams.get('starting_after');
const endingBefore = searchParams.get('ending_before');
if (startingAfter && endingBefore) {
2025-05-13 19:01:28 -07:00
return new ChatSDKError(
'bad_request:api',
'Only one of starting_after or ending_before can be provided.',
).toResponse();
2025-04-03 00:28:36 -07:00
}
2024-10-11 18:00:22 +05:30
const session = await auth();
2025-05-13 19:01:28 -07:00
if (!session?.user) {
return new ChatSDKError('unauthorized:chat').toResponse();
2024-10-11 18:00:22 +05:30
}
2025-05-13 19:01:28 -07:00
const chats = await getChatsByUserId({
id: session.user.id,
limit,
startingAfter,
endingBefore,
});
2025-04-03 00:28:36 -07:00
2025-05-13 19:01:28 -07:00
return Response.json(chats);
2024-10-11 18:00:22 +05:30
}