Browse Source

📝 docs(generic-crud): update route registration order documentation
- 添加注释说明基础CRUD路由必须放在最后,防止自定义路由被覆盖

♻️ refactor(chat-messages): adjust route registration order
- 将historyRoute移至crudRoutes之前,避免自定义路由被覆盖

🐛 fix(chat-messages): add type annotation and validation
- 为timestamp字段添加显式类型注解
- 使用parseWithAwait进行数据验证,确保响应格式正确

yourname 6 months ago
parent
commit
308bb48289

+ 2 - 2
.roo/rules/12-generic-crud.md

@@ -319,11 +319,11 @@ const yourEntityRoutes = createCrudRoutes({
 import { OpenAPIHono } from '@hono/zod-openapi';
 import { OpenAPIHono } from '@hono/zod-openapi';
 
 
 const app = new OpenAPIHono()
 const app = new OpenAPIHono()
-  .route('/', yourEntityRoutes)
   .get('/custom-action', (c) => {
   .get('/custom-action', (c) => {
     // 自定义路由处理逻辑
     // 自定义路由处理逻辑
     return c.json({ message: '自定义操作' });
     return c.json({ message: '自定义操作' });
-  });
+  })
+  .route('/', yourEntityRoutes); // 基础CRUD路由必需放最后,不然自定义路由会覆盖掉基础CRUD路由
 
 
 export default app;
 export default app;
 ```
 ```

+ 2 - 1
src/server/api/chat-messages/history.ts

@@ -6,6 +6,7 @@ import { AppDataSource } from '@/server/data-source';
 import { ChatMessageService } from '@/server/modules/chat/chat-message.service';
 import { ChatMessageService } from '@/server/modules/chat/chat-message.service';
 import { AuthContext } from '@/server/types/context';
 import { AuthContext } from '@/server/types/context';
 import { authMiddleware } from '@/server/middleware/auth.middleware';
 import { authMiddleware } from '@/server/middleware/auth.middleware';
+import { parseWithAwait } from '@/server/utils/parseWithAwait';
 
 
 // 历史消息查询路由
 // 历史消息查询路由
 const routeDef = createRoute({
 const routeDef = createRoute({
@@ -55,7 +56,7 @@ const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
     );
     );
     
     
     return c.json({
     return c.json({
-      data,
+      data: await parseWithAwait(z.array(ChatMessageSchema), data),
       pagination: {
       pagination: {
         total,
         total,
         current: page,
         current: page,

+ 2 - 2
src/server/api/chat-messages/index.ts

@@ -27,7 +27,7 @@ const crudRoutes = createCrudRoutes({
 
 
 // 创建聚合路由
 // 创建聚合路由
 const app = new OpenAPIHono()
 const app = new OpenAPIHono()
-  .route('/', crudRoutes)
-  .route('/', historyRoute);
+  .route('/', historyRoute)
+  .route('/', crudRoutes);
 
 
 export default app;
 export default app;

+ 1 - 1
src/server/modules/chat/chat-message.schema.ts

@@ -27,7 +27,7 @@ export const ChatMessageSchema = z.object({
     example: '张三',
     example: '张三',
     description: '发送者名称'
     description: '发送者名称'
   }),
   }),
-  timestamp: z.coerce.number().int().positive().openapi({
+  timestamp: z.coerce.number<number>().int().positive().openapi({
     example: 1704067200000,
     example: 1704067200000,
     description: '消息时间戳'
     description: '消息时间戳'
   }),
   }),