-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathclient.base.ts
More file actions
319 lines (264 loc) · 11.2 KB
/
Copy pathclient.base.ts
File metadata and controls
319 lines (264 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import type { IClientSettings, ITwitterApiClientPlugin, TwitterApiBasicAuth, TwitterApiOAuth2Init, TwitterApiTokens, TwitterRateLimit, TwitterResponse, UserV1, UserV2Result } from './types';
import { ClientRequestMaker } from './client-mixins/request-maker.mixin';
import TweetStream from './stream/TweetStream';
import { sharedPromise, SharedPromise } from './helpers';
import { API_V1_1_PREFIX, API_V2_PREFIX } from './globals';
import type { TAcceptedInitToken, TCustomizableRequestArgs, TRequestBody, TRequestQuery } from './types/request-maker.mixin.types';
export type TGetClientRequestArgs = TCustomizableRequestArgs & {
prefix?: string;
fullResponse?: boolean;
};
type TGetClientRequestArgsFullResponse = TClientRequestArgs & { fullResponse: true };
type TGetClientRequestArgsDataResponse = TClientRequestArgs & { fullResponse?: false };
export type TClientRequestArgs = TCustomizableRequestArgs & {
prefix?: string;
fullResponse?: boolean;
query?: TRequestQuery;
};
type TClientRequestArgsFullResponse = TClientRequestArgs & { fullResponse: true };
type TClientRequestArgsDataResponse = TClientRequestArgs & { fullResponse?: false };
export type TStreamClientRequestArgs = TCustomizableRequestArgs & {
prefix?: string;
query?: TRequestQuery;
payloadIsError?: (data: any) => boolean;
/**
* Choose to make or not initial connection.
* Method `.connect` must be called on returned `TweetStream` object
* to start stream if `autoConnect` is set to `false`.
* Defaults to `true`.
*/
autoConnect?: boolean;
};
export type TStreamClientRequestArgsWithAutoConnect = TStreamClientRequestArgs & { autoConnect?: true };
export type TStreamClientRequestArgsWithoutAutoConnect = TStreamClientRequestArgs & { autoConnect: false };
/**
* Base class for Twitter instances
*/
export default abstract class TwitterApiBase {
protected _prefix: string | undefined;
protected _currentUser: SharedPromise<UserV1> | null = null;
protected _currentUserV2: SharedPromise<UserV2Result> | null = null;
protected _requestMaker: ClientRequestMaker;
/**
* Create a new TwitterApi object without authentication.
*/
constructor(_?: undefined, settings?: Partial<IClientSettings>);
/**
* Create a new TwitterApi object with OAuth 2.0 Bearer authentication.
*/
constructor(bearerToken: string, settings?: Partial<IClientSettings>);
/**
* Create a new TwitterApi object with three-legged OAuth 1.0a authentication.
*/
constructor(tokens: TwitterApiTokens, settings?: Partial<IClientSettings>);
/**
* Create a new TwitterApi object with only client ID needed for OAuth2 user-flow.
*/
constructor(oauth2Init: TwitterApiOAuth2Init, settings?: Partial<IClientSettings>);
/**
* Create a new TwitterApi object with Basic HTTP authentication.
*/
constructor(credentials: TwitterApiBasicAuth, settings?: Partial<IClientSettings>);
/**
* Create a clone of {instance}.
*/
constructor(instance: TwitterApiBase, settings?: Partial<IClientSettings>);
public constructor(
token?: TAcceptedInitToken | TwitterApiBase,
settings: Partial<IClientSettings> = {},
) {
if (token instanceof TwitterApiBase) {
this._requestMaker = token._requestMaker;
}
else {
this._requestMaker = new ClientRequestMaker(settings);
this._requestMaker.initializeToken(token);
}
}
/* Prefix/Token handling */
protected setPrefix(prefix: string | undefined) {
this._prefix = prefix;
}
public cloneWithPrefix(prefix: string): this {
const clone = (this.constructor as any)(this);
(clone as TwitterApiBase).setPrefix(prefix);
return clone;
}
public getActiveTokens() {
return this._requestMaker.getActiveTokens();
}
/* Rate limit cache / Plugins */
public getPlugins() {
return this._requestMaker.getPlugins();
}
public getPluginOfType<T extends ITwitterApiClientPlugin>(type: { new(...args: any[]): T }): T | undefined {
return this.getPlugins().find(plugin => plugin instanceof type) as T | undefined;
}
/**
* @deprecated - Migrate to plugin `@twitter-api-v2/plugin-rate-limit`
*
* Tells if you hit the Twitter rate limit for {endpoint}.
* (local data only, this should not ask anything to Twitter)
*/
public hasHitRateLimit(endpoint: string) {
if (this.isRateLimitStatusObsolete(endpoint)) {
return false;
}
return this.getLastRateLimitStatus(endpoint)?.remaining === 0;
}
/**
* @deprecated - Migrate to plugin `@twitter-api-v2/plugin-rate-limit`
*
* Tells if you hit the returned Twitter rate limit for {endpoint} has expired.
* If client has no saved rate limit data for {endpoint}, this will gives you `true`.
*/
public isRateLimitStatusObsolete(endpoint: string) {
const rateLimit = this.getLastRateLimitStatus(endpoint);
if (rateLimit === undefined) {
return true;
}
// Timestamps are exprimed in seconds, JS works with ms
return (rateLimit.reset * 1000) < Date.now();
}
/**
* @deprecated - Migrate to plugin `@twitter-api-v2/plugin-rate-limit`
*
* Get the last obtained Twitter rate limit information for {endpoint}.
* (local data only, this should not ask anything to Twitter)
*/
public getLastRateLimitStatus(endpoint: string): TwitterRateLimit | undefined {
const endpointWithPrefix = endpoint.match(/^https?:\/\//) ? endpoint : (this._prefix + endpoint);
return this._requestMaker.getRateLimits()[endpointWithPrefix];
}
/* Current user cache */
/** Get cached current user. */
protected getCurrentUserObject(forceFetch = false) {
if (!forceFetch && this._currentUser) {
if (this._currentUser.value) {
return Promise.resolve(this._currentUser.value);
}
return this._currentUser.promise;
}
this._currentUser = sharedPromise(() => this.get<UserV1>(
'account/verify_credentials.json',
{ tweet_mode: 'extended' },
{ prefix: API_V1_1_PREFIX },
));
return this._currentUser.promise;
}
/**
* Get cached current user from v2 API.
* This can only be the slimest available `UserV2` object, with only `id`, `name` and `username` properties defined.
*
* To get a customized `UserV2Result`, use `.v2.me()`
*
* OAuth2 scopes: `tweet.read` & `users.read`
*/
protected getCurrentUserV2Object(forceFetch = false) {
if (!forceFetch && this._currentUserV2) {
if (this._currentUserV2.value) {
return Promise.resolve(this._currentUserV2.value);
}
return this._currentUserV2.promise;
}
this._currentUserV2 = sharedPromise(() => this.get<UserV2Result>('users/me', undefined, { prefix: API_V2_PREFIX }));
return this._currentUserV2.promise;
}
/* Direct HTTP methods */
async get<T = any>(url: string, query?: TRequestQuery, args?: TGetClientRequestArgsDataResponse) : Promise<T>;
async get<T = any>(url: string, query?: TRequestQuery, args?: TGetClientRequestArgsFullResponse) : Promise<TwitterResponse<T>>;
public async get<T = any>(
url: string,
query: TRequestQuery = {},
{ fullResponse, prefix = this._prefix, ...rest }: TGetClientRequestArgs = {},
) : Promise<T | TwitterResponse<T>> {
if (prefix)
url = prefix + url;
const resp = await this._requestMaker.send<T>({
url,
method: 'GET',
query,
...rest,
});
return fullResponse ? resp : resp.data;
}
async delete<T = any>(url: string, query?: TRequestQuery, args?: TGetClientRequestArgsDataResponse) : Promise<T>;
async delete<T = any>(url: string, query?: TRequestQuery, args?: TGetClientRequestArgsFullResponse) : Promise<TwitterResponse<T>>;
public async delete<T = any>(
url: string,
query: TRequestQuery = {},
{ fullResponse, prefix = this._prefix, ...rest }: TGetClientRequestArgs = {},
) : Promise<T | TwitterResponse<T>> {
if (prefix)
url = prefix + url;
const resp = await this._requestMaker.send<T>({
url,
method: 'DELETE',
query,
...rest,
});
return fullResponse ? resp : resp.data;
}
async post<T = any>(url: string, body?: TRequestBody, args?: TClientRequestArgsDataResponse) : Promise<T>;
async post<T = any>(url: string, body?: TRequestBody, args?: TClientRequestArgsFullResponse) : Promise<TwitterResponse<T>>;
public async post<T = any>(url: string, body?: TRequestBody, { fullResponse, prefix = this._prefix, ...rest }: TClientRequestArgs = {}) : Promise<T | TwitterResponse<T>> {
if (prefix)
url = prefix + url;
const resp = await this._requestMaker.send<T>({
url,
method: 'POST',
body,
...rest,
});
return fullResponse ? resp : resp.data;
}
async put<T = any>(url: string, body?: TRequestBody, args?: TClientRequestArgsDataResponse) : Promise<T>;
async put<T = any>(url: string, body?: TRequestBody, args?: TClientRequestArgsFullResponse) : Promise<TwitterResponse<T>>;
public async put<T = any>(url: string, body: TRequestBody, { fullResponse, prefix = this._prefix, ...rest }: TClientRequestArgs = {}) : Promise<T | TwitterResponse<T>> {
if (prefix)
url = prefix + url;
const resp = await this._requestMaker.send<T>({
url,
method: 'PUT',
body,
...rest,
});
return fullResponse ? resp : resp.data;
}
async patch<T = any>(url: string, body?: TRequestBody, args?: TClientRequestArgsDataResponse) : Promise<T>;
async patch<T = any>(url: string, body?: TRequestBody, args?: TClientRequestArgsFullResponse) : Promise<TwitterResponse<T>>;
public async patch<T = any>(url: string, body?: TRequestBody, { fullResponse, prefix = this._prefix, ...rest }: TClientRequestArgs = {}) : Promise<T | TwitterResponse<T>> {
if (prefix)
url = prefix + url;
const resp = await this._requestMaker.send<T>({
url,
method: 'PATCH',
body,
...rest,
});
return fullResponse ? resp : resp.data;
}
/** Stream request helpers */
getStream<T = any>(url: string, query: TRequestQuery | undefined, options: TStreamClientRequestArgsWithoutAutoConnect) : TweetStream<T>;
getStream<T = any>(url: string, query?: TRequestQuery, options?: TStreamClientRequestArgsWithAutoConnect) : Promise<TweetStream<T>>;
getStream<T = any>(url: string, query?: TRequestQuery, options?: TStreamClientRequestArgs) : Promise<TweetStream<T>> | TweetStream<T>;
public getStream<T = any>(url: string, query?: TRequestQuery, { prefix = this._prefix, ...rest }: TStreamClientRequestArgs = {}) : Promise<TweetStream<T>> | TweetStream<T> {
return this._requestMaker.sendStream<T>({
url: prefix ? prefix + url : url,
method: 'GET',
query,
...rest,
});
}
postStream<T = any>(url: string, body: TRequestBody | undefined, options: TStreamClientRequestArgsWithoutAutoConnect) : TweetStream<T>;
postStream<T = any>(url: string, body?: TRequestBody, options?: TStreamClientRequestArgsWithAutoConnect) : Promise<TweetStream<T>>;
postStream<T = any>(url: string, body?: TRequestBody, options?: TStreamClientRequestArgs) : Promise<TweetStream<T>> | TweetStream<T>;
public postStream<T = any>(url: string, body?: TRequestBody, { prefix = this._prefix, ...rest }: TStreamClientRequestArgs = {}) : Promise<TweetStream<T>> | TweetStream<T> {
return this._requestMaker.sendStream<T>({
url: prefix ? prefix + url : url,
method: 'POST',
body,
...rest,
});
}
}