This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add methods for searching users and hashtags (#24)
- Loading branch information
Showing
7 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ChallengeInfo } from './category'; | ||
import { CountOffsetParams, ListRequestParams, ListResponseData } from './request'; | ||
import { CommonUserDetails } from './user'; | ||
|
||
export interface SearchRequest extends ListRequestParams, CountOffsetParams { | ||
/** The term to search for */ | ||
keyword: string; | ||
} | ||
|
||
export interface UserSearchRequest extends SearchRequest { | ||
/** The scope of the search - users = 1 */ | ||
type?: number; | ||
} | ||
|
||
export interface UserSearchResponse extends ListResponseData, CountOffsetParams { | ||
/** A list of users that match the search term */ | ||
user_list: UserSearchResult[]; | ||
|
||
/** The scope of the search - users = 1 */ | ||
type: number; | ||
} | ||
|
||
export interface UserSearchResult { | ||
/** If the user's nickname contains the search term, this array contains the location of the term */ | ||
position: SubstringPosition[]; | ||
|
||
/** If the user's username (unique_id) contains the search term, this array contains the location of the term */ | ||
uniqid_position: SubstringPosition[]; | ||
|
||
/** Information about the user */ | ||
user_info: CommonUserDetails; | ||
} | ||
|
||
export interface HashtagSearchResponse extends ListResponseData, CountOffsetParams { | ||
/** A list of hashtags that match the search term */ | ||
challenge_list: HashtagSearchResult[]; | ||
|
||
/** True if a challenge matches the search term */ | ||
is_match: boolean; | ||
|
||
/** 1 if the search term is disabled */ | ||
keyword_disabled: 0 | 1; | ||
} | ||
|
||
export interface HashtagSearchResult { | ||
/** Information about the hashtag */ | ||
challenge_info: ChallengeInfo; | ||
|
||
/** If the hashtag contains the search term, this array contains the location of the term */ | ||
position: SubstringPosition[]; | ||
} | ||
|
||
/** | ||
* Represents the location of a substring in a string. | ||
* | ||
* e.g. For the string "The quick brown fox", the substring "quick" would be: | ||
* { | ||
* begin: 4, | ||
* end: 6 | ||
* } | ||
*/ | ||
export interface SubstringPosition { | ||
/** The start index of the substring */ | ||
begin: number; | ||
|
||
/** The end index of the substring */ | ||
end: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { assert } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import TikTokAPI, { | ||
ChallengeInfo, | ||
CommonUserDetails, | ||
HashtagSearchResponse, | ||
UserSearchResponse, | ||
} from '../src'; | ||
import { | ||
loadTestData, | ||
mockConfig, | ||
mockParams, | ||
} from './util'; | ||
|
||
describe('#searchUsers()', () => { | ||
it('a successful response should match the interface', async () => { | ||
const api = new TikTokAPI(mockParams, mockConfig); | ||
const mock = new MockAdapter(api.request); | ||
mock | ||
.onGet(new RegExp('aweme/v1/discover/search/\?.*')) | ||
.reply(200, loadTestData('searchUsers.json'), {}); | ||
|
||
const res = await api.searchUsers({ keyword: 'example', count: 10, cursor: 0 }); | ||
const expected: UserSearchResponse = { | ||
extra: { | ||
now: 1000000000000, | ||
}, | ||
user_list: [ | ||
{ | ||
position: [{ | ||
begin: 0, | ||
end: 6, | ||
}], | ||
uniqid_position: [], | ||
user_info: {} as CommonUserDetails, | ||
}, | ||
], | ||
type: 1, | ||
cursor: 1, | ||
has_more: 1, | ||
status_code: 0, | ||
}; | ||
assert.deepStrictEqual(res.data, expected); | ||
}); | ||
}); | ||
|
||
describe('#searchHashtags()', () => { | ||
it('a successful response should match the interface', async () => { | ||
const api = new TikTokAPI(mockParams, mockConfig); | ||
const mock = new MockAdapter(api.request); | ||
mock | ||
.onGet(new RegExp('aweme/v1/challenge/search/\?.*')) | ||
.reply(200, loadTestData('searchHashtags.json'), {}); | ||
|
||
const res = await api.searchHashtags({ keyword: 'example', count: 10, cursor: 0 }); | ||
const expected: HashtagSearchResponse = { | ||
extra: { | ||
now: 1000000000000, | ||
}, | ||
challenge_list: [ | ||
{ | ||
challenge_info: {} as ChallengeInfo, | ||
position: [{ | ||
begin: 0, | ||
end: 6, | ||
}], | ||
}, | ||
], | ||
is_match: true, | ||
keyword_disabled: 0, | ||
cursor: 1, | ||
has_more: 1, | ||
status_code: 0, | ||
}; | ||
assert.deepStrictEqual(res.data, expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extra": { | ||
"now": 1000000000000 | ||
}, | ||
"challenge_list": [ | ||
{ | ||
"challenge_info": {}, | ||
"position": [{ | ||
"begin": 0, | ||
"end": 6 | ||
}] | ||
} | ||
], | ||
"is_match": true, | ||
"keyword_disabled": 0, | ||
"cursor": 1, | ||
"has_more": 1, | ||
"status_code": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extra": { | ||
"now": 1000000000000 | ||
}, | ||
"user_list": [ | ||
{ | ||
"position": [{ | ||
"begin": 0, | ||
"end": 6 | ||
}], | ||
"uniqid_position": [], | ||
"user_info": {} | ||
} | ||
], | ||
"type": 1, | ||
"cursor": 1, | ||
"has_more": 1, | ||
"status_code": 0 | ||
} |