rettiwt-api 4.2.0-alpha.1 → 4.2.0

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.
Files changed (70) hide show
  1. package/.eslintrc.js +35 -0
  2. package/.github/workflows/documentation.yml +1 -4
  3. package/.github/workflows/publish-alpha.yml +1 -4
  4. package/.github/workflows/publish.yml +1 -4
  5. package/.tool-versions +1 -1
  6. package/README.md +14 -8
  7. package/dist/Rettiwt.d.ts +3 -0
  8. package/dist/Rettiwt.js +2 -0
  9. package/dist/Rettiwt.js.map +1 -1
  10. package/dist/cli.js +2 -0
  11. package/dist/cli.js.map +1 -1
  12. package/dist/collections/Extractors.d.ts +3 -1
  13. package/dist/collections/Extractors.js +6 -0
  14. package/dist/collections/Extractors.js.map +1 -1
  15. package/dist/collections/Groups.js +2 -0
  16. package/dist/collections/Groups.js.map +1 -1
  17. package/dist/collections/Requests.js +3 -1
  18. package/dist/collections/Requests.js.map +1 -1
  19. package/dist/commands/List.d.ts +10 -0
  20. package/dist/commands/List.js +104 -0
  21. package/dist/commands/List.js.map +1 -0
  22. package/dist/commands/Tweet.js +2 -1
  23. package/dist/commands/Tweet.js.map +1 -1
  24. package/dist/commands/User.js +62 -39
  25. package/dist/commands/User.js.map +1 -1
  26. package/dist/enums/Resource.d.ts +2 -0
  27. package/dist/enums/Resource.js +2 -0
  28. package/dist/enums/Resource.js.map +1 -1
  29. package/dist/index.d.ts +2 -3
  30. package/dist/index.js +0 -3
  31. package/dist/index.js.map +1 -1
  32. package/dist/models/args/FetchArgs.d.ts +6 -0
  33. package/dist/models/args/FetchArgs.js +24 -2
  34. package/dist/models/args/FetchArgs.js.map +1 -1
  35. package/dist/models/data/Tweet.d.ts +18 -0
  36. package/dist/models/data/Tweet.js +62 -7
  37. package/dist/models/data/Tweet.js.map +1 -1
  38. package/dist/models/data/User.js +1 -1
  39. package/dist/models/data/User.js.map +1 -1
  40. package/dist/services/public/AuthService.js +1 -1
  41. package/dist/services/public/AuthService.js.map +1 -1
  42. package/dist/services/public/FetcherService.d.ts +5 -5
  43. package/dist/services/public/FetcherService.js +12 -12
  44. package/dist/services/public/FetcherService.js.map +1 -1
  45. package/dist/services/public/ListService.d.ts +71 -0
  46. package/dist/services/public/ListService.js +169 -0
  47. package/dist/services/public/ListService.js.map +1 -0
  48. package/dist/services/public/UserService.d.ts +26 -0
  49. package/dist/services/public/UserService.js +44 -0
  50. package/dist/services/public/UserService.js.map +1 -1
  51. package/package.json +3 -4
  52. package/src/Rettiwt.ts +5 -0
  53. package/src/cli.ts +2 -0
  54. package/src/collections/Extractors.ts +7 -1
  55. package/src/collections/Groups.ts +2 -0
  56. package/src/collections/Requests.ts +3 -1
  57. package/src/commands/List.ts +49 -0
  58. package/src/commands/Tweet.ts +2 -2
  59. package/src/commands/User.ts +13 -0
  60. package/src/enums/Resource.ts +2 -0
  61. package/src/index.ts +3 -1
  62. package/src/models/args/FetchArgs.ts +28 -1
  63. package/src/models/data/Notification.ts +1 -1
  64. package/src/models/data/Tweet.ts +75 -8
  65. package/src/models/data/User.ts +1 -1
  66. package/src/services/public/AuthService.ts +3 -1
  67. package/src/services/public/FetcherService.ts +17 -17
  68. package/src/services/public/ListService.ts +112 -0
  69. package/src/services/public/UserService.ts +42 -1
  70. package/.yarnrc.yml +0 -1
@@ -26,19 +26,19 @@ import { AuthService } from './AuthService';
26
26
  */
27
27
  export class FetcherService {
28
28
  /** The api key to use for authenticating against Twitter API as user. */
29
- private readonly apiKey?: string;
29
+ private readonly _apiKey?: string;
30
30
 
31
31
  /** The service used to handle HTTP and API errors */
32
- private readonly errorHandler: IErrorHandler;
32
+ private readonly _errorHandler: IErrorHandler;
33
33
 
34
34
  /** The guest key to use for authenticating against Twitter API as guest. */
35
- private readonly guestKey?: string;
35
+ private readonly _guestKey?: string;
36
36
 
37
37
  /** The URL To the proxy server to use for all others. */
38
- private readonly proxyUrl?: URL;
38
+ private readonly _proxyUrl?: URL;
39
39
 
40
40
  /** The max wait time for a response. */
41
- private readonly timeout: number;
41
+ private readonly _timeout: number;
42
42
 
43
43
  /** The URL to the proxy server to use only for authentication. */
44
44
  protected readonly authProxyUrl?: URL;
@@ -51,13 +51,13 @@ export class FetcherService {
51
51
  */
52
52
  public constructor(config?: IRettiwtConfig) {
53
53
  LogService.enabled = config?.logging ?? false;
54
- this.apiKey = config?.apiKey;
55
- this.guestKey = config?.guestKey;
54
+ this._apiKey = config?.apiKey;
55
+ this._guestKey = config?.guestKey;
56
56
  this.userId = config?.apiKey ? AuthService.getUserId(config.apiKey) : undefined;
57
57
  this.authProxyUrl = config?.authProxyUrl ?? config?.proxyUrl;
58
- this.proxyUrl = config?.proxyUrl;
59
- this.timeout = config?.timeout ?? 0;
60
- this.errorHandler = config?.errorHandler ?? new ErrorService();
58
+ this._proxyUrl = config?.proxyUrl;
59
+ this._timeout = config?.timeout ?? 0;
60
+ this._errorHandler = config?.errorHandler ?? new ErrorService();
61
61
  }
62
62
 
63
63
  /**
@@ -83,16 +83,16 @@ export class FetcherService {
83
83
  * @returns The generated AuthCredential
84
84
  */
85
85
  private async getCredential(): Promise<AuthCredential> {
86
- if (this.apiKey) {
86
+ if (this._apiKey) {
87
87
  // Logging
88
88
  LogService.log(ELogActions.GET, { target: 'USER_CREDENTIAL' });
89
89
 
90
- return new AuthCredential(AuthService.decodeCookie(this.apiKey).split(';'));
91
- } else if (this.guestKey) {
90
+ return new AuthCredential(AuthService.decodeCookie(this._apiKey).split(';'));
91
+ } else if (this._guestKey) {
92
92
  // Logging
93
93
  LogService.log(ELogActions.GET, { target: 'GUEST_CREDENTIAL' });
94
94
 
95
- return new AuthCredential(undefined, this.guestKey);
95
+ return new AuthCredential(undefined, this._guestKey);
96
96
  } else {
97
97
  // Logging
98
98
  LogService.log(ELogActions.GET, { target: 'NEW_GUEST_CREDENTIAL' });
@@ -183,7 +183,7 @@ export class FetcherService {
183
183
  args = this.validateArgs(resource, args)!;
184
184
 
185
185
  // Getting HTTPS agent
186
- const httpsAgent: Agent = this.getHttpsAgent(this.proxyUrl);
186
+ const httpsAgent: Agent = this.getHttpsAgent(this._proxyUrl);
187
187
 
188
188
  // Getting credentials from key
189
189
  const cred: AuthCredential = await this.getCredential();
@@ -195,7 +195,7 @@ export class FetcherService {
195
195
  config.headers = { ...config.headers, ...cred.toHeader() };
196
196
  config.httpAgent = httpsAgent;
197
197
  config.httpsAgent = httpsAgent;
198
- config.timeout = this.timeout;
198
+ config.timeout = this._timeout;
199
199
 
200
200
  // Sending the request
201
201
  try {
@@ -203,7 +203,7 @@ export class FetcherService {
203
203
  return (await axios<T>(config)).data;
204
204
  } catch (error) {
205
205
  // If error, delegate handling to error handler
206
- this.errorHandler.handle(error);
206
+ this._errorHandler.handle(error);
207
207
  throw error;
208
208
  }
209
209
  }
@@ -0,0 +1,112 @@
1
+ import { IListMembersResponse, IListTweetsResponse } from 'rettiwt-core';
2
+
3
+ import { extractors } from '../../collections/Extractors';
4
+ import { EResourceType } from '../../enums/Resource';
5
+ import { CursoredData } from '../../models/data/CursoredData';
6
+ import { Tweet } from '../../models/data/Tweet';
7
+ import { User } from '../../models/data/User';
8
+ import { IRettiwtConfig } from '../../types/RettiwtConfig';
9
+
10
+ import { FetcherService } from './FetcherService';
11
+
12
+ export class ListService extends FetcherService {
13
+ /**
14
+ * @param config - The config object for configuring the Rettiwt instance.
15
+ *
16
+ * @internal
17
+ */
18
+ public constructor(config?: IRettiwtConfig) {
19
+ super(config);
20
+ }
21
+
22
+ /**
23
+ * Get the list of members of a tweet list.
24
+ *
25
+ * @param id - The id of target list.
26
+ * @param count - The number of members to fetch, must be \<= 100.
27
+ * @param cursor - The cursor to the batch of members to fetch.
28
+ *
29
+ * @returns The list tweets in the given list.
30
+ *
31
+ * @example
32
+ * ```
33
+ * import { Rettiwt } from 'rettiwt-api';
34
+ *
35
+ * // Creating a new Rettiwt instance using the given 'API_KEY'
36
+ * const rettiwt = new Rettiwt({ apiKey: API_KEY });
37
+ *
38
+ * // Fetching the first 100 members of the Twitter list with id '1234567890'
39
+ * rettiwt.list.members('1234567890')
40
+ * .then(res => {
41
+ * console.log(res);
42
+ * })
43
+ * .catch(err => {
44
+ * console.log(err);
45
+ * });
46
+ * ```
47
+ *
48
+ * @remarks Due a bug in Twitter API, the count is ignored when no cursor is provided and defaults to 100.
49
+ */
50
+ public async members(id: string, count?: number, cursor?: string): Promise<CursoredData<User>> {
51
+ const resource: EResourceType = EResourceType.LIST_MEMBERS;
52
+
53
+ // Fetching the raw list of members
54
+ const response = await this.request<IListMembersResponse>(resource, {
55
+ id: id,
56
+ count: count,
57
+ cursor: cursor,
58
+ });
59
+
60
+ // Deserializing response
61
+ const data = extractors[resource](response);
62
+
63
+ return data;
64
+ }
65
+
66
+ /**
67
+ * Get the list of tweets from a tweet list.
68
+ *
69
+ * @param id - The id of target list.
70
+ * @param count - The number of tweets to fetch, must be \<= 100.
71
+ * @param cursor - The cursor to the batch of tweets to fetch.
72
+ *
73
+ * @returns The list tweets in the given list.
74
+ *
75
+ * @example
76
+ * ```
77
+ * import { Rettiwt } from 'rettiwt-api';
78
+ *
79
+ * // Creating a new Rettiwt instance using the given 'API_KEY'
80
+ * const rettiwt = new Rettiwt({ apiKey: API_KEY });
81
+ *
82
+ * // Fetching the most recent 100 tweets of the Twitter list with id '1234567890'
83
+ * rettiwt.list.tweets('1234567890')
84
+ * .then(res => {
85
+ * console.log(res);
86
+ * })
87
+ * .catch(err => {
88
+ * console.log(err);
89
+ * });
90
+ * ```
91
+ *
92
+ * @remarks Due a bug in Twitter API, the count is ignored when no cursor is provided and defaults to 100.
93
+ */
94
+ public async tweets(id: string, count?: number, cursor?: string): Promise<CursoredData<Tweet>> {
95
+ const resource = EResourceType.LIST_TWEETS;
96
+
97
+ // Fetching raw list tweets
98
+ const response = await this.request<IListTweetsResponse>(resource, {
99
+ id: id,
100
+ count: count,
101
+ cursor: cursor,
102
+ });
103
+
104
+ // Deserializing response
105
+ const data = extractors[resource](response);
106
+
107
+ // Sorting the tweets by date, from recent to oldest
108
+ data.list.sort((a, b) => new Date(b.createdAt).valueOf() - new Date(a.createdAt).valueOf());
109
+
110
+ return data;
111
+ }
112
+ }
@@ -1,4 +1,5 @@
1
1
  import {
2
+ IUserBookmarksResponse,
2
3
  IUserDetailsResponse,
3
4
  IUserFollowedResponse,
4
5
  IUserFollowersResponse,
@@ -7,7 +8,7 @@ import {
7
8
  IUserHighlightsResponse,
8
9
  IUserLikesResponse,
9
10
  IUserMediaResponse,
10
- IUserNotifications as IUserNotificationsResponse,
11
+ IUserNotificationsResponse,
11
12
  IUserRecommendedResponse,
12
13
  IUserSubscriptionsResponse,
13
14
  IUserTweetsAndRepliesResponse,
@@ -40,6 +41,46 @@ export class UserService extends FetcherService {
40
41
  super(config);
41
42
  }
42
43
 
44
+ /**
45
+ * Get the list of bookmarks of the logged in user.
46
+ *
47
+ * @param count - The number of bookmakrs to fetch, must be \<= 100.
48
+ * @param cursor - The cursor to the batch of bookmarks to fetch.
49
+ *
50
+ * @returns The list of tweets bookmarked by 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 most recent 100 liked Tweets of the logged in User
60
+ * rettiwt.user.bookmarks()
61
+ * .then(res => {
62
+ * console.log(res);
63
+ * })
64
+ * .catch(err => {
65
+ * console.log(err);
66
+ * });
67
+ * ```
68
+ */
69
+ public async bookmarks(count?: number, cursor?: string): Promise<CursoredData<Tweet>> {
70
+ const resource = EResourceType.USER_BOOKMARKS;
71
+
72
+ // Fetching raw list of likes
73
+ const response = await this.request<IUserBookmarksResponse>(resource, {
74
+ count: count,
75
+ cursor: cursor,
76
+ });
77
+
78
+ // Deserializing response
79
+ const data = extractors[resource](response);
80
+
81
+ return data;
82
+ }
83
+
43
84
  /**
44
85
  * Get the details of a user.
45
86
  *
package/.yarnrc.yml DELETED
@@ -1 +0,0 @@
1
- nodeLinker: node-modules