Skip to content

Commit ee3b967

Browse files
authored
Merge pull request #391 from Abdullah-Malik/master
feat: get friends/followers objects list V1
2 parents e56d781 + 2c35afc commit ee3b967

File tree

5 files changed

+179
-4
lines changed

5 files changed

+179
-4
lines changed

doc/v1.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ For streaming API, see [Streaming part](./streaming.md).
4141
* [List muted users (IDs)](#ListmutedusersIDs)
4242
* [Get Ids of followers of a user](#GetIdsoffollowersofauser)
4343
* [Get Ids of friends of a user](#GetIdsoffriendsofauser)
44+
* [List of followers of the specified user (objects)](#Getlistoffollowersofauser)
45+
* [List of friends of the specified user (objects)](#Getlistoffriendsofauser)
4446
* [Get sizes of profile banner of a user](#Getsizesofprofilebannerofauser)
4547
* [Get detailed relationship between two users](#Getdetailedrelationshipbetweentwousers)
4648
* [Update relationship between you and other user](#Updaterelationshipbetweenyouandotheruser)
@@ -642,6 +644,56 @@ console.log('First page of friend ids of "WSJ" user:', friends.ids);
642644
console.log('Second page of friend ids of "WSJ" user:', (await friends.next()).ids);
643645
```
644646

647+
### <a name='Getlistoffollowersofauser'></a>List of followers of the specified user (objects)
648+
649+
Get an array of user objects for users following the specified user.
650+
Get to know how [paginators work here](./paginators.md).
651+
652+
To access user IDs from the paginator, use `.users` property.
653+
654+
**Method**: `.userFollowerList()`
655+
656+
**Endpoint**: `followers/list.json`
657+
658+
**Right level**: `Read-only`
659+
660+
**Arguments**:
661+
- `options?: UserFollowerListV1Params`
662+
663+
**Returns**: `UserFollowerListV1Paginator` (containing `string` items)
664+
665+
**Example**
666+
```ts
667+
const followers = await client.v1.userFollowerList({ screen_name: 'WSJ' });
668+
console.log('First page of follower of "WSJ" user:', followers.users);
669+
console.log('Second page of follower of "WSJ" user:', (await followers.next()).users);
670+
```
671+
672+
### <a name='Getlistoffriendsofauser'></a>List of friends of the specified user (objects)
673+
674+
Get an array of user objects for every user the specified user is following (otherwise known as their "friends").
675+
Get to know how [paginators work here](./paginators.md).
676+
677+
To access user IDs from the paginator, use `.users` property.
678+
679+
**Method**: `.userFriendList()`
680+
681+
**Endpoint**: `friends/list.json`
682+
683+
**Right level**: `Read-only`
684+
685+
**Arguments**:
686+
- `options?: UserFriendListV1Params`
687+
688+
**Returns**: `UserFriendListV1Paginator` (containing `string` items)
689+
690+
**Example**
691+
```ts
692+
const friends = await client.v1.userFriendList({ screen_name: 'WSJ' });
693+
console.log('First page of friends of "WSJ" user:', friends.users);
694+
console.log('Second page of friends of "WSJ" user:', (await friends.next()).users);
695+
```
696+
645697
### <a name='Getsizesofprofilebannerofauser'></a>Get sizes of profile banner of a user
646698

647699
**Method**: `.userProfileBannerSizes()`

src/paginators/followers.paginator.v1.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
import { CursoredV1Paginator } from './paginator.v1';
2-
import type { UserFollowerIdsV1Params, UserFollowerIdsV1Result, TwitterResponse } from '../types';
2+
import type { UserFollowerIdsV1Params, UserFollowerIdsV1Result, UserFollowerListV1Params, UserFollowerListV1Result, TwitterResponse, UserV1 } from '../types';
3+
4+
export class UserFollowerListV1Paginator extends CursoredV1Paginator<UserFollowerListV1Result, UserFollowerListV1Params, UserV1> {
5+
protected _endpoint = 'followers/list.json';
6+
7+
protected refreshInstanceFromResult(response: TwitterResponse<UserFollowerListV1Result>, isNextPage: true) {
8+
const result = response.data;
9+
this._rateLimit = response.rateLimit!;
10+
11+
if (isNextPage) {
12+
this._realData.users.push(...result.users);
13+
this._realData.next_cursor = result.next_cursor;
14+
}
15+
}
16+
17+
protected getPageLengthFromRequest(result: TwitterResponse<UserFollowerListV1Result>) {
18+
return result.data.users.length;
19+
}
20+
21+
protected getItemArray() {
22+
return this.users;
23+
}
24+
25+
/**
26+
* Users returned by paginator.
27+
*/
28+
get users() {
29+
return this._realData.users;
30+
}
31+
}
332

433
export class UserFollowerIdsV1Paginator extends CursoredV1Paginator<UserFollowerIdsV1Result, UserFollowerIdsV1Params, string> {
534
protected _endpoint = 'followers/ids.json';

src/paginators/friends.paginator.v1.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
import { CursoredV1Paginator } from './paginator.v1';
2-
import type { UserFollowerIdsV1Params, UserFollowerIdsV1Result, TwitterResponse } from '../types';
2+
import type { UserFollowerIdsV1Params, UserFollowerIdsV1Result, UserFriendListV1Params, UserFriendListV1Result, UserV1, TwitterResponse } from '../types';
3+
4+
export class UserFriendListV1Paginator extends CursoredV1Paginator<UserFriendListV1Result, UserFriendListV1Params, UserV1> {
5+
protected _endpoint = 'friends/list.json';
6+
7+
protected refreshInstanceFromResult(response: TwitterResponse<UserFriendListV1Result>, isNextPage: true) {
8+
const result = response.data;
9+
this._rateLimit = response.rateLimit!;
10+
11+
if (isNextPage) {
12+
this._realData.users.push(...result.users);
13+
this._realData.next_cursor = result.next_cursor;
14+
}
15+
}
16+
17+
protected getPageLengthFromRequest(result: TwitterResponse<UserFriendListV1Result>) {
18+
return result.data.users.length;
19+
}
20+
21+
protected getItemArray() {
22+
return this.users;
23+
}
24+
25+
/**
26+
* Users returned by paginator.
27+
*/
28+
get users() {
29+
return this._realData.users;
30+
}
31+
}
332

433
export class UserFollowersIdsV1Paginator extends CursoredV1Paginator<UserFollowerIdsV1Result, UserFollowerIdsV1Params, string> {
534
protected _endpoint = 'friends/ids.json';

src/types/v1/user.v1.types.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ export interface UserFollowerIdsV1Params extends DoubleEndedIdCursorV1Params {
6666
count?: number;
6767
}
6868

69+
interface UserListV1Params {
70+
screen_name?: string;
71+
user_id?: string;
72+
count?: number;
73+
cursor?: string;
74+
skip_status?: boolean;
75+
include_user_entities: boolean;
76+
}
77+
78+
export type UserFollowerListV1Params = UserListV1Params;
79+
80+
export type UserFriendListV1Params = UserListV1Params;
81+
6982
export interface VerifyCredentialsV1Params {
7083
include_entities?: boolean;
7184
skip_status?: boolean;
@@ -190,6 +203,18 @@ export type UserFollowerIdsV1Result = DoubleEndedIdCursorV1Result;
190203

191204
export type UserFollowingIdsV1Result = DoubleEndedIdCursorV1Result;
192205

206+
interface UserListV1Result {
207+
next_cursor?: string;
208+
next_cursor_str?: string;
209+
previous_cursor?: string;
210+
previous_cursor_str?: string;
211+
users: UserV1[];
212+
}
213+
214+
export type UserFollowerListV1Result = UserListV1Result;
215+
216+
export type UserFriendListV1Result = UserListV1Result;
217+
193218
// GET users/profile_banner
194219
export interface BannerSizeV1 {
195220
h: number;

src/v1/client.v1.read.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import {
3333
UserFollowerIdsV1Result,
3434
UserFollowingsIdsV1Params,
3535
UserFollowingIdsV1Result,
36+
UserFriendListV1Params,
37+
UserFriendListV1Result,
38+
UserFollowerListV1Params,
39+
UserFollowerListV1Result,
3640
UserSearchV1Params,
3741
AccountSettingsV1,
3842
ProfileBannerSizeV1,
@@ -64,8 +68,8 @@ import {
6468
} from '../types';
6569
import { HomeTimelineV1Paginator, ListTimelineV1Paginator, MentionTimelineV1Paginator, UserFavoritesV1Paginator, UserTimelineV1Paginator } from '../paginators/tweet.paginator.v1';
6670
import { MuteUserIdsV1Paginator, MuteUserListV1Paginator } from '../paginators/mutes.paginator.v1';
67-
import { UserFollowerIdsV1Paginator } from '../paginators/followers.paginator.v1';
68-
import { UserFollowersIdsV1Paginator } from '../paginators/friends.paginator.v1';
71+
import { UserFollowerIdsV1Paginator, UserFollowerListV1Paginator } from '../paginators/followers.paginator.v1';
72+
import { UserFollowersIdsV1Paginator, UserFriendListV1Paginator } from '../paginators/friends.paginator.v1';
6973
import { FriendshipsIncomingV1Paginator, FriendshipsOutgoingV1Paginator, UserSearchV1Paginator } from '../paginators/user.paginator.v1';
7074
import { ListMembershipsV1Paginator, ListMembersV1Paginator, ListOwnershipsV1Paginator, ListSubscribersV1Paginator, ListSubscriptionsV1Paginator } from '../paginators/list.paginator.v1';
7175
import TweetStream from '../stream/TweetStream';
@@ -307,6 +311,42 @@ export default class TwitterApiv1ReadOnly extends TwitterApiSubClient {
307311
});
308312
}
309313

314+
/**
315+
* Returns an array of user objects of friends of the specified user.
316+
* https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/get-friends-list
317+
*/
318+
public async userFriendList(options: Partial<UserFriendListV1Params> = {}) {
319+
const queryParams: Partial<UserFriendListV1Params> = {
320+
...options,
321+
};
322+
const initialRq = await this.get<UserFriendListV1Result>('friends/list.json', queryParams, { fullResponse: true });
323+
324+
return new UserFriendListV1Paginator({
325+
realData: initialRq.data,
326+
rateLimit: initialRq.rateLimit!,
327+
instance: this,
328+
queryParams,
329+
});
330+
}
331+
332+
/**
333+
* Returns an array of user objects of followers of the specified user.
334+
* https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/get-followers-list
335+
*/
336+
public async userFollowerList(options: Partial<UserFollowerListV1Params> = {}) {
337+
const queryParams: Partial<UserFollowerListV1Params> = {
338+
...options,
339+
};
340+
const initialRq = await this.get<UserFollowerListV1Result>('followers/list.json', queryParams, { fullResponse: true });
341+
342+
return new UserFollowerListV1Paginator({
343+
realData: initialRq.data,
344+
rateLimit: initialRq.rateLimit!,
345+
instance: this,
346+
queryParams,
347+
});
348+
}
349+
310350
/**
311351
* Returns an array of numeric user ids of followers of the specified user.
312352
* https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/get-followers-ids

0 commit comments

Comments
 (0)