rettiwt-api 5.0.0-alpha.2 → 5.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collections/Extractors.d.ts +6 -2
- package/dist/collections/Extractors.js +1 -0
- package/dist/collections/Extractors.js.map +1 -1
- package/dist/collections/Groups.js +1 -0
- package/dist/collections/Groups.js.map +1 -1
- package/dist/collections/Requests.js +1 -0
- package/dist/collections/Requests.js.map +1 -1
- package/dist/commands/User.js +15 -0
- package/dist/commands/User.js.map +1 -1
- package/dist/enums/Api.d.ts +1 -51
- package/dist/enums/Api.js +1 -52
- package/dist/enums/Api.js.map +1 -1
- package/dist/enums/Resource.d.ts +1 -0
- package/dist/enums/Resource.js +1 -0
- package/dist/enums/Resource.js.map +1 -1
- package/dist/index.d.ts +5 -6
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/models/data/List.d.ts +4 -0
- package/dist/models/data/List.js +7 -0
- package/dist/models/data/List.js.map +1 -1
- package/dist/models/data/Notification.d.ts +4 -0
- package/dist/models/data/Notification.js +7 -0
- package/dist/models/data/Notification.js.map +1 -1
- package/dist/models/data/Tweet.d.ts +4 -0
- package/dist/models/data/Tweet.js +7 -0
- package/dist/models/data/Tweet.js.map +1 -1
- package/dist/models/data/User.d.ts +4 -0
- package/dist/models/data/User.js +7 -0
- package/dist/models/data/User.js.map +1 -1
- package/dist/models/errors/TwitterError.d.ts +37 -0
- package/dist/models/errors/TwitterError.js +60 -0
- package/dist/models/errors/TwitterError.js.map +1 -0
- package/dist/requests/List.js +28 -9
- package/dist/requests/List.js.map +1 -1
- package/dist/requests/Tweet.js +111 -34
- package/dist/requests/Tweet.js.map +1 -1
- package/dist/requests/User.js +231 -111
- package/dist/requests/User.js.map +1 -1
- package/dist/services/internal/AuthService.d.ts +5 -5
- package/dist/services/internal/AuthService.js +6 -7
- package/dist/services/internal/AuthService.js.map +1 -1
- package/dist/services/internal/ErrorService.d.ts +5 -76
- package/dist/services/internal/ErrorService.js +13 -116
- package/dist/services/internal/ErrorService.js.map +1 -1
- package/dist/services/internal/TidService.d.ts +4 -4
- package/dist/services/internal/TidService.js +8 -9
- package/dist/services/internal/TidService.js.map +1 -1
- package/dist/services/public/FetcherService.js +2 -2
- package/dist/services/public/FetcherService.js.map +1 -1
- package/dist/services/public/TweetService.js.map +1 -1
- package/dist/services/public/UserService.d.ts +29 -2
- package/dist/services/public/UserService.js +46 -4
- package/dist/services/public/UserService.js.map +1 -1
- package/dist/types/errors/TwitterError.d.ts +30 -0
- package/dist/types/{raw/generic/Error.js → errors/TwitterError.js} +1 -1
- package/dist/types/errors/TwitterError.js.map +1 -0
- package/dist/types/raw/base/Error.d.ts +32 -0
- package/dist/types/raw/base/Error.js +4 -0
- package/dist/types/raw/base/Error.js.map +1 -0
- package/dist/types/raw/generic/Response.d.ts +0 -2
- package/dist/types/raw/tweet/DetailsBulk.d.ts +288 -0
- package/dist/types/raw/tweet/DetailsBulk.js +4 -0
- package/dist/types/raw/tweet/DetailsBulk.js.map +1 -0
- package/dist/types/raw/user/DetailsBulk.d.ts +91 -0
- package/dist/types/raw/user/DetailsBulk.js +4 -0
- package/dist/types/raw/user/DetailsBulk.js.map +1 -0
- package/package.json +1 -1
- package/src/collections/Extractors.ts +8 -2
- package/src/collections/Groups.ts +1 -0
- package/src/collections/Requests.ts +1 -0
- package/src/commands/User.ts +15 -0
- package/src/enums/Api.ts +0 -51
- package/src/enums/Resource.ts +1 -0
- package/src/index.ts +5 -6
- package/src/models/data/List.ts +9 -0
- package/src/models/data/Notification.ts +10 -0
- package/src/models/data/Tweet.ts +9 -0
- package/src/models/data/User.ts +9 -0
- package/src/models/errors/TwitterError.ts +65 -0
- package/src/requests/List.ts +28 -9
- package/src/requests/Tweet.ts +111 -34
- package/src/requests/User.ts +231 -111
- package/src/services/internal/AuthService.ts +7 -8
- package/src/services/internal/ErrorService.ts +14 -132
- package/src/services/internal/TidService.ts +9 -10
- package/src/services/public/FetcherService.ts +2 -2
- package/src/services/public/TweetService.ts +3 -2
- package/src/services/public/UserService.ts +59 -8
- package/src/types/errors/TwitterError.ts +37 -0
- package/src/types/raw/base/Error.ts +37 -0
- package/src/types/raw/generic/Response.ts +0 -3
- package/src/types/raw/tweet/DetailsBulk.ts +338 -0
- package/src/types/raw/user/DetailsBulk.ts +104 -0
- package/dist/enums/Http.d.ts +0 -68
- package/dist/enums/Http.js +0 -73
- package/dist/enums/Http.js.map +0 -1
- package/dist/models/errors/ApiError.d.ts +0 -15
- package/dist/models/errors/ApiError.js +0 -23
- package/dist/models/errors/ApiError.js.map +0 -1
- package/dist/models/errors/HttpError.d.ts +0 -15
- package/dist/models/errors/HttpError.js +0 -23
- package/dist/models/errors/HttpError.js.map +0 -1
- package/dist/models/errors/RettiwtError.d.ts +0 -9
- package/dist/models/errors/RettiwtError.js +0 -17
- package/dist/models/errors/RettiwtError.js.map +0 -1
- package/dist/models/errors/TimeoutError.d.ts +0 -12
- package/dist/models/errors/TimeoutError.js +0 -19
- package/dist/models/errors/TimeoutError.js.map +0 -1
- package/dist/types/raw/generic/Error.d.ts +0 -30
- package/dist/types/raw/generic/Error.js.map +0 -1
- package/src/enums/Http.ts +0 -68
- package/src/models/errors/ApiError.ts +0 -21
- package/src/models/errors/HttpError.ts +0 -21
- package/src/models/errors/RettiwtError.ts +0 -13
- package/src/models/errors/TimeoutError.ts +0 -15
- package/src/types/raw/generic/Error.ts +0 -35
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Agent } from 'https';
|
|
2
|
-
|
|
3
1
|
import axios from 'axios';
|
|
4
2
|
|
|
5
3
|
import { EApiErrors } from '../../enums/Api';
|
|
6
4
|
import { AuthCredential } from '../../models/auth/AuthCredential';
|
|
5
|
+
import { RettiwtConfig } from '../../models/RettiwtConfig';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* The services that handles authentication.
|
|
@@ -11,14 +10,14 @@ import { AuthCredential } from '../../models/auth/AuthCredential';
|
|
|
11
10
|
* @public
|
|
12
11
|
*/
|
|
13
12
|
export class AuthService {
|
|
14
|
-
/** The
|
|
15
|
-
private readonly
|
|
13
|
+
/** The config object. */
|
|
14
|
+
private readonly _config: RettiwtConfig;
|
|
16
15
|
|
|
17
16
|
/**
|
|
18
|
-
* @param
|
|
17
|
+
* @param config - The config for Rettiwt.
|
|
19
18
|
*/
|
|
20
|
-
public constructor(
|
|
21
|
-
this.
|
|
19
|
+
public constructor(config: RettiwtConfig) {
|
|
20
|
+
this._config = config;
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
/**
|
|
@@ -107,7 +106,7 @@ export class AuthService {
|
|
|
107
106
|
/* eslint-enable @typescript-eslint/naming-convention */
|
|
108
107
|
}>('https://api.twitter.com/1.1/guest/activate.json', undefined, {
|
|
109
108
|
headers: cred.toHeader(),
|
|
110
|
-
httpsAgent: this.
|
|
109
|
+
httpsAgent: this._config.httpsAgent,
|
|
111
110
|
})
|
|
112
111
|
.then((res) => {
|
|
113
112
|
cred.guestToken = res.data.guest_token;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import { AxiosError,
|
|
1
|
+
import { AxiosError, isAxiosError } from 'axios';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { EHttpStatus } from '../../enums/Http';
|
|
5
|
-
import { findKeyByValue } from '../../helper/JsonUtils';
|
|
6
|
-
import { ApiError } from '../../models/errors/ApiError';
|
|
7
|
-
import { HttpError } from '../../models/errors/HttpError';
|
|
8
|
-
import { TimeoutError } from '../../models/errors/TimeoutError';
|
|
3
|
+
import { TwitterError } from '../../models/errors/TwitterError';
|
|
9
4
|
import { IErrorHandler } from '../../types/ErrorHandler';
|
|
5
|
+
import { IErrorData as IRawErrorData, IErrorDetails as IRawErrorDetails } from '../../types/raw/base/Error';
|
|
10
6
|
|
|
11
7
|
/**
|
|
12
8
|
* The base service that handles any errors.
|
|
@@ -15,129 +11,19 @@ import { IErrorHandler } from '../../types/ErrorHandler';
|
|
|
15
11
|
*/
|
|
16
12
|
export class ErrorService implements IErrorHandler {
|
|
17
13
|
/**
|
|
18
|
-
*
|
|
19
|
-
*/
|
|
20
|
-
protected static readonly DEFAULT_ERROR_MESSAGE = 'Unknown error';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Creates an API error instance based on the provided error code.
|
|
24
|
-
*
|
|
25
|
-
* @param errorCode - The error code.
|
|
26
|
-
*
|
|
27
|
-
* @returns An API error instance.
|
|
28
|
-
*/
|
|
29
|
-
protected createApiError(errorCode: number): ApiError {
|
|
30
|
-
return new ApiError(errorCode, this.getApiErrorMessage(errorCode));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Creates an HTTP error instance based on the provided HTTP status.
|
|
35
|
-
*
|
|
36
|
-
* @param httpStatus - The HTTP status code.
|
|
37
|
-
*
|
|
38
|
-
* @returns An HTTP error instance.
|
|
39
|
-
*/
|
|
40
|
-
protected createHttpError(httpStatus: number): HttpError {
|
|
41
|
-
return new HttpError(httpStatus, this.getHttpErrorMessage(httpStatus));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Retrieves the API error message based on the provided error code.
|
|
14
|
+
* Handles errors thrown by Twitter.
|
|
46
15
|
*
|
|
47
|
-
* @param
|
|
48
|
-
*
|
|
49
|
-
* @returns The API error message.
|
|
16
|
+
* @param error - The error response received from Twitter.
|
|
50
17
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return !!errorCodeKey && errorCodeKey in EApiErrors
|
|
55
|
-
? EApiErrors[errorCodeKey as keyof typeof EApiErrors]
|
|
56
|
-
: ErrorService.DEFAULT_ERROR_MESSAGE;
|
|
18
|
+
private handleAxiosError(error: AxiosError<IRawErrorData | IRawErrorDetails>): void {
|
|
19
|
+
throw new TwitterError(error);
|
|
57
20
|
}
|
|
58
21
|
|
|
59
22
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @param error - The error object.
|
|
63
|
-
*
|
|
64
|
-
* @returns The response data.
|
|
65
|
-
*
|
|
66
|
-
* @throws The original error if it is not an HTTP error with a response.
|
|
23
|
+
* Handle unknown error.
|
|
67
24
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return error.response;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
throw error;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Retrieves the API error code from the Axios response data.
|
|
78
|
-
*
|
|
79
|
-
* @param response - The response object received.
|
|
80
|
-
*
|
|
81
|
-
* @returns The error code, or undefined if not found.
|
|
82
|
-
*/
|
|
83
|
-
protected getErrorCode(response: AxiosResponse): number | undefined {
|
|
84
|
-
const errors = (response.data as { errors: { code: number }[] }).errors;
|
|
85
|
-
|
|
86
|
-
return !!errors && errors.length ? errors[0].code : undefined;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Retrieves the HTTP error message based on the provided HTTP status.
|
|
91
|
-
*
|
|
92
|
-
* @param httpStatus - The HTTP status code.
|
|
93
|
-
*
|
|
94
|
-
* @returns The HTTP error message.
|
|
95
|
-
*/
|
|
96
|
-
protected getHttpErrorMessage(httpStatus: number): string {
|
|
97
|
-
return Object.values(EHttpStatus).includes(httpStatus)
|
|
98
|
-
? EHttpStatus[httpStatus]
|
|
99
|
-
: ErrorService.DEFAULT_ERROR_MESSAGE;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Handles API error in a response.
|
|
104
|
-
*
|
|
105
|
-
* @param response - The response object received.
|
|
106
|
-
*
|
|
107
|
-
* @throws An error with the corresponding API error message if any API-related error has occurred.
|
|
108
|
-
*/
|
|
109
|
-
protected handleApiError(response: AxiosResponse): void {
|
|
110
|
-
const errorCode = this.getErrorCode(response);
|
|
111
|
-
|
|
112
|
-
if (errorCode === undefined) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
throw this.createApiError(errorCode);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Handles HTTP error in a response.
|
|
121
|
-
*
|
|
122
|
-
* @param response - The response object received.
|
|
123
|
-
*
|
|
124
|
-
* @throws An error with the corresponding HTTP status text if any HTTP-related error has occurred.
|
|
125
|
-
*/
|
|
126
|
-
protected handleHttpError(response: AxiosResponse): void {
|
|
127
|
-
throw this.createHttpError(response.status);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Handles exceeded timeout, configured in RettiwtConfig.
|
|
132
|
-
*
|
|
133
|
-
* @param error - The error object.
|
|
134
|
-
*
|
|
135
|
-
* @throws An error if the configured request timeout has been exceeded.
|
|
136
|
-
*/
|
|
137
|
-
protected handleTimeoutError(error: AxiosError): void {
|
|
138
|
-
if (error.code === 'ECONNABORTED') {
|
|
139
|
-
throw new TimeoutError(error.message);
|
|
140
|
-
}
|
|
25
|
+
private handleUnknownError(): void {
|
|
26
|
+
throw new Error('Unknown error');
|
|
141
27
|
}
|
|
142
28
|
|
|
143
29
|
/**
|
|
@@ -146,14 +32,10 @@ export class ErrorService implements IErrorHandler {
|
|
|
146
32
|
* @param error - The error caught while making HTTP request to Twitter API.
|
|
147
33
|
*/
|
|
148
34
|
public handle(error: unknown): void {
|
|
149
|
-
if (
|
|
150
|
-
|
|
35
|
+
if (isAxiosError(error)) {
|
|
36
|
+
this.handleAxiosError(error as AxiosError<IRawErrorData | IRawErrorDetails>);
|
|
37
|
+
} else {
|
|
38
|
+
this.handleUnknownError();
|
|
151
39
|
}
|
|
152
|
-
|
|
153
|
-
this.handleTimeoutError(error);
|
|
154
|
-
|
|
155
|
-
const axiosResponse = this.getAxiosResponse(error);
|
|
156
|
-
this.handleApiError(axiosResponse);
|
|
157
|
-
this.handleHttpError(axiosResponse);
|
|
158
40
|
}
|
|
159
41
|
}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Agent } from 'http';
|
|
2
|
-
|
|
3
1
|
import axios from 'axios';
|
|
4
2
|
import * as htmlParser from 'node-html-parser';
|
|
5
3
|
|
|
@@ -7,6 +5,7 @@ import { ELogActions } from '../../enums/Logging';
|
|
|
7
5
|
|
|
8
6
|
import { calculateClientTransactionIdHeader } from '../../helper/TidUtils';
|
|
9
7
|
|
|
8
|
+
import { RettiwtConfig } from '../../models/RettiwtConfig';
|
|
10
9
|
import { ITidDynamicArgs } from '../../types/auth/TidDynamicArgs';
|
|
11
10
|
import { ITidProvider } from '../../types/auth/TidProvider';
|
|
12
11
|
|
|
@@ -19,16 +18,16 @@ import { LogService } from './LogService';
|
|
|
19
18
|
*/
|
|
20
19
|
export class TidService implements ITidProvider {
|
|
21
20
|
private readonly _cdnUrl: string;
|
|
22
|
-
private readonly
|
|
21
|
+
private readonly _config: RettiwtConfig;
|
|
23
22
|
private readonly _requestHeaders: NonNullable<unknown>;
|
|
24
23
|
private _dynamicArgs?: ITidDynamicArgs;
|
|
25
24
|
|
|
26
25
|
/**
|
|
27
|
-
* @param
|
|
26
|
+
* @param config - The config for Rettiwt.
|
|
28
27
|
*/
|
|
29
|
-
public constructor(
|
|
28
|
+
public constructor(config: RettiwtConfig) {
|
|
30
29
|
this._cdnUrl = 'https://abs.twimg.com/responsive-web/client-web';
|
|
31
|
-
this.
|
|
30
|
+
this._config = config;
|
|
32
31
|
this._requestHeaders = {
|
|
33
32
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
34
33
|
|
|
@@ -71,8 +70,8 @@ export class TidService implements ITidProvider {
|
|
|
71
70
|
private async getHomepageHtml(): Promise<string> {
|
|
72
71
|
const response = await axios.get<string>('https://x.com', {
|
|
73
72
|
headers: this._requestHeaders,
|
|
74
|
-
httpAgent: this.
|
|
75
|
-
httpsAgent: this.
|
|
73
|
+
httpAgent: this._config.httpsAgent,
|
|
74
|
+
httpsAgent: this._config.httpsAgent,
|
|
76
75
|
});
|
|
77
76
|
|
|
78
77
|
return response.data;
|
|
@@ -88,8 +87,8 @@ export class TidService implements ITidProvider {
|
|
|
88
87
|
|
|
89
88
|
const onDemandFileHash = ondemandFileMatch ? ondemandFileMatch[1] : '';
|
|
90
89
|
const response = await axios.get<string>(`${this._cdnUrl}/ondemand.s.${onDemandFileHash}a.js`, {
|
|
91
|
-
httpAgent: this.
|
|
92
|
-
httpsAgent: this.
|
|
90
|
+
httpAgent: this._config.httpsAgent,
|
|
91
|
+
httpsAgent: this._config.httpsAgent,
|
|
93
92
|
});
|
|
94
93
|
const match = response.data.matchAll(/(\(\w\[(\d{1,2})],\s*16\))+?/gm);
|
|
95
94
|
|
|
@@ -53,9 +53,9 @@ export class FetcherService {
|
|
|
53
53
|
this.config = config;
|
|
54
54
|
this._delay = config.delay;
|
|
55
55
|
this._errorHandler = config.errorHandler ?? new ErrorService();
|
|
56
|
-
this._tidProvider = config.tidProvider ?? new TidService(config
|
|
56
|
+
this._tidProvider = config.tidProvider ?? new TidService(config);
|
|
57
57
|
this._timeout = config.timeout ?? 0;
|
|
58
|
-
this._auth = new AuthService(config
|
|
58
|
+
this._auth = new AuthService(config);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
@@ -11,8 +11,9 @@ import { ITweetFilter } from '../../types/args/FetchArgs';
|
|
|
11
11
|
import { INewTweet } from '../../types/args/PostArgs';
|
|
12
12
|
import { IListTweetsResponse } from '../../types/raw/list/Tweets';
|
|
13
13
|
import { IMediaInitializeUploadResponse } from '../../types/raw/media/InitalizeUpload';
|
|
14
|
-
import { ITweetDetailsResponse } from '../../types/raw/tweet/Details';
|
|
15
14
|
|
|
15
|
+
import { ITweetDetailsResponse } from '../../types/raw/tweet/Details';
|
|
16
|
+
import { ITweetDetailsBulkResponse } from '../../types/raw/tweet/DetailsBulk';
|
|
16
17
|
import { ITweetLikeResponse } from '../../types/raw/tweet/Like';
|
|
17
18
|
import { ITweetLikersResponse } from '../../types/raw/tweet/Likers';
|
|
18
19
|
import { ITweetPostResponse } from '../../types/raw/tweet/Post';
|
|
@@ -89,7 +90,7 @@ export class TweetService extends FetcherService {
|
|
|
89
90
|
resource = EResourceType.TWEET_DETAILS_BULK;
|
|
90
91
|
|
|
91
92
|
// Fetching raw tweet details
|
|
92
|
-
const response = await this.request<
|
|
93
|
+
const response = await this.request<ITweetDetailsBulkResponse>(resource, { ids: id });
|
|
93
94
|
|
|
94
95
|
// Deserializing response
|
|
95
96
|
const data = extractors[resource](response, id);
|
|
@@ -5,8 +5,10 @@ import { Notification } from '../../models/data/Notification';
|
|
|
5
5
|
import { Tweet } from '../../models/data/Tweet';
|
|
6
6
|
import { User } from '../../models/data/User';
|
|
7
7
|
import { RettiwtConfig } from '../../models/RettiwtConfig';
|
|
8
|
+
import { IUserAffiliatesResponse } from '../../types/raw/user/Affiliates';
|
|
8
9
|
import { IUserBookmarksResponse } from '../../types/raw/user/Bookmarks';
|
|
9
10
|
import { IUserDetailsResponse } from '../../types/raw/user/Details';
|
|
11
|
+
import { IUserDetailsBulkResponse } from '../../types/raw/user/DetailsBulk';
|
|
10
12
|
import { IUserFollowResponse } from '../../types/raw/user/Follow';
|
|
11
13
|
import { IUserFollowedResponse } from '../../types/raw/user/Followed';
|
|
12
14
|
import { IUserFollowersResponse } from '../../types/raw/user/Followers';
|
|
@@ -38,6 +40,48 @@ export class UserService extends FetcherService {
|
|
|
38
40
|
super(config);
|
|
39
41
|
}
|
|
40
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Get the list affiliates of a user.
|
|
45
|
+
*
|
|
46
|
+
* @param id - The id of the target user.
|
|
47
|
+
* @param count - The number of affiliates to fetch, must be \<= 100.
|
|
48
|
+
* @param cursor - The cursor to the batch of affiliates to fetch.
|
|
49
|
+
*
|
|
50
|
+
* @returns The list of users affiliated to the target user.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```
|
|
54
|
+
* import { Rettiwt } from 'rettiwt-api';
|
|
55
|
+
*
|
|
56
|
+
* // Creating a new Rettiwt instance using the given 'API_KEY'
|
|
57
|
+
* const rettiwt = new Rettiwt({ apiKey: API_KEY });
|
|
58
|
+
*
|
|
59
|
+
* // Fetching the first 100 affiliates of the User with id '1234567890'
|
|
60
|
+
* rettiwt.user.affiliates('1234567890')
|
|
61
|
+
* .then(res => {
|
|
62
|
+
* console.log(res);
|
|
63
|
+
* })
|
|
64
|
+
* .catch(err => {
|
|
65
|
+
* console.log(err);
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
public async affiliates(id?: string, count?: number, cursor?: string): Promise<CursoredData<User>> {
|
|
70
|
+
const resource = EResourceType.USER_AFFILIATES;
|
|
71
|
+
|
|
72
|
+
// Fetching raw list of affiliates
|
|
73
|
+
const response = await this.request<IUserAffiliatesResponse>(resource, {
|
|
74
|
+
id: id,
|
|
75
|
+
count: count,
|
|
76
|
+
cursor: cursor,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Deserializing response
|
|
80
|
+
const data = extractors[resource](response);
|
|
81
|
+
|
|
82
|
+
return data;
|
|
83
|
+
}
|
|
84
|
+
|
|
41
85
|
/**
|
|
42
86
|
* Get the list of bookmarks of the logged in user.
|
|
43
87
|
*
|
|
@@ -81,7 +125,7 @@ export class UserService extends FetcherService {
|
|
|
81
125
|
/**
|
|
82
126
|
* Get the details of a user.
|
|
83
127
|
*
|
|
84
|
-
* @param id - The username/id(s) of the target user/users.
|
|
128
|
+
* @param id - The username/id(s) of the target user/users. If no ID is provided, uses ID of authenticated user.
|
|
85
129
|
*
|
|
86
130
|
* @returns
|
|
87
131
|
* The details of the given user.
|
|
@@ -123,7 +167,9 @@ export class UserService extends FetcherService {
|
|
|
123
167
|
* });
|
|
124
168
|
* ```
|
|
125
169
|
*/
|
|
126
|
-
public async details<T extends string | string[]
|
|
170
|
+
public async details<T extends string | string[] | undefined>(
|
|
171
|
+
id: T,
|
|
172
|
+
): Promise<T extends string | undefined ? User | undefined : User[]> {
|
|
127
173
|
let resource: EResourceType;
|
|
128
174
|
|
|
129
175
|
// If details of multiple users required
|
|
@@ -131,31 +177,36 @@ export class UserService extends FetcherService {
|
|
|
131
177
|
resource = EResourceType.USER_DETAILS_BY_IDS_BULK;
|
|
132
178
|
|
|
133
179
|
// Fetching raw details
|
|
134
|
-
const response = await this.request<
|
|
180
|
+
const response = await this.request<IUserDetailsBulkResponse>(resource, { ids: id });
|
|
135
181
|
|
|
136
182
|
// Deserializing response
|
|
137
183
|
const data = extractors[resource](response, id);
|
|
138
184
|
|
|
139
|
-
return data as T extends string ? User | undefined : User[];
|
|
185
|
+
return data as T extends string | undefined ? User | undefined : User[];
|
|
140
186
|
}
|
|
141
187
|
// If details of single user required
|
|
142
188
|
else {
|
|
143
189
|
// If username is given
|
|
144
|
-
if (isNaN(Number(id))) {
|
|
190
|
+
if (id && isNaN(Number(id))) {
|
|
145
191
|
resource = EResourceType.USER_DETAILS_BY_USERNAME;
|
|
146
192
|
}
|
|
147
|
-
// If id is given
|
|
193
|
+
// If id is given (or not, for self details)
|
|
148
194
|
else {
|
|
149
195
|
resource = EResourceType.USER_DETAILS_BY_ID;
|
|
150
196
|
}
|
|
151
197
|
|
|
198
|
+
// If no ID is given, and not authenticated, skip
|
|
199
|
+
if (!id && !this.config.userId) {
|
|
200
|
+
return undefined as T extends string | undefined ? User | undefined : User[];
|
|
201
|
+
}
|
|
202
|
+
|
|
152
203
|
// Fetching raw details
|
|
153
|
-
const response = await this.request<IUserDetailsResponse>(resource, { id: id });
|
|
204
|
+
const response = await this.request<IUserDetailsResponse>(resource, { id: id ?? this.config.userId });
|
|
154
205
|
|
|
155
206
|
// Deserializing response
|
|
156
207
|
const data = extractors[resource](response);
|
|
157
208
|
|
|
158
|
-
return data as T extends string ? User | undefined : User[];
|
|
209
|
+
return data as T extends string | undefined ? User | undefined : User[];
|
|
159
210
|
}
|
|
160
211
|
}
|
|
161
212
|
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error thrown by Twitter API.
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export interface ITwitterError extends Error {
|
|
7
|
+
/** The details of each error. */
|
|
8
|
+
details: ITwitterErrorDetails[];
|
|
9
|
+
|
|
10
|
+
/** The error message in the response. */
|
|
11
|
+
message: string;
|
|
12
|
+
|
|
13
|
+
/** The name of the error response. */
|
|
14
|
+
name: string;
|
|
15
|
+
|
|
16
|
+
/** The response status code. */
|
|
17
|
+
status: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The error details.
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export interface ITwitterErrorDetails {
|
|
26
|
+
/** The internal error code. */
|
|
27
|
+
code: number;
|
|
28
|
+
|
|
29
|
+
/** The message associated with the error. */
|
|
30
|
+
message: string;
|
|
31
|
+
|
|
32
|
+
/** The name of the error. */
|
|
33
|
+
name?: string;
|
|
34
|
+
|
|
35
|
+
/** The type of error. */
|
|
36
|
+
type?: string;
|
|
37
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Error details for multiple errors.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export interface IErrorData {
|
|
9
|
+
errors: IErrorDetails[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Error details of a single error.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export interface IErrorDetails {
|
|
18
|
+
message: string;
|
|
19
|
+
extensions?: IErrorExtensions;
|
|
20
|
+
code: number;
|
|
21
|
+
kind?: string;
|
|
22
|
+
name?: string;
|
|
23
|
+
source?: string;
|
|
24
|
+
tracing?: IErrorTracing;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface IErrorExtensions {
|
|
28
|
+
name: string;
|
|
29
|
+
source: string;
|
|
30
|
+
code: number;
|
|
31
|
+
kind: string;
|
|
32
|
+
tracing: IErrorTracing;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface IErrorTracing {
|
|
36
|
+
trace_id: string;
|
|
37
|
+
}
|