Add files via upload

This commit is contained in:
shahramdj
2022-10-07 15:45:45 -04:00
committed by GitHub
parent 8ddcc312b7
commit 91c72d107e
78 changed files with 6467 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
import { TwitterRateLimit, TwitterResponse } from '../types';
import TwitterApiSubClient from '../client.subclient';
export interface ITwitterPaginatorArgs<TApiResult, TApiParams, TParams> {
realData: TApiResult;
rateLimit: TwitterRateLimit;
instance: TwitterApiSubClient;
queryParams: Partial<TApiParams>;
sharedParams?: TParams;
}
/** TwitterPaginator: able to get consume data from initial request, then fetch next data sequentially. */
export declare abstract class TwitterPaginator<TApiResult, TApiParams extends object, TItem, TParams = any> {
protected _realData: TApiResult;
protected _rateLimit: TwitterRateLimit;
protected _instance: TwitterApiSubClient;
protected _queryParams: Partial<TApiParams>;
protected _maxResultsWhenFetchLast: number;
/** informations unrelated to response data/query params that will be shared between paginator instances */
protected _sharedParams: TParams;
protected abstract _endpoint: string;
constructor({ realData, rateLimit, instance, queryParams, sharedParams }: ITwitterPaginatorArgs<TApiResult, TApiParams, TParams>);
protected get _isRateLimitOk(): boolean;
protected makeRequest(queryParams: Partial<TApiParams>): Promise<TwitterResponse<TApiResult>>;
protected makeNewInstanceFromResult(result: TwitterResponse<TApiResult>, queryParams: Partial<TApiParams>): this;
protected getEndpoint(): string;
protected injectQueryParams(maxResults?: number): {
max_results?: number | undefined;
} & Partial<TApiParams>;
protected abstract refreshInstanceFromResult(result: TwitterResponse<TApiResult>, isNextPage: boolean): any;
protected abstract getNextQueryParams(maxResults?: number): Partial<TApiParams>;
protected abstract getPageLengthFromRequest(result: TwitterResponse<TApiResult>): number;
protected abstract isFetchLastOver(result: TwitterResponse<TApiResult>): boolean;
protected abstract canFetchNextPage(result: TApiResult): boolean;
protected abstract getItemArray(): TItem[];
/**
* Next page.
*/
next(maxResults?: number): Promise<this>;
/**
* Next page, but store it in current instance.
*/
fetchNext(maxResults?: number): Promise<this>;
/**
* Fetch up to {count} items after current page,
* as long as rate limit is not hit and Twitter has some results
*/
fetchLast(count?: number): Promise<this>;
get rateLimit(): {
limit: number;
reset: number;
remaining: number;
};
/** Get raw data returned by Twitter API. */
get data(): TApiResult;
get done(): boolean;
/**
* Iterate over currently fetched items.
*/
[Symbol.iterator](): Generator<TItem, void, undefined>;
/**
* Iterate over items "undefinitely" (until rate limit is hit / they're no more items available)
* This will **mutate the current instance** and fill data, metas, etc. inside this instance.
*
* If you need to handle concurrent requests, or you need to rely on immutability, please use `.fetchAndIterate()` instead.
*/
[Symbol.asyncIterator](): AsyncGenerator<TItem, void, undefined>;
/**
* Iterate over items "undefinitely" without modifying the current instance (until rate limit is hit / they're no more items available)
*
* This will **NOT** mutate the current instance, meaning that current instance will not inherit from `includes` and `meta` (v2 API only).
* Use `Symbol.asyncIterator` (`for-await of`) to directly access items with current instance mutation.
*/
fetchAndIterate(): AsyncGenerator<[TItem, this], void, undefined>;
}
/** PreviousableTwitterPaginator: a TwitterPaginator able to get consume data from both side, next and previous. */
export declare abstract class PreviousableTwitterPaginator<TApiResult, TApiParams extends object, TItem, TParams = any> extends TwitterPaginator<TApiResult, TApiParams, TItem, TParams> {
protected abstract getPreviousQueryParams(maxResults?: number): Partial<TApiParams>;
/**
* Previous page (new tweets)
*/
previous(maxResults?: number): Promise<this>;
/**
* Previous page, but in current instance.
*/
fetchPrevious(maxResults?: number): Promise<this>;
}
export default TwitterPaginator;

View File

@@ -0,0 +1,173 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreviousableTwitterPaginator = exports.TwitterPaginator = void 0;
/** TwitterPaginator: able to get consume data from initial request, then fetch next data sequentially. */
class TwitterPaginator {
// noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected
constructor({ realData, rateLimit, instance, queryParams, sharedParams }) {
this._maxResultsWhenFetchLast = 100;
this._realData = realData;
this._rateLimit = rateLimit;
this._instance = instance;
this._queryParams = queryParams;
this._sharedParams = sharedParams;
}
get _isRateLimitOk() {
if (!this._rateLimit) {
return true;
}
const resetDate = this._rateLimit.reset * 1000;
if (resetDate < Date.now()) {
return true;
}
return this._rateLimit.remaining > 0;
}
makeRequest(queryParams) {
return this._instance.get(this.getEndpoint(), queryParams, { fullResponse: true, params: this._sharedParams });
}
makeNewInstanceFromResult(result, queryParams) {
// Construct a subclass
return new this.constructor({
realData: result.data,
rateLimit: result.rateLimit,
instance: this._instance,
queryParams,
sharedParams: this._sharedParams,
});
}
getEndpoint() {
return this._endpoint;
}
injectQueryParams(maxResults) {
return {
...(maxResults ? { max_results: maxResults } : {}),
...this._queryParams,
};
}
/* ---------------------- */
/* Real paginator methods */
/* ---------------------- */
/**
* Next page.
*/
async next(maxResults) {
const queryParams = this.getNextQueryParams(maxResults);
const result = await this.makeRequest(queryParams);
return this.makeNewInstanceFromResult(result, queryParams);
}
/**
* Next page, but store it in current instance.
*/
async fetchNext(maxResults) {
const queryParams = this.getNextQueryParams(maxResults);
const result = await this.makeRequest(queryParams);
// Await in case of async sub-methods
await this.refreshInstanceFromResult(result, true);
return this;
}
/**
* Fetch up to {count} items after current page,
* as long as rate limit is not hit and Twitter has some results
*/
async fetchLast(count = Infinity) {
let queryParams = this.getNextQueryParams(this._maxResultsWhenFetchLast);
let resultCount = 0;
// Break at rate limit limit
while (resultCount < count && this._isRateLimitOk) {
const response = await this.makeRequest(queryParams);
await this.refreshInstanceFromResult(response, true);
resultCount += this.getPageLengthFromRequest(response);
if (this.isFetchLastOver(response)) {
break;
}
queryParams = this.getNextQueryParams(this._maxResultsWhenFetchLast);
}
return this;
}
get rateLimit() {
var _a;
return { ...(_a = this._rateLimit) !== null && _a !== void 0 ? _a : {} };
}
/** Get raw data returned by Twitter API. */
get data() {
return this._realData;
}
get done() {
return !this.canFetchNextPage(this._realData);
}
/**
* Iterate over currently fetched items.
*/
*[Symbol.iterator]() {
yield* this.getItemArray();
}
/**
* Iterate over items "undefinitely" (until rate limit is hit / they're no more items available)
* This will **mutate the current instance** and fill data, metas, etc. inside this instance.
*
* If you need to handle concurrent requests, or you need to rely on immutability, please use `.fetchAndIterate()` instead.
*/
async *[Symbol.asyncIterator]() {
yield* this.getItemArray();
// eslint-disable-next-line @typescript-eslint/no-this-alias
let paginator = this;
let canFetchNextPage = this.canFetchNextPage(this._realData);
while (canFetchNextPage && this._isRateLimitOk && paginator.getItemArray().length > 0) {
const next = await paginator.next(this._maxResultsWhenFetchLast);
// Store data into current instance [needed to access includes and meta]
this.refreshInstanceFromResult({ data: next._realData, headers: {}, rateLimit: next._rateLimit }, true);
canFetchNextPage = this.canFetchNextPage(next._realData);
const items = next.getItemArray();
yield* items;
paginator = next;
}
}
/**
* Iterate over items "undefinitely" without modifying the current instance (until rate limit is hit / they're no more items available)
*
* This will **NOT** mutate the current instance, meaning that current instance will not inherit from `includes` and `meta` (v2 API only).
* Use `Symbol.asyncIterator` (`for-await of`) to directly access items with current instance mutation.
*/
async *fetchAndIterate() {
for (const item of this.getItemArray()) {
yield [item, this];
}
// eslint-disable-next-line @typescript-eslint/no-this-alias
let paginator = this;
let canFetchNextPage = this.canFetchNextPage(this._realData);
while (canFetchNextPage && this._isRateLimitOk && paginator.getItemArray().length > 0) {
const next = await paginator.next(this._maxResultsWhenFetchLast);
// Store data into current instance [needed to access includes and meta]
this.refreshInstanceFromResult({ data: next._realData, headers: {}, rateLimit: next._rateLimit }, true);
canFetchNextPage = this.canFetchNextPage(next._realData);
for (const item of next.getItemArray()) {
yield [item, next];
}
this._rateLimit = next._rateLimit;
paginator = next;
}
}
}
exports.TwitterPaginator = TwitterPaginator;
/** PreviousableTwitterPaginator: a TwitterPaginator able to get consume data from both side, next and previous. */
class PreviousableTwitterPaginator extends TwitterPaginator {
/**
* Previous page (new tweets)
*/
async previous(maxResults) {
const queryParams = this.getPreviousQueryParams(maxResults);
const result = await this.makeRequest(queryParams);
return this.makeNewInstanceFromResult(result, queryParams);
}
/**
* Previous page, but in current instance.
*/
async fetchPrevious(maxResults) {
const queryParams = this.getPreviousQueryParams(maxResults);
const result = await this.makeRequest(queryParams);
await this.refreshInstanceFromResult(result, false);
return this;
}
}
exports.PreviousableTwitterPaginator = PreviousableTwitterPaginator;
exports.default = TwitterPaginator;

View File

@@ -0,0 +1,19 @@
import type { GetDmListV1Args, ReceivedDMEventsV1, TReceivedDMEvent, TwitterResponse, ReceivedWelcomeDMCreateEventV1, WelcomeDirectMessageListV1Result } from '../types';
import { CursoredV1Paginator } from './paginator.v1';
export declare class DmEventsV1Paginator extends CursoredV1Paginator<ReceivedDMEventsV1, GetDmListV1Args, TReceivedDMEvent> {
protected _endpoint: string;
protected refreshInstanceFromResult(response: TwitterResponse<ReceivedDMEventsV1>, isNextPage: true): void;
protected getPageLengthFromRequest(result: TwitterResponse<ReceivedDMEventsV1>): number;
protected getItemArray(): import("../types").DirectMessageCreateV1[];
/**
* Events returned by paginator.
*/
get events(): import("../types").DirectMessageCreateV1[];
}
export declare class WelcomeDmV1Paginator extends CursoredV1Paginator<WelcomeDirectMessageListV1Result, GetDmListV1Args, ReceivedWelcomeDMCreateEventV1> {
protected _endpoint: string;
protected refreshInstanceFromResult(response: TwitterResponse<WelcomeDirectMessageListV1Result>, isNextPage: true): void;
protected getPageLengthFromRequest(result: TwitterResponse<WelcomeDirectMessageListV1Result>): number;
protected getItemArray(): ReceivedWelcomeDMCreateEventV1[];
get welcomeMessages(): ReceivedWelcomeDMCreateEventV1[];
}

View File

@@ -0,0 +1,55 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WelcomeDmV1Paginator = exports.DmEventsV1Paginator = void 0;
const paginator_v1_1 = require("./paginator.v1");
class DmEventsV1Paginator extends paginator_v1_1.CursoredV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'direct_messages/events/list.json';
}
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.events.push(...result.events);
this._realData.next_cursor = result.next_cursor;
}
}
getPageLengthFromRequest(result) {
return result.data.events.length;
}
getItemArray() {
return this.events;
}
/**
* Events returned by paginator.
*/
get events() {
return this._realData.events;
}
}
exports.DmEventsV1Paginator = DmEventsV1Paginator;
class WelcomeDmV1Paginator extends paginator_v1_1.CursoredV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'direct_messages/welcome_messages/list.json';
}
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.welcome_messages.push(...result.welcome_messages);
this._realData.next_cursor = result.next_cursor;
}
}
getPageLengthFromRequest(result) {
return result.data.welcome_messages.length;
}
getItemArray() {
return this.welcomeMessages;
}
get welcomeMessages() {
return this._realData.welcome_messages;
}
}
exports.WelcomeDmV1Paginator = WelcomeDmV1Paginator;

View File

@@ -0,0 +1,9 @@
export * from './tweet.paginator.v2';
export * from './TwitterPaginator';
export * from './dm.paginator.v1';
export * from './mutes.paginator.v1';
export * from './tweet.paginator.v1';
export * from './user.paginator.v1';
export * from './user.paginator.v2';
export * from './list.paginator.v1';
export * from './list.paginator.v2';

View File

@@ -0,0 +1,25 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./tweet.paginator.v2"), exports);
__exportStar(require("./TwitterPaginator"), exports);
__exportStar(require("./dm.paginator.v1"), exports);
__exportStar(require("./mutes.paginator.v1"), exports);
__exportStar(require("./tweet.paginator.v1"), exports);
__exportStar(require("./user.paginator.v1"), exports);
__exportStar(require("./user.paginator.v2"), exports);
__exportStar(require("./list.paginator.v1"), exports);
__exportStar(require("./list.paginator.v2"), exports);

View File

@@ -0,0 +1,36 @@
import { DoubleEndedListsCursorV1Result, DoubleEndedUsersCursorV1Result, ListMembersV1Params, ListOwnershipsV1Params, ListV1, TwitterResponse, UserV1 } from '../types';
import { CursoredV1Paginator } from './paginator.v1';
declare abstract class ListListsV1Paginator extends CursoredV1Paginator<DoubleEndedListsCursorV1Result, ListOwnershipsV1Params, ListV1> {
protected refreshInstanceFromResult(response: TwitterResponse<DoubleEndedListsCursorV1Result>, isNextPage: true): void;
protected getPageLengthFromRequest(result: TwitterResponse<DoubleEndedListsCursorV1Result>): number;
protected getItemArray(): ListV1[];
/**
* Lists returned by paginator.
*/
get lists(): ListV1[];
}
export declare class ListMembershipsV1Paginator extends ListListsV1Paginator {
protected _endpoint: string;
}
export declare class ListOwnershipsV1Paginator extends ListListsV1Paginator {
protected _endpoint: string;
}
export declare class ListSubscriptionsV1Paginator extends ListListsV1Paginator {
protected _endpoint: string;
}
declare abstract class ListUsersV1Paginator extends CursoredV1Paginator<DoubleEndedUsersCursorV1Result, ListMembersV1Params, UserV1> {
protected refreshInstanceFromResult(response: TwitterResponse<DoubleEndedUsersCursorV1Result>, isNextPage: true): void;
protected getPageLengthFromRequest(result: TwitterResponse<DoubleEndedUsersCursorV1Result>): number;
protected getItemArray(): UserV1[];
/**
* Users returned by paginator.
*/
get users(): UserV1[];
}
export declare class ListMembersV1Paginator extends ListUsersV1Paginator {
protected _endpoint: string;
}
export declare class ListSubscribersV1Paginator extends ListUsersV1Paginator {
protected _endpoint: string;
}
export {};

View File

@@ -0,0 +1,83 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListSubscribersV1Paginator = exports.ListMembersV1Paginator = exports.ListSubscriptionsV1Paginator = exports.ListOwnershipsV1Paginator = exports.ListMembershipsV1Paginator = void 0;
const paginator_v1_1 = require("./paginator.v1");
class ListListsV1Paginator extends paginator_v1_1.CursoredV1Paginator {
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.lists.push(...result.lists);
this._realData.next_cursor = result.next_cursor;
}
}
getPageLengthFromRequest(result) {
return result.data.lists.length;
}
getItemArray() {
return this.lists;
}
/**
* Lists returned by paginator.
*/
get lists() {
return this._realData.lists;
}
}
class ListMembershipsV1Paginator extends ListListsV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/memberships.json';
}
}
exports.ListMembershipsV1Paginator = ListMembershipsV1Paginator;
class ListOwnershipsV1Paginator extends ListListsV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/ownerships.json';
}
}
exports.ListOwnershipsV1Paginator = ListOwnershipsV1Paginator;
class ListSubscriptionsV1Paginator extends ListListsV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/subscriptions.json';
}
}
exports.ListSubscriptionsV1Paginator = ListSubscriptionsV1Paginator;
class ListUsersV1Paginator extends paginator_v1_1.CursoredV1Paginator {
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.users.push(...result.users);
this._realData.next_cursor = result.next_cursor;
}
}
getPageLengthFromRequest(result) {
return result.data.users.length;
}
getItemArray() {
return this.users;
}
/**
* Users returned by paginator.
*/
get users() {
return this._realData.users;
}
}
class ListMembersV1Paginator extends ListUsersV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/members.json';
}
}
exports.ListMembersV1Paginator = ListMembersV1Paginator;
class ListSubscribersV1Paginator extends ListUsersV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/subscribers.json';
}
}
exports.ListSubscribersV1Paginator = ListSubscribersV1Paginator;

View File

@@ -0,0 +1,26 @@
import type { GetListTimelineV2Params, ListTimelineV2Result, ListV2 } from '../types';
import { TimelineV2Paginator } from './v2.paginator';
declare abstract class ListTimelineV2Paginator<TResult extends ListTimelineV2Result, TParams extends GetListTimelineV2Params, TShared = any> extends TimelineV2Paginator<TResult, TParams, ListV2, TShared> {
protected getItemArray(): ListV2[];
/**
* Lists returned by paginator.
*/
get lists(): ListV2[];
get meta(): TResult["meta"];
}
export declare class UserOwnedListsV2Paginator extends ListTimelineV2Paginator<ListTimelineV2Result, GetListTimelineV2Params, {
id: string;
}> {
protected _endpoint: string;
}
export declare class UserListMembershipsV2Paginator extends ListTimelineV2Paginator<ListTimelineV2Result, GetListTimelineV2Params, {
id: string;
}> {
protected _endpoint: string;
}
export declare class UserListFollowedV2Paginator extends ListTimelineV2Paginator<ListTimelineV2Result, GetListTimelineV2Params, {
id: string;
}> {
protected _endpoint: string;
}
export {};

View File

@@ -0,0 +1,40 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserListFollowedV2Paginator = exports.UserListMembershipsV2Paginator = exports.UserOwnedListsV2Paginator = void 0;
const v2_paginator_1 = require("./v2.paginator");
class ListTimelineV2Paginator extends v2_paginator_1.TimelineV2Paginator {
getItemArray() {
return this.lists;
}
/**
* Lists returned by paginator.
*/
get lists() {
var _a;
return (_a = this._realData.data) !== null && _a !== void 0 ? _a : [];
}
get meta() {
return super.meta;
}
}
class UserOwnedListsV2Paginator extends ListTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/owned_lists';
}
}
exports.UserOwnedListsV2Paginator = UserOwnedListsV2Paginator;
class UserListMembershipsV2Paginator extends ListTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/list_memberships';
}
}
exports.UserListMembershipsV2Paginator = UserListMembershipsV2Paginator;
class UserListFollowedV2Paginator extends ListTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/followed_lists';
}
}
exports.UserListFollowedV2Paginator = UserListFollowedV2Paginator;

View File

@@ -0,0 +1,23 @@
import { CursoredV1Paginator } from './paginator.v1';
import type { MuteUserIdsV1Params, MuteUserIdsV1Result, MuteUserListV1Params, MuteUserListV1Result, TwitterResponse, UserV1 } from '../types';
export declare class MuteUserListV1Paginator extends CursoredV1Paginator<MuteUserListV1Result, MuteUserListV1Params, UserV1> {
protected _endpoint: string;
protected refreshInstanceFromResult(response: TwitterResponse<MuteUserListV1Result>, isNextPage: true): void;
protected getPageLengthFromRequest(result: TwitterResponse<MuteUserListV1Result>): number;
protected getItemArray(): UserV1[];
/**
* Users returned by paginator.
*/
get users(): UserV1[];
}
export declare class MuteUserIdsV1Paginator extends CursoredV1Paginator<MuteUserIdsV1Result, MuteUserIdsV1Params, string> {
protected _endpoint: string;
protected _maxResultsWhenFetchLast: number;
protected refreshInstanceFromResult(response: TwitterResponse<MuteUserIdsV1Result>, isNextPage: true): void;
protected getPageLengthFromRequest(result: TwitterResponse<MuteUserIdsV1Result>): number;
protected getItemArray(): string[];
/**
* Users IDs returned by paginator.
*/
get ids(): string[];
}

View File

@@ -0,0 +1,59 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MuteUserIdsV1Paginator = exports.MuteUserListV1Paginator = void 0;
const paginator_v1_1 = require("./paginator.v1");
class MuteUserListV1Paginator extends paginator_v1_1.CursoredV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'mutes/users/list.json';
}
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.users.push(...result.users);
this._realData.next_cursor = result.next_cursor;
}
}
getPageLengthFromRequest(result) {
return result.data.users.length;
}
getItemArray() {
return this.users;
}
/**
* Users returned by paginator.
*/
get users() {
return this._realData.users;
}
}
exports.MuteUserListV1Paginator = MuteUserListV1Paginator;
class MuteUserIdsV1Paginator extends paginator_v1_1.CursoredV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'mutes/users/ids.json';
this._maxResultsWhenFetchLast = 5000;
}
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.ids.push(...result.ids);
this._realData.next_cursor = result.next_cursor;
}
}
getPageLengthFromRequest(result) {
return result.data.ids.length;
}
getItemArray() {
return this.ids;
}
/**
* Users IDs returned by paginator.
*/
get ids() {
return this._realData.ids;
}
}
exports.MuteUserIdsV1Paginator = MuteUserIdsV1Paginator;

View File

@@ -0,0 +1,13 @@
import { TwitterResponse } from '../types';
import TwitterPaginator from './TwitterPaginator';
export declare abstract class CursoredV1Paginator<TApiResult extends {
next_cursor_str?: string;
next_cursor?: string;
}, TApiParams extends {
cursor?: string;
}, TItem, TParams = any> extends TwitterPaginator<TApiResult, TApiParams, TItem, TParams> {
protected getNextQueryParams(maxResults?: number): Partial<TApiParams>;
protected isFetchLastOver(result: TwitterResponse<TApiResult>): boolean;
protected canFetchNextPage(result: TApiResult): boolean;
private isNextCursorInvalid;
}

View File

@@ -0,0 +1,33 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CursoredV1Paginator = void 0;
const TwitterPaginator_1 = __importDefault(require("./TwitterPaginator"));
class CursoredV1Paginator extends TwitterPaginator_1.default {
getNextQueryParams(maxResults) {
var _a;
return {
...this._queryParams,
cursor: (_a = this._realData.next_cursor_str) !== null && _a !== void 0 ? _a : this._realData.next_cursor,
...(maxResults ? { count: maxResults } : {}),
};
}
isFetchLastOver(result) {
// If we cant fetch next page
return !this.canFetchNextPage(result.data);
}
canFetchNextPage(result) {
// If one of cursor is valid
return !this.isNextCursorInvalid(result.next_cursor) || !this.isNextCursorInvalid(result.next_cursor_str);
}
isNextCursorInvalid(value) {
return value === undefined
|| value === 0
|| value === -1
|| value === '0'
|| value === '-1';
}
}
exports.CursoredV1Paginator = CursoredV1Paginator;

View File

@@ -0,0 +1,37 @@
import TwitterPaginator from './TwitterPaginator';
import { TwitterResponse, TweetV1, TweetV1TimelineResult, TweetV1TimelineParams, TweetV1UserTimelineParams, ListStatusesV1Params } from '../types';
/** A generic TwitterPaginator able to consume TweetV1 timelines. */
declare abstract class TweetTimelineV1Paginator<TResult extends TweetV1TimelineResult, TParams extends TweetV1TimelineParams, TShared = any> extends TwitterPaginator<TResult, TParams, TweetV1, TShared> {
protected hasFinishedFetch: boolean;
protected refreshInstanceFromResult(response: TwitterResponse<TResult>, isNextPage: true): void;
protected getNextQueryParams(maxResults?: number): {
max_results?: number | undefined;
} & Partial<TParams> & {
max_id: string;
};
protected getPageLengthFromRequest(result: TwitterResponse<TResult>): number;
protected isFetchLastOver(result: TwitterResponse<TResult>): boolean;
protected canFetchNextPage(result: TResult): boolean;
protected getItemArray(): TResult;
/**
* Tweets returned by paginator.
*/
get tweets(): TResult;
get done(): boolean;
}
export declare class HomeTimelineV1Paginator extends TweetTimelineV1Paginator<TweetV1TimelineResult, TweetV1TimelineParams> {
protected _endpoint: string;
}
export declare class MentionTimelineV1Paginator extends TweetTimelineV1Paginator<TweetV1TimelineResult, TweetV1TimelineParams> {
protected _endpoint: string;
}
export declare class UserTimelineV1Paginator extends TweetTimelineV1Paginator<TweetV1TimelineResult, TweetV1UserTimelineParams> {
protected _endpoint: string;
}
export declare class ListTimelineV1Paginator extends TweetTimelineV1Paginator<TweetV1TimelineResult, ListStatusesV1Params> {
protected _endpoint: string;
}
export declare class UserFavoritesV1Paginator extends TweetTimelineV1Paginator<TweetV1TimelineResult, TweetV1UserTimelineParams> {
protected _endpoint: string;
}
export {};

View File

@@ -0,0 +1,92 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserFavoritesV1Paginator = exports.ListTimelineV1Paginator = exports.UserTimelineV1Paginator = exports.MentionTimelineV1Paginator = exports.HomeTimelineV1Paginator = void 0;
const TwitterPaginator_1 = __importDefault(require("./TwitterPaginator"));
/** A generic TwitterPaginator able to consume TweetV1 timelines. */
class TweetTimelineV1Paginator extends TwitterPaginator_1.default {
constructor() {
super(...arguments);
this.hasFinishedFetch = false;
}
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.push(...result);
// HINT: This is an approximation, as "end" of pagination cannot be safely determined without cursors.
this.hasFinishedFetch = result.length === 0;
}
}
getNextQueryParams(maxResults) {
const lastestId = BigInt(this._realData[this._realData.length - 1].id_str);
return {
...this.injectQueryParams(maxResults),
max_id: (lastestId - BigInt(1)).toString(),
};
}
getPageLengthFromRequest(result) {
return result.data.length;
}
isFetchLastOver(result) {
return !result.data.length;
}
canFetchNextPage(result) {
return result.length > 0;
}
getItemArray() {
return this.tweets;
}
/**
* Tweets returned by paginator.
*/
get tweets() {
return this._realData;
}
get done() {
return super.done || this.hasFinishedFetch;
}
}
// Timelines
// Home
class HomeTimelineV1Paginator extends TweetTimelineV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'statuses/home_timeline.json';
}
}
exports.HomeTimelineV1Paginator = HomeTimelineV1Paginator;
// Mention
class MentionTimelineV1Paginator extends TweetTimelineV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'statuses/mentions_timeline.json';
}
}
exports.MentionTimelineV1Paginator = MentionTimelineV1Paginator;
// User
class UserTimelineV1Paginator extends TweetTimelineV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'statuses/user_timeline.json';
}
}
exports.UserTimelineV1Paginator = UserTimelineV1Paginator;
// Lists
class ListTimelineV1Paginator extends TweetTimelineV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/statuses.json';
}
}
exports.ListTimelineV1Paginator = ListTimelineV1Paginator;
// Favorites
class UserFavoritesV1Paginator extends TweetTimelineV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'favorites/list.json';
}
}
exports.UserFavoritesV1Paginator = UserFavoritesV1Paginator;

View File

@@ -0,0 +1,74 @@
import { Tweetv2SearchParams, Tweetv2SearchResult, TwitterResponse, TweetV2, Tweetv2TimelineResult, TweetV2TimelineParams, TweetV2PaginableTimelineResult, TweetV2UserTimelineParams, Tweetv2ListResult, TweetV2PaginableListParams, TweetV2PaginableTimelineParams, TweetV2HomeTimelineParams } from '../types';
import { TimelineV2Paginator, TwitterV2Paginator } from './v2.paginator';
/** A generic PreviousableTwitterPaginator able to consume TweetV2 timelines with since_id, until_id and next_token (when available). */
declare abstract class TweetTimelineV2Paginator<TResult extends Tweetv2TimelineResult, TParams extends TweetV2TimelineParams, TShared = any> extends TwitterV2Paginator<TResult, TParams, TweetV2, TShared> {
protected refreshInstanceFromResult(response: TwitterResponse<TResult>, isNextPage: boolean): void;
protected getNextQueryParams(maxResults?: number): Partial<TParams>;
protected getPreviousQueryParams(maxResults?: number): Partial<TParams>;
protected getPageLengthFromRequest(result: TwitterResponse<TResult>): number;
protected isFetchLastOver(result: TwitterResponse<TResult>): boolean;
protected canFetchNextPage(result: TResult): boolean;
protected getItemArray(): TweetV2[];
protected dateStringToSnowflakeId(dateStr: string): string;
/**
* Tweets returned by paginator.
*/
get tweets(): TweetV2[];
get meta(): TResult["meta"];
}
/** A generic PreviousableTwitterPaginator able to consume TweetV2 timelines with pagination_tokens. */
declare abstract class TweetPaginableTimelineV2Paginator<TResult extends TweetV2PaginableTimelineResult, TParams extends TweetV2PaginableTimelineParams, TShared = any> extends TimelineV2Paginator<TResult, TParams, TweetV2, TShared> {
protected refreshInstanceFromResult(response: TwitterResponse<TResult>, isNextPage: boolean): void;
protected getItemArray(): TweetV2[];
/**
* Tweets returned by paginator.
*/
get tweets(): TweetV2[];
get meta(): TResult["meta"];
}
export declare class TweetSearchRecentV2Paginator extends TweetTimelineV2Paginator<Tweetv2SearchResult, Tweetv2SearchParams> {
protected _endpoint: string;
}
export declare class TweetSearchAllV2Paginator extends TweetTimelineV2Paginator<Tweetv2SearchResult, Tweetv2SearchParams> {
protected _endpoint: string;
}
export declare class QuotedTweetsTimelineV2Paginator extends TweetPaginableTimelineV2Paginator<TweetV2PaginableTimelineResult, TweetV2PaginableTimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export declare class TweetHomeTimelineV2Paginator extends TweetPaginableTimelineV2Paginator<TweetV2PaginableTimelineResult, TweetV2HomeTimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
declare type TUserTimelinePaginatorShared = {
id: string;
};
export declare class TweetUserTimelineV2Paginator extends TweetPaginableTimelineV2Paginator<TweetV2PaginableTimelineResult, TweetV2UserTimelineParams, TUserTimelinePaginatorShared> {
protected _endpoint: string;
}
export declare class TweetUserMentionTimelineV2Paginator extends TweetPaginableTimelineV2Paginator<TweetV2PaginableTimelineResult, TweetV2PaginableTimelineParams, TUserTimelinePaginatorShared> {
protected _endpoint: string;
}
export declare class TweetBookmarksTimelineV2Paginator extends TweetPaginableTimelineV2Paginator<TweetV2PaginableTimelineResult, TweetV2PaginableTimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
/** A generic TwitterPaginator able to consume TweetV2 timelines. */
declare abstract class TweetListV2Paginator<TResult extends Tweetv2ListResult, TParams extends TweetV2PaginableListParams, TShared = any> extends TimelineV2Paginator<TResult, TParams, TweetV2, TShared> {
/**
* Tweets returned by paginator.
*/
get tweets(): TweetV2[];
get meta(): TResult["meta"];
protected getItemArray(): TweetV2[];
}
export declare class TweetV2UserLikedTweetsPaginator extends TweetListV2Paginator<Tweetv2ListResult, TweetV2PaginableListParams, TUserTimelinePaginatorShared> {
protected _endpoint: string;
}
export declare class TweetV2ListTweetsPaginator extends TweetListV2Paginator<Tweetv2ListResult, TweetV2PaginableListParams, TUserTimelinePaginatorShared> {
protected _endpoint: string;
}
export {};

View File

@@ -0,0 +1,205 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TweetV2ListTweetsPaginator = exports.TweetV2UserLikedTweetsPaginator = exports.TweetBookmarksTimelineV2Paginator = exports.TweetUserMentionTimelineV2Paginator = exports.TweetUserTimelineV2Paginator = exports.TweetHomeTimelineV2Paginator = exports.QuotedTweetsTimelineV2Paginator = exports.TweetSearchAllV2Paginator = exports.TweetSearchRecentV2Paginator = void 0;
const v2_paginator_1 = require("./v2.paginator");
/** A generic PreviousableTwitterPaginator able to consume TweetV2 timelines with since_id, until_id and next_token (when available). */
class TweetTimelineV2Paginator extends v2_paginator_1.TwitterV2Paginator {
refreshInstanceFromResult(response, isNextPage) {
var _a;
const result = response.data;
const resultData = (_a = result.data) !== null && _a !== void 0 ? _a : [];
this._rateLimit = response.rateLimit;
if (!this._realData.data) {
this._realData.data = [];
}
if (isNextPage) {
this._realData.meta.oldest_id = result.meta.oldest_id;
this._realData.meta.result_count += result.meta.result_count;
this._realData.meta.next_token = result.meta.next_token;
this._realData.data.push(...resultData);
}
else {
this._realData.meta.newest_id = result.meta.newest_id;
this._realData.meta.result_count += result.meta.result_count;
this._realData.data.unshift(...resultData);
}
this.updateIncludes(result);
}
getNextQueryParams(maxResults) {
this.assertUsable();
const params = { ...this.injectQueryParams(maxResults) };
if (this._realData.meta.next_token) {
params.next_token = this._realData.meta.next_token;
}
else {
if (params.start_time) {
// until_id and start_time are forbidden together for some reason, so convert start_time to a since_id.
params.since_id = this.dateStringToSnowflakeId(params.start_time);
delete params.start_time;
}
if (params.end_time) {
// until_id overrides end_time, so delete it
delete params.end_time;
}
params.until_id = this._realData.meta.oldest_id;
}
return params;
}
getPreviousQueryParams(maxResults) {
this.assertUsable();
return {
...this.injectQueryParams(maxResults),
since_id: this._realData.meta.newest_id,
};
}
getPageLengthFromRequest(result) {
var _a, _b;
return (_b = (_a = result.data.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
}
isFetchLastOver(result) {
var _a;
return !((_a = result.data.data) === null || _a === void 0 ? void 0 : _a.length) || !this.canFetchNextPage(result.data);
}
canFetchNextPage(result) {
return !!result.meta.next_token;
}
getItemArray() {
return this.tweets;
}
dateStringToSnowflakeId(dateStr) {
const TWITTER_START_EPOCH = BigInt('1288834974657');
const date = new Date(dateStr);
if (isNaN(date.valueOf())) {
throw new Error('Unable to convert start_time/end_time to a valid date. A ISO 8601 DateTime is excepted, please check your input.');
}
const dateTimestamp = BigInt(date.valueOf());
return ((dateTimestamp - TWITTER_START_EPOCH) << BigInt('22')).toString();
}
/**
* Tweets returned by paginator.
*/
get tweets() {
var _a;
return (_a = this._realData.data) !== null && _a !== void 0 ? _a : [];
}
get meta() {
return super.meta;
}
}
/** A generic PreviousableTwitterPaginator able to consume TweetV2 timelines with pagination_tokens. */
class TweetPaginableTimelineV2Paginator extends v2_paginator_1.TimelineV2Paginator {
refreshInstanceFromResult(response, isNextPage) {
super.refreshInstanceFromResult(response, isNextPage);
const result = response.data;
if (isNextPage) {
this._realData.meta.oldest_id = result.meta.oldest_id;
}
else {
this._realData.meta.newest_id = result.meta.newest_id;
}
}
getItemArray() {
return this.tweets;
}
/**
* Tweets returned by paginator.
*/
get tweets() {
var _a;
return (_a = this._realData.data) !== null && _a !== void 0 ? _a : [];
}
get meta() {
return super.meta;
}
}
// ----------------
// - Tweet search -
// ----------------
class TweetSearchRecentV2Paginator extends TweetTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'tweets/search/recent';
}
}
exports.TweetSearchRecentV2Paginator = TweetSearchRecentV2Paginator;
class TweetSearchAllV2Paginator extends TweetTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'tweets/search/all';
}
}
exports.TweetSearchAllV2Paginator = TweetSearchAllV2Paginator;
class QuotedTweetsTimelineV2Paginator extends TweetPaginableTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'tweets/:id/quote_tweets';
}
}
exports.QuotedTweetsTimelineV2Paginator = QuotedTweetsTimelineV2Paginator;
// -----------------
// - Home timeline -
// -----------------
class TweetHomeTimelineV2Paginator extends TweetPaginableTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/timelines/reverse_chronological';
}
}
exports.TweetHomeTimelineV2Paginator = TweetHomeTimelineV2Paginator;
class TweetUserTimelineV2Paginator extends TweetPaginableTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/tweets';
}
}
exports.TweetUserTimelineV2Paginator = TweetUserTimelineV2Paginator;
class TweetUserMentionTimelineV2Paginator extends TweetPaginableTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/mentions';
}
}
exports.TweetUserMentionTimelineV2Paginator = TweetUserMentionTimelineV2Paginator;
// -------------
// - Bookmarks -
// -------------
class TweetBookmarksTimelineV2Paginator extends TweetPaginableTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/bookmarks';
}
}
exports.TweetBookmarksTimelineV2Paginator = TweetBookmarksTimelineV2Paginator;
// ---------------------------------------------------------------------------------
// - Tweet lists (consume tweets with pagination tokens instead of since/until id) -
// ---------------------------------------------------------------------------------
/** A generic TwitterPaginator able to consume TweetV2 timelines. */
class TweetListV2Paginator extends v2_paginator_1.TimelineV2Paginator {
/**
* Tweets returned by paginator.
*/
get tweets() {
var _a;
return (_a = this._realData.data) !== null && _a !== void 0 ? _a : [];
}
get meta() {
return super.meta;
}
getItemArray() {
return this.tweets;
}
}
class TweetV2UserLikedTweetsPaginator extends TweetListV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/liked_tweets';
}
}
exports.TweetV2UserLikedTweetsPaginator = TweetV2UserLikedTweetsPaginator;
class TweetV2ListTweetsPaginator extends TweetListV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/:id/tweets';
}
}
exports.TweetV2ListTweetsPaginator = TweetV2ListTweetsPaginator;

View File

@@ -0,0 +1,37 @@
import TwitterPaginator from './TwitterPaginator';
import { FriendshipsIncomingV1Params, FriendshipsIncomingV1Result, TwitterResponse, UserSearchV1Params, UserV1 } from '../types';
import { CursoredV1Paginator } from './paginator.v1';
/** A generic TwitterPaginator able to consume TweetV1 timelines. */
export declare class UserSearchV1Paginator extends TwitterPaginator<UserV1[], UserSearchV1Params, UserV1> {
_endpoint: string;
protected refreshInstanceFromResult(response: TwitterResponse<UserV1[]>, isNextPage: true): void;
protected getNextQueryParams(maxResults?: number): {
count?: number | undefined;
page: number;
q?: string | undefined;
include_entities?: boolean | undefined;
tweet_mode?: "extended" | undefined;
};
protected getPageLengthFromRequest(result: TwitterResponse<UserV1[]>): number;
protected isFetchLastOver(result: TwitterResponse<UserV1[]>): boolean;
protected canFetchNextPage(result: UserV1[]): boolean;
protected getItemArray(): UserV1[];
/**
* Users returned by paginator.
*/
get users(): UserV1[];
}
export declare class FriendshipsIncomingV1Paginator extends CursoredV1Paginator<FriendshipsIncomingV1Result, FriendshipsIncomingV1Params, string> {
protected _endpoint: string;
protected _maxResultsWhenFetchLast: number;
protected refreshInstanceFromResult(response: TwitterResponse<FriendshipsIncomingV1Result>, isNextPage: true): void;
protected getPageLengthFromRequest(result: TwitterResponse<FriendshipsIncomingV1Result>): number;
protected getItemArray(): string[];
/**
* Users IDs returned by paginator.
*/
get ids(): string[];
}
export declare class FriendshipsOutgoingV1Paginator extends FriendshipsIncomingV1Paginator {
protected _endpoint: string;
}

View File

@@ -0,0 +1,85 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FriendshipsOutgoingV1Paginator = exports.FriendshipsIncomingV1Paginator = exports.UserSearchV1Paginator = void 0;
const TwitterPaginator_1 = __importDefault(require("./TwitterPaginator"));
const paginator_v1_1 = require("./paginator.v1");
/** A generic TwitterPaginator able to consume TweetV1 timelines. */
class UserSearchV1Paginator extends TwitterPaginator_1.default {
constructor() {
super(...arguments);
this._endpoint = 'users/search.json';
}
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.push(...result);
}
}
getNextQueryParams(maxResults) {
var _a;
const previousPage = Number((_a = this._queryParams.page) !== null && _a !== void 0 ? _a : '1');
return {
...this._queryParams,
page: previousPage + 1,
...maxResults ? { count: maxResults } : {},
};
}
getPageLengthFromRequest(result) {
return result.data.length;
}
isFetchLastOver(result) {
return !result.data.length;
}
canFetchNextPage(result) {
return result.length > 0;
}
getItemArray() {
return this.users;
}
/**
* Users returned by paginator.
*/
get users() {
return this._realData;
}
}
exports.UserSearchV1Paginator = UserSearchV1Paginator;
class FriendshipsIncomingV1Paginator extends paginator_v1_1.CursoredV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'friendships/incoming.json';
this._maxResultsWhenFetchLast = 5000;
}
refreshInstanceFromResult(response, isNextPage) {
const result = response.data;
this._rateLimit = response.rateLimit;
if (isNextPage) {
this._realData.ids.push(...result.ids);
this._realData.next_cursor = result.next_cursor;
}
}
getPageLengthFromRequest(result) {
return result.data.ids.length;
}
getItemArray() {
return this.ids;
}
/**
* Users IDs returned by paginator.
*/
get ids() {
return this._realData.ids;
}
}
exports.FriendshipsIncomingV1Paginator = FriendshipsIncomingV1Paginator;
class FriendshipsOutgoingV1Paginator extends FriendshipsIncomingV1Paginator {
constructor() {
super(...arguments);
this._endpoint = 'friendships/outgoing.json';
}
}
exports.FriendshipsOutgoingV1Paginator = FriendshipsOutgoingV1Paginator;

View File

@@ -0,0 +1,52 @@
import { UserV2, UserV2TimelineParams, UserV2TimelineResult } from '../types';
import { TimelineV2Paginator } from './v2.paginator';
/** A generic PreviousableTwitterPaginator able to consume UserV2 timelines. */
declare abstract class UserTimelineV2Paginator<TResult extends UserV2TimelineResult, TParams extends UserV2TimelineParams, TShared = any> extends TimelineV2Paginator<TResult, TParams, UserV2, TShared> {
protected getItemArray(): UserV2[];
/**
* Users returned by paginator.
*/
get users(): UserV2[];
get meta(): TResult["meta"];
}
export declare class UserBlockingUsersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export declare class UserMutingUsersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export declare class UserFollowersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export declare class UserFollowingV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export declare class UserListMembersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export declare class UserListFollowersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export declare class TweetLikingUsersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export declare class TweetRetweetersUsersV2Paginator extends UserTimelineV2Paginator<UserV2TimelineResult, UserV2TimelineParams, {
id: string;
}> {
protected _endpoint: string;
}
export {};

View File

@@ -0,0 +1,76 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TweetRetweetersUsersV2Paginator = exports.TweetLikingUsersV2Paginator = exports.UserListFollowersV2Paginator = exports.UserListMembersV2Paginator = exports.UserFollowingV2Paginator = exports.UserFollowersV2Paginator = exports.UserMutingUsersV2Paginator = exports.UserBlockingUsersV2Paginator = void 0;
const v2_paginator_1 = require("./v2.paginator");
/** A generic PreviousableTwitterPaginator able to consume UserV2 timelines. */
class UserTimelineV2Paginator extends v2_paginator_1.TimelineV2Paginator {
getItemArray() {
return this.users;
}
/**
* Users returned by paginator.
*/
get users() {
var _a;
return (_a = this._realData.data) !== null && _a !== void 0 ? _a : [];
}
get meta() {
return super.meta;
}
}
class UserBlockingUsersV2Paginator extends UserTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/blocking';
}
}
exports.UserBlockingUsersV2Paginator = UserBlockingUsersV2Paginator;
class UserMutingUsersV2Paginator extends UserTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/muting';
}
}
exports.UserMutingUsersV2Paginator = UserMutingUsersV2Paginator;
class UserFollowersV2Paginator extends UserTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/followers';
}
}
exports.UserFollowersV2Paginator = UserFollowersV2Paginator;
class UserFollowingV2Paginator extends UserTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'users/:id/following';
}
}
exports.UserFollowingV2Paginator = UserFollowingV2Paginator;
class UserListMembersV2Paginator extends UserTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/:id/members';
}
}
exports.UserListMembersV2Paginator = UserListMembersV2Paginator;
class UserListFollowersV2Paginator extends UserTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'lists/:id/followers';
}
}
exports.UserListFollowersV2Paginator = UserListFollowersV2Paginator;
class TweetLikingUsersV2Paginator extends UserTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'tweets/:id/liking_users';
}
}
exports.TweetLikingUsersV2Paginator = TweetLikingUsersV2Paginator;
class TweetRetweetersUsersV2Paginator extends UserTimelineV2Paginator {
constructor() {
super(...arguments);
this._endpoint = 'tweets/:id/retweeted_by';
}
}
exports.TweetRetweetersUsersV2Paginator = TweetRetweetersUsersV2Paginator;

View File

@@ -0,0 +1,36 @@
import type { TwitterResponse } from '../types';
import type { DataMetaAndIncludeV2 } from '../types/v2/shared.v2.types';
import { TwitterV2IncludesHelper } from '../v2/includes.v2.helper';
import { PreviousableTwitterPaginator } from './TwitterPaginator';
/** A generic PreviousableTwitterPaginator with common v2 helper methods. */
export declare abstract class TwitterV2Paginator<TResult extends DataMetaAndIncludeV2<any, any, any>, TParams extends object, TItem, TShared = any> extends PreviousableTwitterPaginator<TResult, TParams, TItem, TShared> {
protected _includesInstance?: TwitterV2IncludesHelper;
protected updateIncludes(data: TResult): void;
/** Throw if the current paginator is not usable. */
protected assertUsable(): void;
get meta(): any;
get includes(): TwitterV2IncludesHelper;
get errors(): import("../types").InlineErrorV2[];
/** `true` if this paginator only contains error payload and no metadata found to consume data. */
get unusable(): boolean;
}
/** A generic TwitterV2Paginator able to consume v2 timelines that use max_results and pagination tokens. */
export declare abstract class TimelineV2Paginator<TResult extends DataMetaAndIncludeV2<any, any, any>, TParams extends {
max_results?: number;
pagination_token?: string;
}, TItem, TShared = any> extends TwitterV2Paginator<TResult, TParams, TItem, TShared> {
protected refreshInstanceFromResult(response: TwitterResponse<TResult>, isNextPage: boolean): void;
protected getNextQueryParams(maxResults?: number): {
max_results?: number | undefined;
} & Partial<TParams> & {
pagination_token: any;
};
protected getPreviousQueryParams(maxResults?: number): {
max_results?: number | undefined;
} & Partial<TParams> & {
pagination_token: any;
};
protected getPageLengthFromRequest(result: TwitterResponse<TResult>): any;
protected isFetchLastOver(result: TwitterResponse<TResult>): boolean;
protected canFetchNextPage(result: TResult): boolean;
}

View File

@@ -0,0 +1,113 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimelineV2Paginator = exports.TwitterV2Paginator = void 0;
const includes_v2_helper_1 = require("../v2/includes.v2.helper");
const TwitterPaginator_1 = require("./TwitterPaginator");
/** A generic PreviousableTwitterPaginator with common v2 helper methods. */
class TwitterV2Paginator extends TwitterPaginator_1.PreviousableTwitterPaginator {
updateIncludes(data) {
// Update errors
if (data.errors) {
if (!this._realData.errors) {
this._realData.errors = [];
}
this._realData.errors = [...this._realData.errors, ...data.errors];
}
// Update includes
if (!data.includes) {
return;
}
if (!this._realData.includes) {
this._realData.includes = {};
}
const includesRealData = this._realData.includes;
for (const [includeKey, includeArray] of Object.entries(data.includes)) {
if (!includesRealData[includeKey]) {
includesRealData[includeKey] = [];
}
includesRealData[includeKey] = [
...includesRealData[includeKey],
...includeArray,
];
}
}
/** Throw if the current paginator is not usable. */
assertUsable() {
if (this.unusable) {
throw new Error('Unable to use this paginator to fetch more data, as it does not contain any metadata.' +
' Check .errors property for more details.');
}
}
get meta() {
return this._realData.meta;
}
get includes() {
var _a;
if (!((_a = this._realData) === null || _a === void 0 ? void 0 : _a.includes)) {
return new includes_v2_helper_1.TwitterV2IncludesHelper(this._realData);
}
if (this._includesInstance) {
return this._includesInstance;
}
return this._includesInstance = new includes_v2_helper_1.TwitterV2IncludesHelper(this._realData);
}
get errors() {
var _a;
return (_a = this._realData.errors) !== null && _a !== void 0 ? _a : [];
}
/** `true` if this paginator only contains error payload and no metadata found to consume data. */
get unusable() {
return this.errors.length > 0 && !this._realData.meta && !this._realData.data;
}
}
exports.TwitterV2Paginator = TwitterV2Paginator;
/** A generic TwitterV2Paginator able to consume v2 timelines that use max_results and pagination tokens. */
class TimelineV2Paginator extends TwitterV2Paginator {
refreshInstanceFromResult(response, isNextPage) {
var _a;
const result = response.data;
const resultData = (_a = result.data) !== null && _a !== void 0 ? _a : [];
this._rateLimit = response.rateLimit;
if (!this._realData.data) {
this._realData.data = [];
}
if (isNextPage) {
this._realData.meta.result_count += result.meta.result_count;
this._realData.meta.next_token = result.meta.next_token;
this._realData.data.push(...resultData);
}
else {
this._realData.meta.result_count += result.meta.result_count;
this._realData.meta.previous_token = result.meta.previous_token;
this._realData.data.unshift(...resultData);
}
this.updateIncludes(result);
}
getNextQueryParams(maxResults) {
this.assertUsable();
return {
...this.injectQueryParams(maxResults),
pagination_token: this._realData.meta.next_token,
};
}
getPreviousQueryParams(maxResults) {
this.assertUsable();
return {
...this.injectQueryParams(maxResults),
pagination_token: this._realData.meta.previous_token,
};
}
getPageLengthFromRequest(result) {
var _a, _b;
return (_b = (_a = result.data.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
}
isFetchLastOver(result) {
var _a;
return !((_a = result.data.data) === null || _a === void 0 ? void 0 : _a.length) || !this.canFetchNextPage(result.data);
}
canFetchNextPage(result) {
var _a;
return !!((_a = result.meta) === null || _a === void 0 ? void 0 : _a.next_token);
}
}
exports.TimelineV2Paginator = TimelineV2Paginator;