Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
feat: add method for searching users
Browse files Browse the repository at this point in the history
  • Loading branch information
szdc committed Aug 22, 2018
1 parent de88ef3 commit 6dacc82
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ api.getUser('<user_id>')

See the [user types](src/types/user.d.ts) for the response data.

#### .searchUsers(params)

Searches for users.

```javascript
api.searchUsers({
keyword: 'example',
count: 10,
cursor: 0,
})
.then(res => console.log(res.data.user_list))
.catch(console.log);

// Outputs:
// [{ user_info: {...}, position: [], uniqposition: [] }, ...]

```

See the [search types](src/types/search.d.ts) for the complete request/response objects.

#### .listPosts(params)

Lists a user's posts.
Expand Down
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
PostCommentRequest,
PostCommentResponse,
RequiredUserDefinedRequestParams,
SearchUsersRequest,
SearchUsersResponse,
StaticRequestParams,
Tag,
TikTokAPIConfig,
Expand Down Expand Up @@ -121,6 +123,23 @@ export default class TikTokAPI {
getUser = (userId: string) =>
this.request.get<UserProfileResponse | BaseResponseData>('aweme/v1/user/', { params: { user_id: userId } })

/**
* Searches for users.
*
* @param params
* @returns {AxiosPromise<SearchUsersResponse | BaseResponseData>}
*/
searchUsers = (params: SearchUsersRequest) =>
this.request.get<SearchUsersResponse | BaseResponseData>('aweme/v1/discover/search/', {
params: withDefaultListParams(<SearchUsersRequest>{
count: 10,
cursor: 0,
keyword: '',
type: 1,
...params,
}),
})

/**
* Lists a user's posts.
*
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './login';
export * from './music';
export * from './post';
export * from './request';
export * from './search';
export * from './tag';
export * from './user';
export * from './video';
46 changes: 46 additions & 0 deletions src/types/search.d.ts
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;
}
45 changes: 45 additions & 0 deletions test/search.spec.ts
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);
});
});
19 changes: 19 additions & 0 deletions test/testdata/searchUsers.json
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
}

0 comments on commit 6dacc82

Please sign in to comment.