Browse Source

♻️ refactor(post): improve post type definition and schema

- 客户端使用Hono的InferResponseType从API响应推断Post类型,替代直接导入PostEntity
- 完善PostSchema,添加user字段关联用户信息
- 调整HomePage中posts状态的类型定义为新的Post类型
yourname 5 months ago
parent
commit
63928c7329
2 changed files with 8 additions and 4 deletions
  1. 5 2
      src/client/home/pages/HomePage.tsx
  2. 3 2
      src/server/modules/posts/post.entity.ts

+ 5 - 2
src/client/home/pages/HomePage.tsx

@@ -4,13 +4,16 @@ import { UserOutlined, MessageOutlined, HeartOutlined, SendOutlined } from '@ant
 import { useAuth } from '@/client/home/hooks/AuthProvider';
 import { useNavigate } from 'react-router-dom';
 import { postClient } from '@/client/api';
-import type { PostEntity } from '@/server/modules/posts/post.entity';
+import { InferResponseType } from 'hono';
+
+
+type Post = InferResponseType<typeof postClient.$get, 200>['data'][0];
 
 const { Header, Content, Footer } = Layout;
 const { Title, Text, Paragraph } = Typography;
 
 const HomePage: React.FC = () => {
-  const [posts, setPosts] = useState<PostEntity[]>([]);
+  const [posts, setPosts] = useState<Post[]>([]);
   const [loading, setLoading] = useState(true);
   const [content, setContent] = useState('');
   const { user } = useAuth();

+ 3 - 2
src/server/modules/posts/post.entity.ts

@@ -1,6 +1,6 @@
 import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, CreateDateColumn, UpdateDateColumn } from 'typeorm';
 import { z } from '@hono/zod-openapi';
-import { UserEntity } from '../users/user.entity';
+import { UserEntity, UserSchema } from '../users/user.entity';
 import { DeleteStatus } from '@/share/types';
 
 @Entity({ name: 'posts' })
@@ -67,7 +67,8 @@ export const PostSchema = z.object({
     description: '是否删除(0:未删除,1:已删除)'
   }),
   createdAt: z.date().openapi({ description: '创建时间' }),
-  updatedAt: z.date().openapi({ description: '更新时间' })
+  updatedAt: z.date().openapi({ description: '更新时间' }),
+  user: UserSchema
 });
 
 export const CreatePostDto = z.object({