Skip to content

Commit d80cab9

Browse files
committed
feat(api): add search topic
1 parent 5210450 commit d80cab9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/api/topics.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { request, toResponse } from '../request'
2+
import type { LiteralUnion } from '../utils'
23
import type { Topics } from '../types/api-responses'
34
import type { PaginationOption } from '../types/options'
45

@@ -40,3 +41,29 @@ export const getTabsSelectedFeed = <T = Topics.GetTabsSelectedFeedResponse>(
4041
},
4142
})
4243
)
44+
45+
/**
46+
* 搜索圈子
47+
* @param keywords 关键词
48+
* @param option 选项
49+
*/
50+
export const search = <T = Topics.SearchResponse>(
51+
keywords: string,
52+
option: PaginationOption<{ skip: number }> & {
53+
/** 类型 */
54+
type?: LiteralUnion<'ALL'>
55+
/** 仅用户发帖时为 true */
56+
onlyUserPostEnabled?: boolean
57+
} = {}
58+
) =>
59+
toResponse<T>(
60+
request.post('1.0/users/topics/search', {
61+
json: {
62+
type: option.type ?? 'ALL',
63+
keywords,
64+
onlyUserPostEnabled: option.onlyUserPostEnabled,
65+
limit: option.limit ?? 20,
66+
loadMoreKey: option.loadMoreKey,
67+
},
68+
})
69+
)

src/types/api-responses.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
SquareFeed,
1313
Story,
1414
TabIcons,
15+
Topic,
1516
User,
1617
} from './entity'
1718

@@ -228,4 +229,18 @@ export namespace Topics {
228229
offset: number
229230
}
230231
}
232+
233+
// 搜索圈子
234+
export interface SearchResponse {
235+
data: Topic[]
236+
count: number
237+
loadMoreKey?: {
238+
skip: number
239+
}
240+
highlightWord: {
241+
words: string[]
242+
singleMaxHighlightTime: number
243+
totalMaxHighlightTime: number
244+
}
245+
}
231246
}

tests/api/topics.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ describe('topics should work', () => {
1919
expect(isSuccess(result)).toBe(true)
2020
expect(result.data.data.length).toBe(limit)
2121
})
22+
23+
it('search should work', async () => {
24+
const result = await api.topics.search('好', { limit })
25+
expect(isSuccess(result)).toBe(true)
26+
expect(result.data.data.length).toBe(limit)
27+
})
2228
})

0 commit comments

Comments
 (0)