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 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add method for searching users
- Loading branch information
Showing
6 changed files
with
150 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,46 @@ | ||
import { CountOffsetParams, ListRequestParams, ListResponseData } from './request'; | ||
import { CommonUserDetails } from './user'; | ||
|
||
export interface SearchUsersRequest extends ListRequestParams, CountOffsetParams { | ||
/** The term to search for */ | ||
keyword: string; | ||
|
||
/** The scope of the search - users = 1 */ | ||
type?: number; | ||
} | ||
|
||
export interface SearchUsersResponse 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[]; | ||
|
||
/** Details about the user */ | ||
user_info: CommonUserDetails; | ||
} | ||
|
||
/** | ||
* 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,45 @@ | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { assert } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import TikTokAPI, { | ||
CommonUserDetails, | ||
SearchUsersResponse, | ||
} 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: SearchUsersResponse = { | ||
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); | ||
}); | ||
}); |
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 | ||
} |