From cee120a6f862b6d69235403f8e000b669fdfe235 Mon Sep 17 00:00:00 2001 From: lafay <2021211506@stu.hit.edu.cn> Date: Mon, 23 Mar 2026 00:16:10 +0800 Subject: [PATCH] refactor(PostRepository): update PostsListApiResponse structure and handle response mapping - Change 'posts' to 'list' in PostsListApiResponse for consistency with API response. - Introduce optional fields: 'page', 'page_size', and 'total_pages' for enhanced pagination support. - Update response handling in PostRepository methods to accommodate new structure and ensure default values for 'hasMore'. --- src/data/repositories/PostRepository.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/data/repositories/PostRepository.ts b/src/data/repositories/PostRepository.ts index 5d26cb0..c013194 100644 --- a/src/data/repositories/PostRepository.ts +++ b/src/data/repositories/PostRepository.ts @@ -53,10 +53,13 @@ interface PostApiResponse { * API帖子列表响应类型 */ interface PostsListApiResponse { - posts: PostApiResponse[]; - has_more: boolean; + list: PostApiResponse[]; + has_more?: boolean; next_cursor?: string; total?: number; + page?: number; + page_size?: number; + total_pages?: number; } /** @@ -242,7 +245,7 @@ export class PostRepository implements IPostRepository { const response = await this.api.get('/posts', queryParams); - const posts = response.posts.map(p => this.mapToPost(p)); + const posts = (response.list || []).map(p => this.mapToPost(p)); // 缓存帖子 posts.forEach(post => { @@ -252,7 +255,7 @@ export class PostRepository implements IPostRepository { return { posts, - hasMore: response.has_more, + hasMore: response.has_more ?? false, nextCursor: response.next_cursor, total: response.total, }; @@ -315,7 +318,7 @@ export class PostRepository implements IPostRepository { const response = await this.api.get(`/users/${userId}/posts`, queryParams); - const posts = response.posts.map(p => this.mapToPost(p)); + const posts = (response.list || []).map(p => this.mapToPost(p)); // 缓存帖子 posts.forEach(post => { @@ -325,7 +328,7 @@ export class PostRepository implements IPostRepository { return { posts, - hasMore: response.has_more, + hasMore: response.has_more ?? false, nextCursor: response.next_cursor, total: response.total, }; @@ -350,7 +353,7 @@ export class PostRepository implements IPostRepository { const response = await this.api.get('/posts/search', queryParams); - const posts = response.posts.map(p => this.mapToPost(p)); + const posts = (response.list || []).map(p => this.mapToPost(p)); // 缓存帖子 posts.forEach(post => { @@ -360,7 +363,7 @@ export class PostRepository implements IPostRepository { return { posts, - hasMore: response.has_more, + hasMore: response.has_more ?? false, nextCursor: response.next_cursor, total: response.total, };