Skip to content

Commit de80516

Browse files
committed
feat(jike-client): add createPost
1 parent 052c921 commit de80516

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

src/client/client.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { resolveAreaCode } from './utils/user'
77
import { JikeUser } from './user'
88
import { fetchPaginated } from './utils/paginate'
99
import { AuthorizationError } from './errors/AuthorizationError'
10+
import { JikePost } from './post'
11+
import type { CreatePostOption, PostType } from '../types/options'
1012
import type { Notification } from '../types/entity'
1113
import type { BeforeRetryState } from 'ky/distribution/types/hooks'
1214
import type { PaginatedFetcher, PaginatedOption } from './utils/paginate'
@@ -192,6 +194,27 @@ export class JikeClient {
192194
)
193195
}
194196

197+
/**
198+
*
199+
* @param type 动态类型,原帖 或 转发
200+
* @param content 内容
201+
* @param options 发送动态选项
202+
*/
203+
async createPost(
204+
type: PostType,
205+
content: string,
206+
options?: CreatePostOption
207+
) {
208+
const result = await this.#client.posts.create(type, content, options)
209+
if (!isSuccess(result)) throwRequestFailureError(result, '发送动态')
210+
return {
211+
/** 动态 */
212+
post: new JikePost(this, type, result.data.data.id, result.data.data),
213+
/** 提示文本 */
214+
toast: result.data.toast,
215+
}
216+
}
217+
195218
/**
196219
* 刷新 access token
197220
*/
@@ -217,14 +240,16 @@ export class JikeClient {
217240
async toJSON(): Promise<JikeClientJSON> {
218241
// eslint-disable-next-line @typescript-eslint/no-unused-vars
219242
const { beforeRetry, ...config } = this.#config
220-
const profile = await this.getSelf().queryProfile()
243+
const profile = await this.getSelf()
244+
.queryProfile()
245+
.catch(() => undefined)
221246
return {
222247
...config,
223248
accessToken: this.accessToken,
224249
refreshToken: this.refreshToken,
225-
userId: profile.user.id,
226-
username: profile.user.username,
227-
screenName: profile.user.screenName,
250+
userId: profile?.user.id ?? '',
251+
username: profile?.user.username ?? '',
252+
screenName: profile?.user.screenName ?? '',
228253
}
229254
}
230255

src/types/api-responses.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111

1212
export namespace Posts {
1313
export interface CreateResponse {
14+
/** 提示文本 */
1415
toast: string
1516
data: PostDetail
1617
}

tests/jike-client/constructor.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { JikeClient } from '../../src'
3+
import { config, refreshToken } from '../config'
4+
5+
describe('constructor', () => {
6+
const client = new JikeClient({ ...config, refreshToken })
7+
8+
it('toJSON should work', async () => {
9+
const json = await client.toJSON()
10+
const profile = await client.getSelf().queryProfile()
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12+
const { beforeRetry, ...cfg } = client.config
13+
expect(json).toEqual({
14+
...cfg,
15+
refreshToken: client.refreshToken,
16+
userId: profile.user.id,
17+
username: profile.user.username,
18+
screenName: profile.user.screenName,
19+
})
20+
})
21+
22+
it('serialize should work', async () => {
23+
const data = await client.serialize()
24+
const profile = await client.getSelf().queryProfile()
25+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
26+
const { beforeRetry, ...cfg } = client.config
27+
expect(data).toEqual(
28+
JSON.stringify({
29+
...cfg,
30+
refreshToken: client.refreshToken,
31+
userId: profile.user.id,
32+
username: profile.user.username,
33+
screenName: profile.user.screenName,
34+
})
35+
)
36+
})
37+
38+
it('fromJSON should work', async () => {
39+
const client2 = JikeClient.fromJSON(await client.toJSON())
40+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
41+
const { beforeRetry: _, ...clienConfig } = client.config
42+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
43+
const { beforeRetry, ...clien2Config } = client2.config
44+
expect(clien2Config).toEqual(clienConfig)
45+
})
46+
47+
it('serialize should work', async () => {
48+
const client2 = JikeClient.fromJSON(JSON.parse(await client.serialize()))
49+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
50+
const { beforeRetry: _, ...clienConfig } = client.config
51+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
52+
const { beforeRetry, ...clien2Config } = client2.config
53+
expect(clien2Config).toEqual(clienConfig)
54+
})
55+
})

tests/jike-client/post.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { JikeClient } from '../../src'
3+
import { PostType } from '../../src/types/options'
4+
import { config, refreshToken } from '../config'
5+
6+
describe('post', () => {
7+
const client = new JikeClient({ ...config, refreshToken })
8+
9+
it('createPost should work', async () => {
10+
const { post, toast } = await client.createPost(
11+
PostType.ORIGINAL,
12+
'👋 Hello World!\n\n ✨ From Jike SDK. \n\n Made with ❤️.'
13+
)
14+
15+
expect(toast).to.be.a('string')
16+
expect(post.id).to.be.a('string')
17+
})
18+
})

0 commit comments

Comments
 (0)