Skip to content

Commit 53438f3

Browse files
authored
Merge pull request #169 from PLhery/feat/pagination-v2-likes-rt
feat: Pagination for tweet liked by / tweet retweeted by
2 parents 3bb0f00 + f33b973 commit 53438f3

File tree

5 files changed

+86
-11
lines changed

5 files changed

+86
-11
lines changed

doc/v2.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,20 @@ const tweets = await client.v2.tweets(['20', '141']);
343343

344344
**Arguments**:
345345
- `tweetId: string`
346-
- `options?: UsersV2Params`
346+
- `options?: TweetRetweetedOrLikedByV2Params`
347347

348-
**Returns**: `TweetV2LikedByResult`
348+
**Returns**: `TweetV2LikedByResult` or `TweetLikingUsersV2Paginator` (if `options.asPaginator`)
349349

350350
**Example**
351351
```ts
352352
const users = await client.v2.tweetLikedBy('20');
353353
console.log(users.data[0].id);
354+
355+
const usersPaginated = await client.v2.tweetLikedBy('20', { asPaginator: true });
356+
357+
for await (const user of usersPaginated) {
358+
console.log(user.id);
359+
}
354360
```
355361

356362
### <a name='Likeatweet'></a>Like a tweet
@@ -445,14 +451,20 @@ console.log(allTweetsWithNode.data[0].tweet_count);
445451

446452
**Arguments**:
447453
- `tweetId: string`
448-
- `options?: UsersV2Params`
454+
- `options?: TweetRetweetedOrLikedByV2Params`
449455

450-
**Returns**: `TweetV2RetweetedByResult`
456+
**Returns**: `TweetV2RetweetedByResult` or `TweetRetweetersUsersV2Paginator` (if `options.asPaginator`)
451457

452458
**Example**
453459
```ts
454460
const users = await client.v2.tweetRetweetedBy('20');
455461
console.log(users.data[0].id);
462+
463+
const usersPaginated = await client.v2.tweetRetweetedBy('20', { asPaginator: true });
464+
465+
for await (const user of usersPaginated) {
466+
console.log(user.id);
467+
}
456468
```
457469

458470
### <a name='Retweetatweet'></a>Retweet a tweet

src/paginators/user.paginator.v2.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ export class UserListMembersV2Paginator extends UserTimelineV2Paginator<UserV2Ti
4242
export class UserListFollowersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, { id: string }> {
4343
protected _endpoint = 'lists/:id/followers';
4444
}
45+
46+
export class TweetLikingUsersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, { id: string }> {
47+
protected _endpoint = 'tweets/:id/liking_users';
48+
}
49+
50+
export class TweetRetweetersUsersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, { id: string }> {
51+
protected _endpoint = 'tweets/:id/retweeted_by';
52+
}

src/types/v2/tweet.v2.types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,17 @@ export type TweetV2LikeResult = DataV2<{
130130
liked: boolean;
131131
}>;
132132

133-
export type TweetV2LikedByResult = DataAndIncludeV2<UserV2[], ApiV2Includes>;
133+
export type TweetV2LikedByResult = DataMetaAndIncludeV2<UserV2[], {
134+
result_count: number;
135+
next_token?: string;
136+
previous_token?: string;
137+
}, ApiV2Includes>;
134138

135139
/// -- Retweets
136140

137141
export type TweetV2RetweetResult = DataV2<{ retweeted: boolean }>;
138142

139-
export type TweetV2RetweetedByResult = DataMetaAndIncludeV2<UserV2[], { result_count: number }, ApiV2Includes>;
143+
export type TweetV2RetweetedByResult = TweetV2LikedByResult;
140144

141145
/// Tweets
142146

src/types/v2/user.v2.types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ export interface UserV2TimelineParams {
2929
pagination_token?: string;
3030
}
3131

32+
export interface TweetRetweetedOrLikedByV2Params extends Partial<UsersV2Params> {
33+
asPaginator?: boolean;
34+
}
35+
36+
export interface TweetRetweetedOrLikedByV2ParamsWithoutPaginator extends TweetRetweetedOrLikedByV2Params {
37+
asPaginator?: false;
38+
}
39+
40+
export interface TweetRetweetedOrLikedByV2ParamsWithPaginator extends TweetRetweetedOrLikedByV2Params {
41+
asPaginator: true;
42+
}
43+
3244
export interface FollowersV2Params extends UserV2TimelineParams {
3345
asPaginator?: boolean;
3446
}

src/v2/client.v2.read.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ import {
4646
ListGetV2Result,
4747
GetListTimelineV2Params,
4848
ListTimelineV2Result,
49+
TweetRetweetedOrLikedByV2Params,
50+
TweetRetweetedOrLikedByV2ParamsWithPaginator,
51+
TweetRetweetedOrLikedByV2ParamsWithoutPaginator,
4952
} from '../types';
5053
import {
5154
TweetSearchAllV2Paginator,
@@ -59,7 +62,7 @@ import {
5962
TweetV2ListTweetsPaginator,
6063
} from '../paginators';
6164
import TwitterApiv2LabsReadOnly from '../v2-labs/client.v2.labs.read';
62-
import { UserBlockingUsersV2Paginator, UserFollowersV2Paginator, UserFollowingV2Paginator, UserListFollowersV2Paginator, UserListMembersV2Paginator, UserMutingUsersV2Paginator } from '../paginators/user.paginator.v2';
65+
import { TweetLikingUsersV2Paginator, TweetRetweetersUsersV2Paginator, UserBlockingUsersV2Paginator, UserFollowersV2Paginator, UserFollowingV2Paginator, UserListFollowersV2Paginator, UserListMembersV2Paginator, UserMutingUsersV2Paginator } from '../paginators/user.paginator.v2';
6366
import { isTweetStreamV2ErrorPayload } from '../helpers';
6467
import TweetStream from '../stream/TweetStream';
6568
import { PromiseOrType } from '../types/shared.types';
@@ -164,16 +167,52 @@ export default class TwitterApiv2ReadOnly extends TwitterApiSubClient {
164167
* Allows you to get information about who has Retweeted a Tweet.
165168
* https://developer.twitter.com/en/docs/twitter-api/tweets/retweets/api-reference/get-tweets-id-retweeted_by
166169
*/
167-
public tweetRetweetedBy(tweetId: string, options: Partial<UsersV2Params> = {}) {
168-
return this.get<TweetV2RetweetedByResult>('tweets/:id/retweeted_by', options, { params: { id: tweetId } });
170+
public tweetRetweetedBy(tweetId: string, options?: Partial<TweetRetweetedOrLikedByV2ParamsWithoutPaginator>): Promise<TweetV2RetweetedByResult>;
171+
public tweetRetweetedBy(tweetId: string, options: TweetRetweetedOrLikedByV2ParamsWithPaginator): Promise<TweetRetweetersUsersV2Paginator>;
172+
public async tweetRetweetedBy(tweetId: string, options: TweetRetweetedOrLikedByV2Params = {}) {
173+
const { asPaginator, ...parameters } = options;
174+
const initialRq = await this.get<TweetV2RetweetedByResult>('tweets/:id/retweeted_by', parameters as any, {
175+
fullResponse: true,
176+
params: { id: tweetId },
177+
});
178+
179+
if (!asPaginator) {
180+
return initialRq.data;
181+
}
182+
183+
return new TweetRetweetersUsersV2Paginator({
184+
realData: initialRq.data,
185+
rateLimit: initialRq.rateLimit!,
186+
instance: this,
187+
queryParams: parameters,
188+
sharedParams: { id: tweetId },
189+
});
169190
}
170191

171192
/**
172193
* Allows you to get information about who has Liked a Tweet.
173194
* https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-tweets-id-liking_users
174195
*/
175-
public tweetLikedBy(tweetId: string, options: Partial<UsersV2Params> = {}) {
176-
return this.get<TweetV2LikedByResult>('tweets/:id/liking_users', options, { params: { id: tweetId } });
196+
public tweetLikedBy(tweetId: string, options?: Partial<TweetRetweetedOrLikedByV2ParamsWithoutPaginator>): Promise<TweetV2LikedByResult>;
197+
public tweetLikedBy(tweetId: string, options: TweetRetweetedOrLikedByV2ParamsWithPaginator): Promise<TweetLikingUsersV2Paginator>;
198+
public async tweetLikedBy(tweetId: string, options: TweetRetweetedOrLikedByV2Params = {}) {
199+
const { asPaginator, ...parameters } = options;
200+
const initialRq = await this.get<TweetV2LikedByResult>('tweets/:id/liking_users', parameters as any, {
201+
fullResponse: true,
202+
params: { id: tweetId },
203+
});
204+
205+
if (!asPaginator) {
206+
return initialRq.data;
207+
}
208+
209+
return new TweetLikingUsersV2Paginator({
210+
realData: initialRq.data,
211+
rateLimit: initialRq.rateLimit!,
212+
instance: this,
213+
queryParams: parameters,
214+
sharedParams: { id: tweetId },
215+
});
177216
}
178217

179218
/**

0 commit comments

Comments
 (0)