Skip to content

Commit 9bf4a63

Browse files
committed
feat: add follow and unfollow
1 parent 5169c2a commit 9bf4a63

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
- 用户关系
3030
- [x] 获取关注列表
3131
- [x] 获取被关注列表
32+
- [x] 关注
33+
- [x] 取消关注
3234
- 动态帖子
3335
- [x] 发送动态
3436
- [x] 获取动态详情

src/api/user-relation.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { request, toResponse } from '../request'
22
import type { UserRelation } from '../types/api-responses'
3-
import type { PaginationOption } from '../types/options'
3+
import type {
4+
PaginationOption,
5+
UserFollowOption,
6+
UserUnfollowOption,
7+
} from '../types/options'
48

59
/**
610
* 获取关注列表
@@ -39,3 +43,42 @@ export const getFollowerList = <T = UserRelation.GetFollowerListResponse>(
3943
},
4044
})
4145
)
46+
47+
/**
48+
* 关注用户
49+
* @param username 用户名
50+
* @param pageName 页面名称,可选
51+
*/
52+
export const follow = <T = UserRelation.FollowResponse>(
53+
username: string,
54+
{ pageName = 11 }: UserFollowOption = {}
55+
) =>
56+
toResponse<T>(
57+
request.post('1.0/userRelation/follow', {
58+
json: {
59+
sourcePageName: pageName,
60+
currentPageName: pageName,
61+
username,
62+
},
63+
})
64+
)
65+
66+
/**
67+
* 取消关注用户
68+
* @param username 用户名
69+
* @param option 选项,可选
70+
*/
71+
export const unfollow = <T = UserRelation.UnfollowResponse>(
72+
username: string,
73+
{ pageName = 49, ref = 'PROFILE_MY_FOLLOWINGS' }: UserUnfollowOption = {}
74+
) =>
75+
toResponse<T>(
76+
request.post('1.0/userRelation/unfollow', {
77+
json: {
78+
currentPageName: pageName,
79+
username,
80+
ref,
81+
sourcePageName: pageName,
82+
},
83+
})
84+
)

src/client/user.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isSuccess, throwRequestFailureError } from './utils/response'
22
import { fetchPaginated } from './utils/paginate'
33
import { JikePostWithDetail } from './post'
44
import { rawTypeToEnum } from './utils/post'
5+
import type { UserUnfollowOption } from '../types/options'
56
import type { PostDetail, PostTypeRaw, User } from '../types/entity'
67
import type { Users } from '../types/api-responses'
78
import type { PaginatedFetcher, PaginatedOption } from './utils/paginate'
@@ -185,4 +186,25 @@ export class JikeUser<M extends boolean = boolean> {
185186
option
186187
)
187188
}
189+
190+
/**
191+
* 关注用户
192+
*/
193+
async follow() {
194+
const result = await this.#client.apiClient.userRelation.follow(
195+
await this.getUsername()
196+
)
197+
if (!isSuccess(result)) throwRequestFailureError(result, '关注用户')
198+
}
199+
200+
/**
201+
* 取消关注用户
202+
*/
203+
async unfollow(option: UserUnfollowOption = {}) {
204+
const result = await this.#client.apiClient.userRelation.unfollow(
205+
await this.getUsername(),
206+
option
207+
)
208+
if (!isSuccess(result)) throwRequestFailureError(result, '取消关注用户')
209+
}
188210
}

src/types/api-responses.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-namespace */
2+
import type { LiteralUnion } from '../utils/typings'
23
import type {
34
Comment,
45
FollowingUpdate,
@@ -48,6 +49,14 @@ export namespace UserRelation {
4849
createdAt: string
4950
}
5051
}
52+
53+
export interface FollowResponse {
54+
toast: LiteralUnion<'关注成功'>
55+
}
56+
57+
export interface UnfollowResponse {
58+
toast: LiteralUnion<'关注已取消'>
59+
}
5160
}
5261

5362
export namespace Users {

src/types/options.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,18 @@ export interface ListRecommendFeedOption
101101
*/
102102
trigger?: string
103103
}
104+
105+
/**
106+
* @description 关注用户选项
107+
*/
108+
export interface UserFollowOption {
109+
pageName?: number
110+
}
111+
112+
/**
113+
* @description 取消关注用户选项
114+
*/
115+
export interface UserUnfollowOption {
116+
pageName?: number
117+
ref?: string
118+
}

0 commit comments

Comments
 (0)