Skip to content

Commit 1c4cbb1

Browse files
committed
feat(jike-client): add queryFollowingUpdates
1 parent 1c97d66 commit 1c4cbb1

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/client/client.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import { JikeUser } from './user'
88
import { fetchPaginated } from './utils/paginate'
99
import { AuthorizationError } from './errors/AuthorizationError'
1010
import { JikePost } from './post'
11+
import type { PersonalUpdate } from '../types/api-responses'
1112
import type { CreatePostOption, PostType } from '../types/options'
12-
import type { Notification } from '../types/entity'
13+
import type { Notification, PostDetail } from '../types/entity'
1314
import type { BeforeRetryState } from 'ky/distribution/types/hooks'
1415
import type { PaginatedFetcher, PaginatedOption } from './utils/paginate'
1516
import type { Api } from '../api'
1617
import type { ApiConfig, ApiConfigResolved } from '../request'
1718

19+
type FollowingUpdatesMoreKey = PersonalUpdate.FollowingUpdatesResponseMoreKey
20+
1821
export class JikeClient {
1922
#refreshToken: string
2023
#config: ApiConfigResolved
@@ -194,6 +197,37 @@ export class JikeClient {
194197
)
195198
}
196199

200+
/**
201+
* 查询关注动态
202+
*/
203+
async queryFollowingUpdates(
204+
option: PaginatedOption<
205+
PostDetail,
206+
'createdAt',
207+
FollowingUpdatesMoreKey
208+
> = {}
209+
) {
210+
const fetcher: PaginatedFetcher<
211+
PostDetail,
212+
FollowingUpdatesMoreKey
213+
> = async (lastKey) => {
214+
const result = await this.#client.personalUpdate.followingUpdates({
215+
loadMoreKey: lastKey,
216+
})
217+
if (!isSuccess(result)) throwRequestFailureError(result, '查询关注动态')
218+
const newKey = result.data.loadMoreKey
219+
return [newKey, result.data.data]
220+
}
221+
return fetchPaginated(
222+
fetcher,
223+
(item, data) => ({
224+
createdAt: new Date(item.createdAt),
225+
total: data.length + 1,
226+
}),
227+
option
228+
)
229+
}
230+
197231
/**
198232
*
199233
* @param type 动态类型,原帖 或 转发

src/types/api-responses.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ export namespace PersonalUpdate {
126126
lastReadTime: number
127127
}
128128
}
129+
130+
export type FollowingUpdatesResponseMoreKey =
131+
FollowingUpdatesResponse['loadMoreKey']
129132
}
130133

131134
export namespace Comments {

tests/jike-client/feeds.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { JikeClient, limit } from '../../src'
3+
import { config, refreshToken } from '../config'
4+
5+
describe('feeds', () => {
6+
const client = new JikeClient({ ...config, refreshToken })
7+
8+
it('queryFollowingUpdates should work', async () => {
9+
const MAX_COUNT = 40
10+
const records = await client.queryFollowingUpdates({
11+
limit: limit.limitMaxCount(MAX_COUNT),
12+
})
13+
expect(records.length).toBe(40)
14+
})
15+
})

0 commit comments

Comments
 (0)