rettiwt-api 1.1.2 → 1.1.7

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 (301) hide show
  1. package/README.md +32 -7
  2. package/dist/endpoints/Endpoints.d.ts +14 -0
  3. package/dist/endpoints/Endpoints.js +20 -0
  4. package/dist/endpoints/Endpoints.js.map +1 -0
  5. package/dist/endpoints/Url.d.ts +5 -0
  6. package/dist/endpoints/Url.js +12 -0
  7. package/dist/endpoints/Url.js.map +1 -0
  8. package/dist/enums/Endpoints.d.ts +25 -0
  9. package/dist/enums/Endpoints.js +31 -0
  10. package/dist/enums/Endpoints.js.map +1 -0
  11. package/dist/enums/Errors.d.ts +20 -0
  12. package/dist/enums/Errors.js +28 -0
  13. package/dist/enums/Errors.js.map +1 -0
  14. package/dist/enums/HTTP.d.ts +17 -0
  15. package/dist/enums/HTTP.js +23 -0
  16. package/dist/enums/HTTP.js.map +1 -0
  17. package/dist/graphql/enums/Errors.d.ts +21 -0
  18. package/dist/graphql/enums/Errors.js +29 -0
  19. package/dist/graphql/enums/Errors.js.map +1 -0
  20. package/dist/graphql/queries/RootQuery.js +10 -3
  21. package/dist/graphql/queries/RootQuery.js.map +1 -1
  22. package/dist/graphql/resolvers/AccountResolver.d.ts +3 -3
  23. package/dist/graphql/resolvers/AccountResolver.js.map +1 -1
  24. package/dist/graphql/resolvers/ResolverBase.d.ts +14 -3
  25. package/dist/graphql/resolvers/ResolverBase.js +13 -1
  26. package/dist/graphql/resolvers/ResolverBase.js.map +1 -1
  27. package/dist/graphql/resolvers/TweetResolver.d.ts +3 -3
  28. package/dist/graphql/resolvers/TweetResolver.js +30 -18
  29. package/dist/graphql/resolvers/TweetResolver.js.map +1 -1
  30. package/dist/graphql/resolvers/UserResolver.d.ts +13 -4
  31. package/dist/graphql/resolvers/UserResolver.js +83 -18
  32. package/dist/graphql/resolvers/UserResolver.js.map +1 -1
  33. package/dist/graphql/types/Errors.d.ts +20 -0
  34. package/dist/graphql/types/Errors.js +28 -0
  35. package/dist/graphql/types/Errors.js.map +1 -0
  36. package/dist/graphql/types/TweetTypes.js +2 -2
  37. package/dist/graphql/types/TweetTypes.js.map +1 -1
  38. package/dist/graphql/types/UserTypes.js +1 -1
  39. package/dist/graphql/types/UserTypes.js.map +1 -1
  40. package/dist/index.d.ts +31 -15
  41. package/dist/index.js +32 -17
  42. package/dist/index.js.map +1 -1
  43. package/dist/middlewares/Authentication.d.ts +0 -0
  44. package/dist/middlewares/Authentication.js +2 -0
  45. package/dist/middlewares/Authentication.js.map +1 -0
  46. package/dist/models/args/TweetFilter.d.ts +59 -0
  47. package/dist/models/args/TweetFilter.js +101 -0
  48. package/dist/models/args/TweetFilter.js.map +1 -0
  49. package/dist/models/args/TweetListArgs.d.ts +21 -0
  50. package/dist/models/args/TweetListArgs.js +54 -0
  51. package/dist/models/args/TweetListArgs.js.map +1 -0
  52. package/dist/models/args/UserListArgs.d.ts +21 -0
  53. package/dist/models/args/UserListArgs.js +54 -0
  54. package/dist/models/args/UserListArgs.js.map +1 -0
  55. package/dist/models/auth/AuthCookie.d.ts +21 -0
  56. package/dist/models/auth/AuthCookie.js +33 -0
  57. package/dist/models/auth/AuthCookie.js.map +1 -0
  58. package/dist/models/data/CursoredData.d.ts +34 -0
  59. package/dist/models/data/CursoredData.js +42 -0
  60. package/dist/models/data/CursoredData.js.map +1 -0
  61. package/dist/models/data/DataValidationError.d.ts +18 -0
  62. package/dist/models/data/DataValidationError.js +21 -0
  63. package/dist/models/data/DataValidationError.js.map +1 -0
  64. package/dist/models/data/Errors.d.ts +18 -0
  65. package/dist/models/data/Errors.js +21 -0
  66. package/dist/models/data/Errors.js.map +1 -0
  67. package/dist/models/data/Service.d.ts +33 -0
  68. package/dist/models/data/Service.js +41 -0
  69. package/dist/models/data/Service.js.map +1 -0
  70. package/dist/models/data/Tweet.d.ts +53 -0
  71. package/dist/models/data/Tweet.js +104 -0
  72. package/dist/models/data/Tweet.js.map +1 -0
  73. package/dist/models/data/User.d.ts +41 -0
  74. package/dist/models/data/User.js +32 -0
  75. package/dist/models/data/User.js.map +1 -0
  76. package/dist/models/errors/DataValidationError.d.ts +20 -0
  77. package/dist/models/errors/DataValidationError.js +23 -0
  78. package/dist/models/errors/DataValidationError.js.map +1 -0
  79. package/dist/models/query/Variables.d.ts +2 -0
  80. package/dist/models/query/Variables.js +10 -0
  81. package/dist/models/query/Variables.js.map +1 -0
  82. package/dist/requests/Url.d.ts +5 -0
  83. package/dist/requests/Url.js +12 -0
  84. package/dist/requests/Url.js.map +1 -0
  85. package/dist/requests/payloads/Variables.d.ts +23 -0
  86. package/dist/requests/payloads/Variables.js +24 -0
  87. package/dist/requests/payloads/Variables.js.map +1 -0
  88. package/dist/server.d.ts +1 -1
  89. package/dist/server.js +9 -10
  90. package/dist/server.js.map +1 -1
  91. package/dist/services/accounts/AccountService.d.ts +6 -0
  92. package/dist/services/accounts/AccountService.js +6 -0
  93. package/dist/services/accounts/AccountService.js.map +1 -1
  94. package/dist/services/auth/AccountService.d.ts +88 -0
  95. package/dist/services/auth/AccountService.js +392 -0
  96. package/dist/services/auth/AccountService.js.map +1 -0
  97. package/dist/services/auth/AuthService.d.ts +31 -0
  98. package/dist/services/auth/AuthService.js +118 -0
  99. package/dist/services/auth/AuthService.js.map +1 -0
  100. package/dist/services/auth/LoginFlows.d.ts +77 -0
  101. package/dist/services/auth/LoginFlows.js +92 -0
  102. package/dist/services/auth/LoginFlows.js.map +1 -0
  103. package/dist/services/data/TweetService.d.ts +46 -16
  104. package/dist/services/data/TweetService.js +71 -54
  105. package/dist/services/data/TweetService.js.map +1 -1
  106. package/dist/services/data/UserService.d.ts +58 -19
  107. package/dist/services/data/UserService.js +104 -68
  108. package/dist/services/data/UserService.js.map +1 -1
  109. package/dist/services/helper/Headers.d.ts +4 -4
  110. package/dist/services/helper/Headers.js.map +1 -1
  111. package/dist/services/helper/Parser.d.ts +2 -2
  112. package/dist/services/helper/Parser.js.map +1 -1
  113. package/dist/services/helper/extractors/Tweets.d.ts +6 -6
  114. package/dist/services/helper/extractors/Tweets.js +1 -1
  115. package/dist/services/helper/extractors/Tweets.js.map +1 -1
  116. package/dist/services/helper/extractors/Users.d.ts +10 -4
  117. package/dist/services/helper/extractors/Users.js +54 -2
  118. package/dist/services/helper/extractors/Users.js.map +1 -1
  119. package/dist/services/helper/payloads/LoginFlows.d.ts +77 -0
  120. package/dist/services/helper/payloads/LoginFlows.js +92 -0
  121. package/dist/services/helper/payloads/LoginFlows.js.map +1 -0
  122. package/dist/services/helper/urls/Users.d.ts +8 -1
  123. package/dist/services/helper/urls/Users.js +12 -2
  124. package/dist/services/helper/urls/Users.js.map +1 -1
  125. package/dist/services/util/CacheService.d.ts +33 -0
  126. package/dist/services/util/CacheService.js +96 -0
  127. package/dist/services/util/CacheService.js.map +1 -0
  128. package/dist/services/util/FetcherService.d.ts +65 -0
  129. package/dist/services/util/FetcherService.js +214 -0
  130. package/dist/services/util/FetcherService.js.map +1 -0
  131. package/dist/types/Args.d.ts +38 -0
  132. package/dist/types/Args.js +5 -0
  133. package/dist/types/Args.js.map +1 -0
  134. package/dist/types/Authentication.d.ts +17 -2
  135. package/dist/types/Authentication.js +1 -0
  136. package/dist/types/Authentication.js.map +1 -1
  137. package/dist/types/Query.d.ts +80 -0
  138. package/dist/types/Query.js +3 -0
  139. package/dist/types/Query.js.map +1 -0
  140. package/dist/types/Resolvers.d.ts +5 -4
  141. package/dist/types/Rettiwt.d.ts +16 -0
  142. package/dist/types/Rettiwt.js +3 -0
  143. package/dist/types/Rettiwt.js.map +1 -0
  144. package/dist/types/Service.d.ts +15 -23
  145. package/dist/types/Service.js +2 -16
  146. package/dist/types/Service.js.map +1 -1
  147. package/dist/types/Tweet.d.ts +25 -19
  148. package/dist/types/Tweet.js +0 -2
  149. package/dist/types/Tweet.js.map +1 -1
  150. package/dist/types/User.d.ts +35 -0
  151. package/dist/types/User.js +3 -0
  152. package/dist/types/User.js.map +1 -0
  153. package/dist/types/args/TweetFilter.d.ts +54 -0
  154. package/dist/types/args/TweetFilter.js +96 -0
  155. package/dist/types/args/TweetFilter.js.map +1 -0
  156. package/dist/types/args/TweetListArg.d.ts +10 -0
  157. package/dist/types/args/TweetListArg.js +42 -0
  158. package/dist/types/args/TweetListArg.js.map +1 -0
  159. package/dist/types/args/TweetListArgs.d.ts +20 -0
  160. package/dist/types/args/TweetListArgs.js +52 -0
  161. package/dist/types/args/TweetListArgs.js.map +1 -0
  162. package/dist/types/args/UserListArgs.d.ts +16 -0
  163. package/dist/types/args/UserListArgs.js +48 -0
  164. package/dist/types/args/UserListArgs.js.map +1 -0
  165. package/dist/types/data/TweetFilter.d.ts +49 -0
  166. package/dist/types/data/TweetFilter.js +63 -0
  167. package/dist/types/data/TweetFilter.js.map +1 -0
  168. package/dist/types/interfaces/Args.d.ts +38 -0
  169. package/dist/types/interfaces/Args.js +5 -0
  170. package/dist/types/interfaces/Args.js.map +1 -0
  171. package/dist/types/interfaces/Authentication.d.ts +40 -0
  172. package/dist/types/interfaces/Authentication.js +5 -0
  173. package/dist/types/interfaces/Authentication.js.map +1 -0
  174. package/dist/types/interfaces/Resolvers.d.ts +14 -0
  175. package/dist/types/interfaces/Resolvers.js +3 -0
  176. package/dist/types/interfaces/Resolvers.js.map +1 -0
  177. package/dist/types/interfaces/Rettiwt.d.ts +16 -0
  178. package/dist/types/interfaces/Rettiwt.js +3 -0
  179. package/dist/types/interfaces/Rettiwt.js.map +1 -0
  180. package/dist/types/interfaces/Service.d.ts +13 -0
  181. package/dist/types/interfaces/Service.js +3 -0
  182. package/dist/types/interfaces/Service.js.map +1 -0
  183. package/dist/types/interfaces/Services.d.ts +13 -0
  184. package/dist/types/interfaces/Services.js +3 -0
  185. package/dist/types/interfaces/Services.js.map +1 -0
  186. package/dist/types/interfaces/Tweet.d.ts +46 -0
  187. package/dist/types/interfaces/Tweet.js +3 -0
  188. package/dist/types/interfaces/Tweet.js.map +1 -0
  189. package/dist/types/interfaces/TweetFilter.d.ts +0 -0
  190. package/dist/types/interfaces/TweetFilter.js +2 -0
  191. package/dist/types/interfaces/TweetFilter.js.map +1 -0
  192. package/{src/types/data/User.ts → dist/types/interfaces/User.d.ts} +35 -51
  193. package/dist/types/interfaces/User.js +3 -0
  194. package/dist/types/interfaces/User.js.map +1 -0
  195. package/dist/types/raw/data/tweet/Favouriters.d.ts +164 -0
  196. package/dist/types/raw/data/tweet/Favouriters.js +3 -0
  197. package/dist/types/raw/data/tweet/Favouriters.js.map +1 -0
  198. package/dist/types/raw/data/tweet/Retweeters.d.ts +171 -0
  199. package/dist/types/raw/data/tweet/Retweeters.js +3 -0
  200. package/dist/types/raw/data/tweet/Retweeters.js.map +1 -0
  201. package/dist/types/raw/data/tweet/Tweet.d.ts +746 -0
  202. package/dist/types/raw/data/tweet/Tweet.js +3 -0
  203. package/dist/types/raw/data/tweet/Tweet.js.map +1 -0
  204. package/dist/types/raw/data/tweet/Tweets.d.ts +386 -0
  205. package/dist/types/raw/data/tweet/Tweets.js +3 -0
  206. package/dist/types/raw/data/tweet/Tweets.js.map +1 -0
  207. package/dist/types/raw/data/user/Followers.d.ts +176 -0
  208. package/dist/types/raw/data/user/Followers.js +3 -0
  209. package/dist/types/raw/data/user/Followers.js.map +1 -0
  210. package/dist/types/raw/data/user/Following.d.ts +176 -0
  211. package/dist/types/raw/data/user/Following.js +3 -0
  212. package/dist/types/raw/data/user/Following.js.map +1 -0
  213. package/dist/types/raw/data/user/Likes.d.ts +1059 -0
  214. package/dist/types/raw/data/user/Likes.js +3 -0
  215. package/dist/types/raw/data/user/Likes.js.map +1 -0
  216. package/dist/types/raw/data/user/User.d.ts +117 -0
  217. package/dist/types/raw/data/user/User.js +3 -0
  218. package/dist/types/raw/data/user/User.js.map +1 -0
  219. package/dist/types/raw/query/tweet/Details.d.ts +80 -0
  220. package/dist/types/raw/query/tweet/Details.js +5 -0
  221. package/dist/types/raw/query/tweet/Details.js.map +1 -0
  222. package/dist/types/raw/query/tweet/Engagements.d.ts +29 -0
  223. package/dist/types/raw/query/tweet/Engagements.js +3 -0
  224. package/dist/types/raw/query/tweet/Engagements.js.map +1 -0
  225. package/dist/types/raw/query/tweet/Likes.d.ts +29 -0
  226. package/dist/types/raw/query/tweet/Likes.js +3 -0
  227. package/dist/types/raw/query/tweet/Likes.js.map +1 -0
  228. package/dist/types/raw/query/tweet/Retweets.d.ts +29 -0
  229. package/dist/types/raw/query/tweet/Retweets.js +3 -0
  230. package/dist/types/raw/query/tweet/Retweets.js.map +1 -0
  231. package/dist/types/raw/query/tweet/Search.d.ts +40 -0
  232. package/dist/types/raw/query/tweet/Search.js +3 -0
  233. package/dist/types/raw/query/tweet/Search.js.map +1 -0
  234. package/dist/types/raw/query/tweet/TweetDetails.d.ts +34 -0
  235. package/dist/types/raw/query/tweet/TweetDetails.js +5 -0
  236. package/dist/types/raw/query/tweet/TweetDetails.js.map +1 -0
  237. package/dist/types/raw/query/tweet/TweetLike.d.ts +29 -0
  238. package/dist/types/raw/query/tweet/TweetLike.js +3 -0
  239. package/dist/types/raw/query/tweet/TweetLike.js.map +1 -0
  240. package/dist/types/raw/query/tweet/TweetLikes.d.ts +29 -0
  241. package/dist/types/raw/query/tweet/TweetLikes.js +3 -0
  242. package/dist/types/raw/query/tweet/TweetLikes.js.map +1 -0
  243. package/dist/types/raw/query/tweet/TweetRetweets.d.ts +0 -0
  244. package/dist/types/raw/query/tweet/TweetRetweets.js +2 -0
  245. package/dist/types/raw/query/tweet/TweetRetweets.js.map +1 -0
  246. package/dist/types/raw/query/user/Details.d.ts +34 -0
  247. package/dist/types/raw/query/user/Details.js +3 -0
  248. package/dist/types/raw/query/user/Details.js.map +1 -0
  249. package/dist/types/raw/user/Tweets.d.ts +668 -1584
  250. package/dist/types/services/args/TweetFilter.d.ts +50 -0
  251. package/dist/types/services/args/TweetFilter.js +76 -0
  252. package/dist/types/services/args/TweetFilter.js.map +1 -0
  253. package/package.json +6 -3
  254. package/src/enums/Errors.ts +21 -0
  255. package/src/graphql/enums/Errors.ts +22 -0
  256. package/src/graphql/queries/RootQuery.ts +11 -4
  257. package/src/graphql/resolvers/AccountResolver.ts +3 -3
  258. package/src/graphql/resolvers/ResolverBase.ts +19 -5
  259. package/src/graphql/resolvers/TweetResolver.ts +26 -17
  260. package/src/graphql/resolvers/UserResolver.ts +75 -20
  261. package/src/graphql/types/TweetTypes.ts +2 -2
  262. package/src/graphql/types/UserTypes.ts +4 -4
  263. package/src/index.ts +39 -17
  264. package/src/models/args/TweetFilter.ts +119 -0
  265. package/src/models/args/TweetListArgs.ts +47 -0
  266. package/src/models/args/UserListArgs.ts +47 -0
  267. package/src/models/auth/AuthCookie.ts +43 -0
  268. package/src/models/data/CursoredData.ts +45 -0
  269. package/src/models/data/Tweet.ts +118 -0
  270. package/src/models/data/User.ts +72 -0
  271. package/src/models/errors/DataValidationError.ts +29 -0
  272. package/src/server.ts +9 -10
  273. package/src/services/{accounts → auth}/AccountService.ts +71 -19
  274. package/src/services/auth/AuthService.ts +81 -0
  275. package/src/services/data/TweetService.ts +81 -62
  276. package/src/services/data/UserService.ts +118 -80
  277. package/src/services/helper/Headers.ts +4 -4
  278. package/src/services/helper/Parser.ts +2 -2
  279. package/src/services/helper/extractors/Tweets.ts +7 -7
  280. package/src/services/helper/extractors/Users.ts +60 -5
  281. package/src/services/helper/urls/Users.ts +39 -23
  282. package/src/services/{CacheService.ts → util/CacheService.ts} +4 -1
  283. package/src/services/{FetcherService.ts → util/FetcherService.ts} +19 -11
  284. package/src/types/Args.ts +49 -0
  285. package/src/types/Authentication.ts +29 -7
  286. package/src/types/Resolvers.ts +5 -4
  287. package/src/types/Rettiwt.ts +20 -0
  288. package/src/types/Service.ts +24 -0
  289. package/src/types/Tweet.ts +61 -0
  290. package/src/types/User.ts +48 -0
  291. package/src/types/raw/user/Tweets.ts +1747 -0
  292. package/tsconfig.json +2 -2
  293. package/src/services/AuthService.ts +0 -68
  294. package/src/services/helper/deserializers/Tweets.ts +0 -70
  295. package/src/services/helper/deserializers/Users.ts +0 -26
  296. package/src/types/data/Errors.ts +0 -37
  297. package/src/types/data/Service.ts +0 -55
  298. package/src/types/data/Tweet.ts +0 -123
  299. package/src/types/raw/auth/Cookie.ts +0 -16
  300. /package/src/{types → enums}/HTTP.ts +0 -0
  301. /package/src/services/{accounts → helper/payloads}/LoginFlows.ts +0 -0
@@ -2,15 +2,17 @@
2
2
  import { curly, CurlyResult } from 'node-libcurl';
3
3
 
4
4
  // SERVICES
5
- import { AuthService } from '../AuthService';
5
+ import { AuthService } from './AuthService';
6
6
 
7
7
  // TYPES
8
- import { GuestCredentials } from '../../types/Authentication';
9
- import { HttpStatus } from '../../types/HTTP';
10
- import { AuthenticationErrors } from '../../types/data/Errors';
8
+ import { IGuestCredentials, IAuthCookie } from '../../types/Authentication';
9
+
10
+ // ENUMS
11
+ import { HttpStatus } from '../../enums/HTTP';
12
+ import { AuthenticationErrors } from '../../enums/Errors';
11
13
 
12
14
  // HELPERS
13
- import LoginFlows from './LoginFlows';
15
+ import LoginFlows from '../helper/payloads/LoginFlows';
14
16
  import { loginHeader } from '../helper/Headers';
15
17
  import { Cookie, CookieJar } from 'cookiejar';
16
18
 
@@ -19,12 +21,11 @@ import { Cookie, CookieJar } from 'cookiejar';
19
21
  * @public
20
22
  */
21
23
  export class AccountService {
22
- // MEMBER DATA
23
24
  /** The AuthService instance to use for authentication. */
24
25
  private auth: AuthService;
25
26
 
26
27
  /** The current guest credentials to use. */
27
- private guestCreds: GuestCredentials;
28
+ private guestCreds: IGuestCredentials;
28
29
 
29
30
  /** The cookies received from Twitter after logging in. */
30
31
  private cookies: Cookie[];
@@ -32,7 +33,6 @@ export class AccountService {
32
33
  /** The flow token received after execution of current flow. */
33
34
  private flowToken: string;
34
35
 
35
- // MEMBER METHODS
36
36
  constructor() {
37
37
  this.auth = new AuthService();
38
38
  this.guestCreds = { authToken: '', guestToken: '' };
@@ -43,7 +43,7 @@ export class AccountService {
43
43
  /**
44
44
  * @returns The current guest credentials to use. If if does not exists, then a new one is created
45
45
  */
46
- private async getGuestCredentials(): Promise<GuestCredentials> {
46
+ private async getGuestCredentials(): Promise<IGuestCredentials> {
47
47
  // If a guest credential has not been already set, get a new one
48
48
  if (!this.guestCreds.guestToken) {
49
49
  this.guestCreds = await this.auth.getGuestCredentials();
@@ -176,15 +176,15 @@ export class AccountService {
176
176
  }
177
177
 
178
178
  /**
179
- * Login to Twitter using the given credentials and get back the cookies.
180
- * @public
179
+ * Execute all the flows required to login to Twitter, using the given credentials, then set the response cookies.
180
+ *
181
+ * @internal
181
182
  *
182
- * @param email The email of the account to be logged into
183
- * @param userName The username associated with the given account
184
- * @param password The password to the account
185
- * @returns The cookies for authenticating with the given account
183
+ * @param email The email of the account to be logged into.
184
+ * @param userName The username associated with the given account.
185
+ * @param password The password to the account.
186
186
  */
187
- public async login(email: string, userName: string, password: string): Promise<string> {
187
+ private async executeLoginFlows(email: string, userName: string, password: string): Promise<void> {
188
188
  /**
189
189
  * This works by sending a chain of request that are required for login to twitter.
190
190
  * Each method in the chain returns a flow token that must be provied as payload in the next method in the chain.
@@ -197,9 +197,61 @@ export class AccountService {
197
197
  await this.enterUserIdentifier(email);
198
198
  await this.enterAlternateUserIdentifier(userName);
199
199
  await this.enterPassword(password);
200
- await this.accountDuplicationCheck();
200
+ await this.accountDuplicationCheck();
201
+ }
202
+
203
+ /**
204
+ * Parse the authentication cookies recieved from Twitter into known format.
205
+ *
206
+ * @internal
207
+ *
208
+ * @param cookies The raw cookies received from Twitter.
209
+ *
210
+ * @returns The parsed cookies of type {@link AuthCookie}
211
+ */
212
+ private parseCookies(cookies: Cookie[]): IAuthCookie {
213
+ /** The tempoorary parsed cookies. */
214
+ let tempCookies: any = {};
215
+
216
+ /**
217
+ * Parsing the cookies into a standard JSON format.
218
+ * The format is 'cookie_name': 'cookie_value'.
219
+ * All other cookie parameters like expiry, etc are dropped.
220
+ */
221
+ cookies.forEach(cookie => {
222
+ tempCookies[cookie.name] = cookie.value;
223
+ });
224
+
225
+ return {
226
+ kdt: tempCookies['kdt'],
227
+ twid: tempCookies['twid'],
228
+ ct0: tempCookies['ct0'],
229
+ auth_token: tempCookies['auth_token']
230
+ };
231
+ }
232
+
233
+ /**
234
+ * Login to Twitter using the given credentials and get back the cookies.
235
+ *
236
+ * @public
237
+ *
238
+ * @param email The email of the account to be logged into.
239
+ * @param userName The username associated with the given account.
240
+ * @param password The password to the account.
241
+ *
242
+ * @returns The cookies for authenticating with the given account.
243
+ */
244
+ public async login(email: string, userName: string, password: string): Promise<IAuthCookie> {
245
+ /** The parsed cookies that will be returned. */
246
+ let parsedCookies: IAuthCookie;
247
+
248
+ // Executing all login flows
249
+ await this.executeLoginFlows(email, userName, password);
250
+
251
+ // Parsing the cookies
252
+ parsedCookies = this.parseCookies(this.cookies);
201
253
 
202
- // Returning the final cookies
203
- return this.cookies.join(';');
254
+ // Returning the final parsed cookies
255
+ return parsedCookies;
204
256
  }
205
257
  }
@@ -0,0 +1,81 @@
1
+ // PACKAGE
2
+ import axios from 'axios';
3
+
4
+ // URLS
5
+ import { guestTokenUrl } from '../helper/urls/Authentication';
6
+
7
+ // MODELS
8
+ import { AuthCookie } from '../../models/auth/AuthCookie';
9
+
10
+ // TYPES
11
+ import { IGuestCredentials, IAuthCredentials } from '../../types/Authentication';
12
+
13
+ // CONFIGS
14
+ import { config } from '../../config/env';
15
+
16
+ /**
17
+ * Handles authentication of http requests and other authentication related tasks.
18
+ *
19
+ * @internal
20
+ */
21
+ export class AuthService {
22
+ /** The common bearer token for authentication. */
23
+ private authToken: string;
24
+
25
+ /** The current authentication credentials. */
26
+ private credentials: IAuthCredentials;
27
+
28
+ /** Whether instance has been authenticated or not. */
29
+ public isAuthenticated: boolean;
30
+
31
+ /**
32
+ * @param cookie The cookie to be used for authenticating.
33
+ *
34
+ * @remarks
35
+ *
36
+ * If no cookie is supplied, then guest authentication is used.
37
+ */
38
+ constructor(cookie?: AuthCookie) {
39
+ // Reading the auth token from the config, since it's always the same
40
+ this.authToken = config.twitter_auth_token;
41
+
42
+ // Setting authentication status
43
+ this.isAuthenticated = (cookie?.auth_token && cookie?.ct0 && cookie?.kdt && cookie?.twid) ? true : false;
44
+
45
+ // If a cookies is supplied, initializing authenticated credentials
46
+ if (this.isAuthenticated) {
47
+ // Converting the cookie from JSON to object
48
+ cookie = new AuthCookie(cookie);
49
+
50
+ // Setting up the authenticated credentials
51
+ this.credentials = { authToken: this.authToken, csrfToken: cookie.ct0, cookie: cookie.toString() };
52
+ }
53
+ // If no cookie has been supplied, initializing empty credentials
54
+ else {
55
+ // Setting up the authenticated credentials
56
+ this.credentials = { authToken: this.authToken, csrfToken: '', cookie: '' };
57
+ }
58
+ }
59
+
60
+ /**
61
+ * @returns The current authentication credentials. A different credential is returned each time this is invoked
62
+ */
63
+ async getAuthCredentials(): Promise<IAuthCredentials> {
64
+ return this.credentials;
65
+ }
66
+
67
+ /**
68
+ * @returns The guest credentials fetched from twitter.
69
+ */
70
+ async getGuestCredentials(): Promise<IGuestCredentials> {
71
+ // Getting the guest credentials from twitter
72
+ return await axios.post<{ guest_token: string }>(guestTokenUrl(), null, {
73
+ headers: {
74
+ 'Authorization': this.authToken
75
+ }
76
+ }).then(res => ({
77
+ authToken: this.authToken,
78
+ guestToken: res.data.guest_token
79
+ }));
80
+ }
81
+ }
@@ -1,17 +1,23 @@
1
1
  // SERVICES
2
- import { FetcherService } from "../FetcherService";
3
- import { AuthService } from "../AuthService";
2
+ import { FetcherService } from "../util/FetcherService";
3
+ import { AuthService } from "../auth/AuthService";
4
+
5
+ // MODELS
6
+ import { Tweet } from "../../models/data/Tweet";
7
+ import { User } from "../../models/data/User";
8
+ import { TweetListArgs } from "../../models/args/TweetListArgs";
9
+ import { TweetFilter } from "../../models/args/TweetFilter";
10
+ import { CursoredData } from '../../models/data/CursoredData';
4
11
 
5
12
  // TYPES
6
- import { TweetFilter, Tweet } from "../../types/data/Tweet";
7
- import { User } from "../../types/data/User";
8
- import { CursoredData } from '../../types/data/Service';
9
13
  import RawTweet, { Result as TweetData } from '../../types/raw/tweet/Tweet';
10
14
  import { Result as UserData } from "../../types/raw/user/User";
11
15
  import RawTweets from '../../types/raw/tweet/Tweets';
12
16
  import RawLikers from '../../types/raw/tweet/Favouriters';
13
17
  import RawRetweeters from '../../types/raw/tweet/Retweeters';
14
- import * as Errors from '../../types/data/Errors';
18
+
19
+ // ENUMS
20
+ import { AuthenticationErrors } from '../../enums/Errors';
15
21
 
16
22
  // URLS
17
23
  import * as TweetUrls from '../helper/urls/Tweets';
@@ -19,10 +25,6 @@ import * as TweetUrls from '../helper/urls/Tweets';
19
25
  // EXTRACTORS
20
26
  import * as TweetExtractors from "../helper/extractors/Tweets";
21
27
 
22
- // DESERIALIZERS
23
- import * as UserDeserializers from '../helper/deserializers/Users';
24
- import * as TweetDeserializers from '../helper/deserializers/Tweets';
25
-
26
28
  // PARSERS
27
29
  import { toQueryString } from '../helper/Parser';
28
30
 
@@ -31,7 +33,6 @@ import { toQueryString } from '../helper/Parser';
31
33
  * @public
32
34
  */
33
35
  export class TweetService extends FetcherService {
34
- // MEMBER METHODS
35
36
  /**
36
37
  * @param auth The AuthService instance to use for authentication.
37
38
  */
@@ -41,19 +42,24 @@ export class TweetService extends FetcherService {
41
42
 
42
43
  /**
43
44
  * @param filter The filter be used for searching the tweets.
44
- * @param count The number of tweets to fetch.
45
+ * @param count The number of tweets to fetch, must be >= 10 and <= 20
45
46
  * @param cursor The cursor to the next batch of tweets. If blank, first batch is fetched.
47
+ *
46
48
  * @returns The list of tweets that match the given filter.
47
- * @remarks count must be >= 1 and <= 100.
49
+ *
50
+ * @throws {@link Errors.ValidationErrors.InvalidCount} error, if an invalid count has been provided.
51
+ *
52
+ * @remarks
53
+ *
54
+ * Cookies are required to use this method!
48
55
  */
49
- async getTweets(filter: TweetFilter, count: number, cursor: string): Promise<CursoredData<Tweet>> {
50
- // If invalid count provided
51
- if (count < 1 && !cursor) {
52
- throw new Error(Errors.ValidationErrors.InvalidCount);
53
- }
56
+ async getTweets(query: TweetFilter, count?: number, cursor?: string): Promise<CursoredData<Tweet>> {
57
+ // Objectifying parameters
58
+ let filter: TweetFilter = new TweetFilter(query);
59
+ let args: TweetListArgs = new TweetListArgs(count, cursor);
54
60
 
55
61
  // Getting the raw data
56
- let res = await this.request<RawTweets>(TweetUrls.tweetsUrl(toQueryString(filter), count, cursor), false).then(res => res.data);
62
+ let res = await this.request<RawTweets>(TweetUrls.tweetsUrl(toQueryString(filter), args.count, args.cursor), this.isAuthenticated).then(res => res.data);
57
63
 
58
64
  // Extracting data
59
65
  let data = TweetExtractors.extractTweets(res);
@@ -62,62 +68,75 @@ export class TweetService extends FetcherService {
62
68
  this.cacheData(data);
63
69
 
64
70
  // Parsing data
65
- let tweets = data.required.map((item: TweetData) => TweetDeserializers.toTweet(item));
71
+ let tweets = data.required.map((item: TweetData) => new Tweet(item));
66
72
 
67
- return {
68
- list: tweets,
69
- next: { value: data.cursor }
70
- };
73
+ // Sorting the tweets by date, from recent to oldest
74
+ tweets.sort((a, b) => new Date(b.createdAt).valueOf() - new Date(a.createdAt).valueOf());
75
+
76
+ return new CursoredData<Tweet>(tweets, data.cursor);
71
77
  }
72
78
 
73
79
  /**
74
- * @param tweetId The rest id of the target tweet.
80
+ * @param id The id of the target tweet.
81
+ *
75
82
  * @returns The details of a single tweet with the given tweet id.
83
+ *
84
+ * @throws {@link Errors.DataErrors.TweetNotFound} error, if no tweet with the given id was found.
85
+ *
86
+ * @remarks
87
+ *
88
+ * No cookies are required to use this method.
76
89
  */
77
- async getTweetById(tweetId: string): Promise<Tweet> {
90
+ async getTweetDetails(id: string): Promise<Tweet> {
78
91
  // Getting data from cache
79
- let cachedData = await this.readData(tweetId);
92
+ let cachedData = await this.readData(id);
80
93
 
81
94
  // If data exists in cache
82
95
  if (cachedData) {
83
96
  return cachedData;
84
97
  }
85
-
98
+
86
99
  // Fetching the raw data
87
- let res = await this.request<RawTweet>(TweetUrls.tweetDetailsUrl(tweetId), false).then(res => res.data);
100
+ let res = await this.request<RawTweet>(TweetUrls.tweetDetailsUrl(id), false).then(res => res.data);
88
101
 
89
102
  // Extracting data
90
- let data = TweetExtractors.extractTweet(res, tweetId);
103
+ let data = TweetExtractors.extractTweet(res, id);
91
104
 
92
105
  // Caching data
93
106
  this.cacheData(data);
94
107
 
95
108
  // Parsing data
96
- let tweet = TweetDeserializers.toTweet(data.required[0]);
109
+ let tweet = new Tweet(data.required[0]);
97
110
 
98
111
  return tweet;
99
112
  }
100
113
 
101
114
  /**
102
115
  * @param tweetId The rest id of the target tweet.
103
- * @param count The batch size of the list.
116
+ * @param count The batch size of the list, must be >= 10 (when no cursor is provided) and <= 20.
104
117
  * @param cursor The cursor to the next batch of users. If blank, first batch is fetched.
118
+ *
105
119
  * @returns The list of users who liked the given tweet.
106
- * @remarks count must be >= 10 (when no cursor is provided) and <= 100.
120
+ *
121
+ * @throws {@link Errors.AuthenticationErrors.NotAuthenticated} error, if no cookies have been provided.
122
+ * @throws {@link Errors.ValidationErrors.InvalidCount} error, if invalid count is provided.
123
+ * @throws {@link Errors.DataErrors.TweetNotFound} error, if no tweet with the given id was found.
124
+ *
125
+ * @remarks
126
+ *
127
+ * Cookies are required to use this method!
107
128
  */
108
- async getTweetLikers(tweetId: string, count: number, cursor: string): Promise<CursoredData<User>> {
129
+ async getTweetLikers(tweetId: string, count?: number, cursor?: string): Promise<CursoredData<User>> {
109
130
  // If user is not authenticated, abort
110
- if(!this.isAuthenticated) {
111
- throw new Error(Errors.AuthenticationErrors.NotAuthenticated);
131
+ if (!this.isAuthenticated) {
132
+ throw new Error(AuthenticationErrors.NotAuthenticated);
112
133
  }
113
134
 
114
- // If invalid count provided
115
- if (count < 10 && !cursor) {
116
- throw new Error(Errors.ValidationErrors.InvalidCount);
117
- }
118
-
135
+ // Objectifying parameters
136
+ let args: TweetListArgs = new TweetListArgs(count, cursor);
137
+
119
138
  // Fetching the raw data
120
- let res = await this.request<RawLikers>(TweetUrls.tweetLikesUrl(tweetId, count, cursor)).then(res => res.data);
139
+ let res = await this.request<RawLikers>(TweetUrls.tweetLikesUrl(tweetId, args.count, args.cursor)).then(res => res.data);
121
140
 
122
141
  // Extracting data
123
142
  let data = TweetExtractors.extractTweetLikers(res);
@@ -126,34 +145,37 @@ export class TweetService extends FetcherService {
126
145
  this.cacheData(data);
127
146
 
128
147
  // Parsing data
129
- let users = data.required.map((item: UserData) => UserDeserializers.toUser(item));
148
+ let users = data.required.map((item: UserData) => new User(item));
130
149
 
131
- return {
132
- list: users,
133
- next: { value: data.cursor }
134
- };
150
+ return new CursoredData<User>(users, data.cursor);
135
151
  }
136
152
 
137
153
  /**
138
154
  * @param tweetId The rest id of the target tweet.
139
- * @param count The batch size of the list.
155
+ * @param count The batch size of the list, must be >= 10 (when no cursor is provided) and <= 100.
140
156
  * @param cursor The cursor to the next batch of users. If blank, first batch is fetched.
157
+ *
141
158
  * @returns The list of users who retweeted the given tweet.
142
- * @remarks count must be >= 10 (when no cursor is provided) and <= 100.
159
+ *
160
+ * @throws {@link Errors.AuthenticationErrors.NotAuthenticated} error, if no cookies have been provided.
161
+ * @throws {@link Errors.ValidationErrors.InvalidCount} error, if invalid count is provided.
162
+ * @throws {@link Errors.DataErrors.TweetNotFound} error, if no tweet with the given id was found.
163
+ *
164
+ * @remarks
165
+ *
166
+ * Cookies are required to use this method!
143
167
  */
144
- async getTweetRetweeters(tweetId: string, count: number, cursor: string): Promise<CursoredData<User>> {
168
+ async getTweetRetweeters(tweetId: string, count?: number, cursor?: string): Promise<CursoredData<User>> {
145
169
  // If user is not authenticated, abort
146
- if(!this.isAuthenticated) {
147
- throw new Error(Errors.AuthenticationErrors.NotAuthenticated);
170
+ if (!this.isAuthenticated) {
171
+ throw new Error(AuthenticationErrors.NotAuthenticated);
148
172
  }
149
173
 
150
- // If invalid count provided
151
- if (count < 10 && !cursor) {
152
- throw new Error(Errors.ValidationErrors.InvalidCount);
153
- }
174
+ // Objectifying parameters
175
+ let args: TweetListArgs = new TweetListArgs(count, cursor);
154
176
 
155
177
  // Fetching the raw data
156
- let res = await this.request<RawRetweeters>(TweetUrls.tweetRetweetUrl(tweetId, count, cursor)).then(res => res.data);
178
+ let res = await this.request<RawRetweeters>(TweetUrls.tweetRetweetUrl(tweetId, args.count, args.cursor)).then(res => res.data);
157
179
 
158
180
  // Extracting data
159
181
  let data = TweetExtractors.extractTweetRetweeters(res);
@@ -162,12 +184,9 @@ export class TweetService extends FetcherService {
162
184
  this.cacheData(data);
163
185
 
164
186
  // Parsing data
165
- let users = data.required.map((item: UserData) => UserDeserializers.toUser(item));
187
+ let users = data.required.map((item: UserData) => new User(item));
166
188
 
167
- return {
168
- list: users,
169
- next: { value: data.cursor }
170
- };
189
+ return new CursoredData<User>(users, data.cursor);
171
190
  }
172
191
 
173
192
  /**