v3: added a chatbox
Some checks failed
Build and Publish Docker Image / build-and-push (push) Has been cancelled
Some checks failed
Build and Publish Docker Image / build-and-push (push) Has been cancelled
This commit is contained in:
26
api/routes/chat.ts
Normal file
26
api/routes/chat.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Router } from "@oak/oak";
|
||||
import type { APIResponse, ChatMessage } from "../model/interfaces.ts";
|
||||
import { authMiddleware } from "../middleware/auth.ts";
|
||||
import { getRecentChatMessages } from "../services/chat-service.ts";
|
||||
|
||||
const router = new Router({ prefix: "/api/chat" });
|
||||
|
||||
// GET /api/chat — recent messages (ascending). Optional `before` (ISO created_at)
|
||||
// cursor loads the page of older messages, and `limit` caps the page size.
|
||||
router.get("/", authMiddleware, (ctx) => {
|
||||
const params = ctx.request.url.searchParams;
|
||||
const before = params.get("before") ?? undefined;
|
||||
const limitParam = params.get("limit");
|
||||
const limit = limitParam ? Number(limitParam) : undefined;
|
||||
const messages = getRecentChatMessages(
|
||||
Number.isFinite(limit) ? limit : undefined,
|
||||
before,
|
||||
);
|
||||
const responseBody: APIResponse<ChatMessage[]> = {
|
||||
success: true,
|
||||
data: messages,
|
||||
};
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user