Skip to content

Commit 57c4069

Browse files
committed
feat(api): add list comment
1 parent 0b6a8ec commit 57c4069

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
- [动态广场](http://jike-sdk.sxzz.moe/modules/api_recommend_feed.html)
5252
- [x] [获取动态推荐](http://jike-sdk.sxzz.moe/functions/api_recommend_feed.list.html)
5353
- [评论](http://jike-sdk.sxzz.moe/modules/api_comments.html)
54-
- [x] [获取评论](http://jike-sdk.sxzz.moe/functions/api_comments.listPrimary.html)
54+
- [x] [获取动态下评论](http://jike-sdk.sxzz.moe/functions/api_comments.listPrimary.html)
55+
- [x] [获取评论](http://jike-sdk.sxzz.moe/functions/api_comments.list.html)
5556
- [x] [发送评论](http://jike-sdk.sxzz.moe/functions/api_comments.add.html) (发送子评论)
5657
- [x] [点赞](http://jike-sdk.sxzz.moe/functions/api_comments.like.html)
5758
- [x] [取消点赞](http://jike-sdk.sxzz.moe/functions/api_comments.unlike.html)

src/api/comments.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { request, toResponse } from '../request'
22
import { type PostTypeRaw } from '../types/entity'
3-
import { type AddCommentOption, type ListCommentOption } from '../types/options'
3+
import {
4+
type AddCommentOption,
5+
type ListCommentMoreKey,
6+
type ListPrimaryCommentOption,
7+
type PaginationOption,
8+
} from '../types/options'
49
import { type Comments } from '../types/api-responses'
510

611
export const add = <T = Comments.AddResponse>(
@@ -52,7 +57,7 @@ export const remove = <T = Comments.RemoveResponse>(
5257
export const listPrimary = <T = Comments.ListPrimaryResponse>(
5358
targetType: PostTypeRaw,
5459
targetId: string,
55-
option: ListCommentOption = {}
60+
option: ListPrimaryCommentOption = {}
5661
) =>
5762
toResponse<T>(
5863
request.post('1.0/comments/listPrimary', {
@@ -65,3 +70,19 @@ export const listPrimary = <T = Comments.ListPrimaryResponse>(
6570
},
6671
})
6772
)
73+
74+
export const list = <T = Comments.ListResponse>(
75+
targetType: PostTypeRaw,
76+
threadId: string,
77+
option: PaginationOption<ListCommentMoreKey> = {}
78+
) =>
79+
toResponse<T>(
80+
request.post('1.0/comments/list', {
81+
json: {
82+
targetType,
83+
threadId,
84+
limit: option.limit ?? 20,
85+
loadMoreKey: option.loadMoreKey,
86+
},
87+
})
88+
)

src/client/post.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
type AddCommentOption,
3-
type ListCommentMoreKey,
4-
type ListCommentOption,
3+
type ListPrimaryCommentMoreKey,
4+
type ListPrimaryCommentOption,
55
type PostType,
66
} from '../types/options'
77
import { type Comment, type Post, type User } from '../types/entity'
@@ -143,10 +143,10 @@ export class JikePost {
143143
* 查询评论
144144
*/
145145
queryComments(
146-
order?: ListCommentOption['order'],
147-
option: PaginatedOption<Comment, 'createdAt', ListCommentMoreKey> = {}
146+
order?: ListPrimaryCommentOption['order'],
147+
option: PaginatedOption<Comment, 'createdAt', ListPrimaryCommentMoreKey> = {}
148148
) {
149-
const fetcher: PaginatedFetcher<Comment, ListCommentMoreKey> = async (
149+
const fetcher: PaginatedFetcher<Comment, ListPrimaryCommentMoreKey> = async (
150150
lastKey
151151
) => {
152152
const result = await this.apiClient.comments.listPrimary(

src/types/api-responses.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import {
1515
type Topic,
1616
type User,
1717
} from './entity'
18+
import {
19+
type ListCommentMoreKey,
20+
type ListPrimaryCommentMoreKey,
21+
} from './options'
1822

1923
export namespace Posts {
2024
export interface CreateResponse {
@@ -196,10 +200,12 @@ export namespace Comments {
196200

197201
export interface ListPrimaryResponse {
198202
data: Comment[]
199-
loadMoreKey?: {
200-
key: string
201-
partition: string
202-
}
203+
loadMoreKey?: ListPrimaryCommentMoreKey
204+
}
205+
206+
export interface ListResponse {
207+
data: Comment[]
208+
loadMoreKey?: ListCommentMoreKey
203209
}
204210
}
205211

src/types/options.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,27 @@ export interface AddCommentOption {
7979
replyToCommentId?: string
8080
}
8181

82-
export interface ListCommentMoreKey {
82+
export interface ListPrimaryCommentMoreKey {
8383
key: string
8484
partition: string
8585
}
8686
/**
87-
* @description 获取评论选项
87+
* @description 获取主要评论选项
8888
*/
89-
export interface ListCommentOption
90-
extends PaginationOption<ListCommentMoreKey> {
89+
export interface ListPrimaryCommentOption
90+
extends PaginationOption<ListPrimaryCommentMoreKey> {
9191
/**
9292
* 排序
9393
* @default `LIKES`
9494
*/
9595
order?: 'LIKES' | 'TIME'
9696
}
9797

98+
export interface ListCommentMoreKey {
99+
skip: number
100+
partition: LiteralUnion<'NORMAL'>
101+
}
102+
98103
/**
99104
* @description 获取动态广场选项
100105
*/

tests/api/posts.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ describe('posts should work', () => {
3838
})
3939

4040
describe('comment should work', () => {
41-
it('get should work', async () => {
41+
it('list should work', async () => {
4242
const result = await api.comments.listPrimary(
4343
'ORIGINAL_POST',
44-
'61c0d39877abf80010426ba3'
44+
'65071ec94101002b06c3469d'
4545
)
4646
expect(isSuccess(result)).toBe(true)
4747
expect(result.data.data.length).greaterThan(0)
48+
49+
const comments = await api.comments.list(
50+
'ORIGINAL_POST',
51+
result.data.data[0].threadId
52+
)
53+
expect(comments.data.data.length).greaterThan(0)
4854
})
4955

5056
it('like should work', async () => {
@@ -75,7 +81,7 @@ describe('new post should work', () => {
7581
{ topicId: '5be41ae2a666dd00172d6072' }
7682
)
7783
expect(isSuccess(result)).toBe(true)
78-
expect(result.data.toast).toBe('发送成功')
84+
expect(result.data.toast).a('string')
7985
expect(result.data.data.id).a('string')
8086
id = result.data.data.id
8187
})
@@ -87,14 +93,14 @@ describe('new post should work', () => {
8793
`测试评论${Math.random()}`
8894
)
8995
expect(isSuccess(result)).toBe(true)
90-
expect(result.data.toast).toBe('发送成功')
96+
expect(result.data.toast).a('string')
9197
expect(result.data.data.id).a('string')
9298
})
9399

94100
it('remove should work', async () => {
95101
const result = await api.posts.remove(PostType.ORIGINAL, id)
96102
expect(isSuccess(result)).toBe(true)
97-
expect(result.data.toast).toBe('删除成功')
103+
expect(result.data.toast).a('string')
98104
})
99105

100106
it('listLikedUsers should work', async () => {

0 commit comments

Comments
 (0)