rettiwt-api 2.7.1 → 3.0.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 (139) hide show
  1. package/.eslintrc.js +73 -5
  2. package/.tool-versions +1 -0
  3. package/README.md +87 -20
  4. package/dist/Rettiwt.js +0 -1
  5. package/dist/Rettiwt.js.map +1 -1
  6. package/dist/cli.js +2 -4
  7. package/dist/cli.js.map +1 -1
  8. package/dist/collections/Extractors.d.ts +10 -0
  9. package/dist/collections/Extractors.js +43 -0
  10. package/dist/collections/Extractors.js.map +1 -0
  11. package/dist/collections/Groups.d.ts +19 -0
  12. package/dist/collections/Groups.js +55 -0
  13. package/dist/collections/Groups.js.map +1 -0
  14. package/dist/collections/Requests.d.ts +12 -0
  15. package/dist/collections/Requests.js +46 -0
  16. package/dist/collections/Requests.js.map +1 -0
  17. package/dist/commands/Auth.d.ts +6 -0
  18. package/dist/commands/Auth.js +26 -8
  19. package/dist/commands/Auth.js.map +1 -1
  20. package/dist/commands/Tweet.js +237 -82
  21. package/dist/commands/Tweet.js.map +1 -1
  22. package/dist/commands/User.js +197 -36
  23. package/dist/commands/User.js.map +1 -1
  24. package/dist/enums/Api.d.ts +30 -0
  25. package/dist/enums/Api.js +32 -1
  26. package/dist/enums/Api.js.map +1 -1
  27. package/dist/enums/Data.d.ts +9 -0
  28. package/dist/enums/Data.js +14 -0
  29. package/dist/enums/Data.js.map +1 -0
  30. package/dist/enums/Http.d.ts +1 -1
  31. package/dist/enums/Http.js +1 -1
  32. package/dist/enums/Logging.d.ts +6 -5
  33. package/dist/enums/Logging.js +6 -5
  34. package/dist/enums/Logging.js.map +1 -1
  35. package/dist/enums/Resource.d.ts +33 -0
  36. package/dist/enums/Resource.js +42 -0
  37. package/dist/enums/Resource.js.map +1 -0
  38. package/dist/helper/CliUtils.d.ts +1 -1
  39. package/dist/helper/CliUtils.js.map +1 -1
  40. package/dist/index.d.ts +11 -9
  41. package/dist/index.js +11 -14
  42. package/dist/index.js.map +1 -1
  43. package/dist/models/args/FetchArgs.d.ts +129 -0
  44. package/dist/models/args/FetchArgs.js +263 -0
  45. package/dist/models/args/FetchArgs.js.map +1 -0
  46. package/dist/models/args/PostArgs.d.ts +116 -0
  47. package/dist/models/args/PostArgs.js +232 -0
  48. package/dist/models/args/PostArgs.js.map +1 -0
  49. package/dist/models/data/CursoredData.d.ts +11 -11
  50. package/dist/models/data/CursoredData.js +21 -16
  51. package/dist/models/data/CursoredData.js.map +1 -1
  52. package/dist/models/data/List.d.ts +8 -10
  53. package/dist/models/data/List.js +2 -4
  54. package/dist/models/data/List.js.map +1 -1
  55. package/dist/models/data/Tweet.d.ts +41 -29
  56. package/dist/models/data/Tweet.js +71 -15
  57. package/dist/models/data/Tweet.js.map +1 -1
  58. package/dist/models/data/User.d.ts +36 -20
  59. package/dist/models/data/User.js +69 -7
  60. package/dist/models/data/User.js.map +1 -1
  61. package/dist/models/errors/ApiError.d.ts +1 -3
  62. package/dist/models/errors/ApiError.js +1 -4
  63. package/dist/models/errors/ApiError.js.map +1 -1
  64. package/dist/models/errors/DataValidationError.d.ts +30 -0
  65. package/dist/models/errors/DataValidationError.js +34 -0
  66. package/dist/models/errors/DataValidationError.js.map +1 -0
  67. package/dist/models/errors/HttpError.d.ts +1 -3
  68. package/dist/models/errors/HttpError.js +1 -4
  69. package/dist/models/errors/HttpError.js.map +1 -1
  70. package/dist/models/errors/TimeoutError.d.ts +2 -4
  71. package/dist/models/errors/TimeoutError.js +2 -5
  72. package/dist/models/errors/TimeoutError.js.map +1 -1
  73. package/dist/services/internal/ErrorService.d.ts +45 -35
  74. package/dist/services/internal/ErrorService.js +70 -68
  75. package/dist/services/internal/ErrorService.js.map +1 -1
  76. package/dist/services/internal/LogService.d.ts +7 -5
  77. package/dist/services/internal/LogService.js +28 -9
  78. package/dist/services/internal/LogService.js.map +1 -1
  79. package/dist/services/public/AuthService.d.ts +24 -20
  80. package/dist/services/public/AuthService.js +38 -36
  81. package/dist/services/public/AuthService.js.map +1 -1
  82. package/dist/services/public/FetcherService.d.ts +99 -0
  83. package/dist/services/public/FetcherService.js +254 -0
  84. package/dist/services/public/FetcherService.js.map +1 -0
  85. package/dist/services/public/TweetService.d.ts +213 -94
  86. package/dist/services/public/TweetService.js +405 -208
  87. package/dist/services/public/TweetService.js.map +1 -1
  88. package/dist/services/public/UserService.d.ts +185 -52
  89. package/dist/services/public/UserService.js +333 -103
  90. package/dist/services/public/UserService.js.map +1 -1
  91. package/dist/types/ReturnTypes.d.ts +21 -0
  92. package/dist/types/ReturnTypes.js +3 -0
  93. package/dist/types/ReturnTypes.js.map +1 -0
  94. package/package.json +4 -2
  95. package/src/Rettiwt.ts +0 -3
  96. package/src/cli.ts +2 -4
  97. package/src/collections/Extractors.ts +63 -0
  98. package/src/collections/Groups.ts +54 -0
  99. package/src/collections/Requests.ts +52 -0
  100. package/src/commands/Auth.ts +19 -7
  101. package/src/commands/Tweet.ts +179 -91
  102. package/src/commands/User.ts +118 -25
  103. package/src/enums/Api.ts +31 -0
  104. package/src/enums/Data.ts +9 -0
  105. package/src/enums/Http.ts +1 -1
  106. package/src/enums/Logging.ts +6 -5
  107. package/src/enums/Resource.ts +40 -0
  108. package/src/helper/CliUtils.ts +1 -1
  109. package/src/index.ts +41 -14
  110. package/src/models/args/FetchArgs.ts +296 -0
  111. package/src/models/args/PostArgs.ts +263 -0
  112. package/src/models/data/CursoredData.ts +23 -15
  113. package/src/models/data/List.ts +12 -15
  114. package/src/models/data/Tweet.ts +105 -39
  115. package/src/models/data/User.ts +97 -30
  116. package/src/models/errors/ApiError.ts +1 -4
  117. package/src/models/errors/DataValidationError.ts +44 -0
  118. package/src/models/errors/HttpError.ts +1 -4
  119. package/src/models/errors/TimeoutError.ts +2 -5
  120. package/src/services/internal/ErrorService.ts +76 -75
  121. package/src/services/internal/LogService.ts +20 -10
  122. package/src/services/public/AuthService.ts +39 -38
  123. package/src/services/public/FetcherService.ts +230 -0
  124. package/src/services/public/TweetService.ts +381 -179
  125. package/src/services/public/UserService.ts +314 -86
  126. package/src/types/RettiwtConfig.ts +0 -1
  127. package/src/types/ReturnTypes.ts +24 -0
  128. package/dist/models/args/TweetArgs.d.ts +0 -44
  129. package/dist/models/args/TweetArgs.js +0 -82
  130. package/dist/models/args/TweetArgs.js.map +0 -1
  131. package/dist/models/data/Media.d.ts +0 -14
  132. package/dist/models/data/Media.js +0 -19
  133. package/dist/models/data/Media.js.map +0 -1
  134. package/dist/services/internal/FetcherService.d.ts +0 -106
  135. package/dist/services/internal/FetcherService.js +0 -365
  136. package/dist/services/internal/FetcherService.js.map +0 -1
  137. package/src/models/args/TweetArgs.ts +0 -98
  138. package/src/models/data/Media.ts +0 -19
  139. package/src/services/internal/FetcherService.ts +0 -365
@@ -52,19 +52,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
52
52
  };
53
53
  Object.defineProperty(exports, "__esModule", { value: true });
54
54
  exports.AuthService = void 0;
55
- // PACKAGES
56
55
  var rettiwt_auth_1 = require("rettiwt-auth");
57
- // SERVICES
58
- var FetcherService_1 = require("../internal/FetcherService");
56
+ var FetcherService_1 = require("./FetcherService");
59
57
  /**
60
- * Handles authentication.
58
+ * The services that handles authentication.
61
59
  *
62
60
  * @public
63
61
  */
64
62
  var AuthService = /** @class */ (function (_super) {
65
63
  __extends(AuthService, _super);
66
64
  /**
67
- * @param config - The config object for configuring the Rettiwt instance.
65
+ * @param config - The config object for configuring the `Rettiwt` instance.
68
66
  *
69
67
  * @internal
70
68
  */
@@ -72,12 +70,9 @@ var AuthService = /** @class */ (function (_super) {
72
70
  return _super.call(this, config) || this;
73
71
  }
74
72
  /**
75
- * Login to twitter using account credentials.
73
+ * Login to twitter as guest.
76
74
  *
77
- * @param email - The email id associated with the Twitter account.
78
- * @param userName - The username associated with the Twitter account.
79
- * @param password - The password to the Twitter account.
80
- * @returns The API_KEY for the Twitter account.
75
+ * @returns A new guest key.
81
76
  *
82
77
  * @example
83
78
  * ```
@@ -86,10 +81,10 @@ var AuthService = /** @class */ (function (_super) {
86
81
  * // Creating a new Rettiwt instance
87
82
  * const rettiwt = new Rettiwt();
88
83
  *
89
- * // Logging in an getting the API_KEY
90
- * rettiwt.auth.login("email@domain.com", "username", "password")
91
- * .then(apiKey => {
92
- * // Use the API_KEY
84
+ * // Logging in an getting a new guest key
85
+ * rettiwt.auth.guest()
86
+ * .then(guestKey => {
87
+ * // Use the guest key
93
88
  * ...
94
89
  * })
95
90
  * .catch(err => {
@@ -97,30 +92,28 @@ var AuthService = /** @class */ (function (_super) {
97
92
  * });
98
93
  * ```
99
94
  */
100
- AuthService.prototype.login = function (email, userName, password) {
95
+ AuthService.prototype.guest = function () {
101
96
  var _a;
102
97
  return __awaiter(this, void 0, void 0, function () {
103
- var apiKey;
98
+ var guestKey;
104
99
  return __generator(this, function (_b) {
105
100
  switch (_b.label) {
106
- case 0: return [4 /*yield*/, new rettiwt_auth_1.Auth({ proxyUrl: this.authProxyUrl }).getUserCredential({
107
- email: email,
108
- userName: userName,
109
- password: password,
110
- })];
101
+ case 0: return [4 /*yield*/, new rettiwt_auth_1.Auth().getGuestCredential()];
111
102
  case 1:
112
- apiKey = (_a = (_b.sent()).toHeader().cookie) !== null && _a !== void 0 ? _a : '';
113
- // Converting the credentials to base64 string
114
- apiKey = Buffer.from(apiKey).toString('base64');
115
- return [2 /*return*/, apiKey];
103
+ guestKey = (_a = (_b.sent()).guestToken) !== null && _a !== void 0 ? _a : '';
104
+ return [2 /*return*/, guestKey];
116
105
  }
117
106
  });
118
107
  });
119
108
  };
120
109
  /**
121
- * Login to twitter as guest.
110
+ * Login to twitter using account credentials.
122
111
  *
123
- * @returns A new guest key.
112
+ * @param email - The email id associated with the Twitter account.
113
+ * @param userName - The username associated with the Twitter account.
114
+ * @param password - The password to the Twitter account.
115
+ *
116
+ * @returns The `API_KEY` for the Twitter account.
124
117
  *
125
118
  * @example
126
119
  * ```
@@ -129,27 +122,36 @@ var AuthService = /** @class */ (function (_super) {
129
122
  * // Creating a new Rettiwt instance
130
123
  * const rettiwt = new Rettiwt();
131
124
  *
132
- * // Logging in an getting a new guest key
133
- * rettiwt.auth.guest()
134
- * .then(guestKey => {
135
- * // Use the guest key
125
+ * // Logging in an getting the API_KEY
126
+ * rettiwt.auth.login("email@domain.com", "username", "password")
127
+ * .then(apiKey => {
128
+ * // Use the API_KEY
136
129
  * ...
137
130
  * })
138
131
  * .catch(err => {
139
132
  * console.log(err);
140
133
  * });
141
134
  * ```
135
+ *
136
+ * @remarks
137
+ * Interchanging `email` and `userName` works too.
142
138
  */
143
- AuthService.prototype.guest = function () {
139
+ AuthService.prototype.login = function (email, userName, password) {
144
140
  var _a;
145
141
  return __awaiter(this, void 0, void 0, function () {
146
- var guestKey;
142
+ var apiKey;
147
143
  return __generator(this, function (_b) {
148
144
  switch (_b.label) {
149
- case 0: return [4 /*yield*/, new rettiwt_auth_1.Auth().getGuestCredential()];
145
+ case 0: return [4 /*yield*/, new rettiwt_auth_1.Auth({ proxyUrl: this.authProxyUrl }).getUserCredential({
146
+ email: email,
147
+ userName: userName,
148
+ password: password,
149
+ })];
150
150
  case 1:
151
- guestKey = (_a = (_b.sent()).guestToken) !== null && _a !== void 0 ? _a : '';
152
- return [2 /*return*/, guestKey];
151
+ apiKey = (_a = (_b.sent()).toHeader().cookie) !== null && _a !== void 0 ? _a : '';
152
+ // Converting the credentials to base64 string
153
+ apiKey = Buffer.from(apiKey).toString('base64');
154
+ return [2 /*return*/, apiKey];
153
155
  }
154
156
  });
155
157
  });
@@ -1 +1 @@
1
- {"version":3,"file":"AuthService.js","sourceRoot":"","sources":["../../../src/services/public/AuthService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,WAAW;AACX,6CAAoC;AAEpC,WAAW;AACX,6DAA4D;AAK5D;;;;GAIG;AACH;IAAiC,+BAAc;IAC9C;;;;OAIG;IACH,qBAAmB,MAAuB;eACzC,kBAAM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AI,mBAAI,EAAE,CAAC,kBAAkB,EAAE,EAAA;;wBAAzD,QAAQ,GAAW,MAAA,CAAC,SAAqC,CAAC,CAAC,UAAU,mCAAI,EAAE;wBAEjF,sBAAO,QAAQ,EAAC;;;;KAChB;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACU,2BAAK,GAAlB,UAAmB,KAAa,EAAE,QAAgB,EAAE,QAAgB;;;;;;4BAIjE,qBAAM,IAAI,mBAAI,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC,iBAAiB,CAAC;4BACjE,KAAK,EAAE,KAAK;4BACZ,QAAQ,EAAE,QAAQ;4BAClB,QAAQ,EAAE,QAAQ;yBAClB,CAAC,EAAA;;wBANA,MAAM,GACT,MAAC,CACA,SAIE,CACF,CAAC,QAAQ,EAAE,CAAC,MAAiB,mCAAI,EAAE;wBAErC,8CAA8C;wBAC9C,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;wBAEhD,sBAAO,MAAM,EAAC;;;;KACd;IACF,kBAAC;AAAD,CAAC,AAtFD,CAAiC,+BAAc,GAsF9C;AAtFY,kCAAW"}
@@ -0,0 +1,99 @@
1
+ import { EResourceType } from '../../enums/Resource';
2
+ import { FetchArgs } from '../../models/args/FetchArgs';
3
+ import { PostArgs } from '../../models/args/PostArgs';
4
+ import { IRettiwtConfig } from '../../types/RettiwtConfig';
5
+ import { AllReturnTypes } from '../../types/ReturnTypes';
6
+ /**
7
+ * The base service that handles all HTTP requests.
8
+ *
9
+ * @public
10
+ */
11
+ export declare class FetcherService {
12
+ /** The api key to use for authenticating against Twitter API as user. */
13
+ private readonly apiKey?;
14
+ /** The service used to handle HTTP and API errors */
15
+ private readonly errorHandler;
16
+ /** The guest key to use for authenticating against Twitter API as guest. */
17
+ private readonly guestKey?;
18
+ /** Whether the instance is authenticated or not. */
19
+ private readonly isAuthenticated;
20
+ /** The URL To the proxy server to use for all others. */
21
+ private readonly proxyUrl?;
22
+ /** The max wait time for a response. */
23
+ private readonly timeout;
24
+ /** The URL to the proxy server to use only for authentication. */
25
+ protected readonly authProxyUrl?: URL;
26
+ /**
27
+ * @param config - The config object for configuring the Rettiwt instance.
28
+ */
29
+ constructor(config?: IRettiwtConfig);
30
+ /**
31
+ * Checks the authorization status based on the requested resource.
32
+ *
33
+ * @param resource - The requested resource.
34
+ *
35
+ * @throws An error if not authorized to access the requested resource.
36
+ */
37
+ private checkAuthorization;
38
+ /**
39
+ * Returns the AuthCredentials based on the type of key present.
40
+ *
41
+ * @returns The generated AuthCredential
42
+ */
43
+ private getCredential;
44
+ /**
45
+ * Gets the https agent based on whether a proxy is used or not.
46
+ *
47
+ * @param proxyUrl - Optional URL with proxy configuration to use for requests to Twitter API.
48
+ *
49
+ * @returns The https agent to use.
50
+ */
51
+ private getHttpsAgent;
52
+ /**
53
+ * Validates the given args against the given resource.
54
+ *
55
+ * @param resource - The resource against which validation is to be done.
56
+ * @param args - The args to be validated.
57
+ *
58
+ * @returns The validated args.
59
+ */
60
+ private validateArgs;
61
+ /**
62
+ * Extracts and deserializes the required data based on the requested resource.
63
+ *
64
+ * @param data - The raw response data from which extraction is to be done.
65
+ * @param resource - The requested resource.
66
+ *
67
+ * @returns The extracted and deserialized data.
68
+ */
69
+ protected extract<T extends AllReturnTypes>(response: NonNullable<unknown>, resource: EResourceType): T | undefined;
70
+ /**
71
+ * Makes an HTTP request according to the given parameters.
72
+ *
73
+ * @param resource - The requested resource.
74
+ * @param config - The request configuration.
75
+ *
76
+ * @typeParam T - The type of the returned response data.
77
+ *
78
+ * @returns The raw data response received.
79
+ *
80
+ * @example
81
+ * Fetching the raw details of a user with username 'user1'
82
+ * ```
83
+ * import { FetcherService, EResourceType } from 'rettiwt-api';
84
+ *
85
+ * // Creating a new FetcherService instance using the given 'API_KEY'
86
+ * const fetcher = new FetcherService({ apiKey: API_KEY });
87
+ *
88
+ * // Fetching the details of the User with username 'user1'
89
+ * fetcher.request(EResourceType.USER_DETAILS_BY_USERNAME, { id: 'user1' })
90
+ * .then(res => {
91
+ * console.log(res);
92
+ * })
93
+ * .catch(err => {
94
+ * console.log(err);
95
+ * })
96
+ * ```
97
+ */
98
+ request<T>(resource: EResourceType, args: FetchArgs | PostArgs): Promise<T>;
99
+ }
@@ -0,0 +1,254 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.FetcherService = void 0;
54
+ var https_1 = __importDefault(require("https"));
55
+ var axios_1 = __importDefault(require("axios"));
56
+ var https_proxy_agent_1 = require("https-proxy-agent");
57
+ var rettiwt_auth_1 = require("rettiwt-auth");
58
+ var Extractors_1 = require("../../collections/Extractors");
59
+ var Groups_1 = require("../../collections/Groups");
60
+ var Requests_1 = require("../../collections/Requests");
61
+ var Api_1 = require("../../enums/Api");
62
+ var Logging_1 = require("../../enums/Logging");
63
+ var FetchArgs_1 = require("../../models/args/FetchArgs");
64
+ var PostArgs_1 = require("../../models/args/PostArgs");
65
+ var ErrorService_1 = require("../internal/ErrorService");
66
+ var LogService_1 = require("../internal/LogService");
67
+ /**
68
+ * The base service that handles all HTTP requests.
69
+ *
70
+ * @public
71
+ */
72
+ var FetcherService = /** @class */ (function () {
73
+ /**
74
+ * @param config - The config object for configuring the Rettiwt instance.
75
+ */
76
+ function FetcherService(config) {
77
+ var _a, _b, _c, _d;
78
+ LogService_1.LogService.enabled = (_a = config === null || config === void 0 ? void 0 : config.logging) !== null && _a !== void 0 ? _a : false;
79
+ this.apiKey = config === null || config === void 0 ? void 0 : config.apiKey;
80
+ this.guestKey = config === null || config === void 0 ? void 0 : config.guestKey;
81
+ this.isAuthenticated = (config === null || config === void 0 ? void 0 : config.apiKey) ? true : false;
82
+ this.authProxyUrl = (_b = config === null || config === void 0 ? void 0 : config.authProxyUrl) !== null && _b !== void 0 ? _b : config === null || config === void 0 ? void 0 : config.proxyUrl;
83
+ this.proxyUrl = config === null || config === void 0 ? void 0 : config.proxyUrl;
84
+ this.timeout = (_c = config === null || config === void 0 ? void 0 : config.timeout) !== null && _c !== void 0 ? _c : 0;
85
+ this.errorHandler = (_d = config === null || config === void 0 ? void 0 : config.errorHandler) !== null && _d !== void 0 ? _d : new ErrorService_1.ErrorService();
86
+ }
87
+ /**
88
+ * Checks the authorization status based on the requested resource.
89
+ *
90
+ * @param resource - The requested resource.
91
+ *
92
+ * @throws An error if not authorized to access the requested resource.
93
+ */
94
+ FetcherService.prototype.checkAuthorization = function (resource) {
95
+ // Logging
96
+ LogService_1.LogService.log(Logging_1.ELogActions.AUTHORIZATION, { authenticated: this.isAuthenticated });
97
+ // Checking authorization status
98
+ if (!Groups_1.allowGuestAuthentication.includes(resource) && this.isAuthenticated == false) {
99
+ throw new Error(Api_1.EApiErrors.RESOURCE_NOT_ALLOWED);
100
+ }
101
+ };
102
+ /**
103
+ * Returns the AuthCredentials based on the type of key present.
104
+ *
105
+ * @returns The generated AuthCredential
106
+ */
107
+ FetcherService.prototype.getCredential = function () {
108
+ return __awaiter(this, void 0, void 0, function () {
109
+ var cookies;
110
+ return __generator(this, function (_a) {
111
+ switch (_a.label) {
112
+ case 0:
113
+ if (!this.apiKey) return [3 /*break*/, 1];
114
+ // Logging
115
+ LogService_1.LogService.log(Logging_1.ELogActions.GET, { target: 'USER_CREDENTIAL' });
116
+ cookies = Buffer.from(this.apiKey, 'base64').toString('ascii').split(';');
117
+ return [2 /*return*/, new rettiwt_auth_1.AuthCredential(cookies)];
118
+ case 1:
119
+ if (!this.guestKey) return [3 /*break*/, 2];
120
+ // Logging
121
+ LogService_1.LogService.log(Logging_1.ELogActions.GET, { target: 'GUEST_CREDENTIAL' });
122
+ return [2 /*return*/, new rettiwt_auth_1.AuthCredential(undefined, this.guestKey)];
123
+ case 2:
124
+ // Logging
125
+ LogService_1.LogService.log(Logging_1.ELogActions.GET, { target: 'NEW_GUEST_CREDENTIAL' });
126
+ return [4 /*yield*/, new rettiwt_auth_1.Auth({ proxyUrl: this.authProxyUrl }).getGuestCredential()];
127
+ case 3: return [2 /*return*/, _a.sent()];
128
+ }
129
+ });
130
+ });
131
+ };
132
+ /**
133
+ * Gets the https agent based on whether a proxy is used or not.
134
+ *
135
+ * @param proxyUrl - Optional URL with proxy configuration to use for requests to Twitter API.
136
+ *
137
+ * @returns The https agent to use.
138
+ */
139
+ FetcherService.prototype.getHttpsAgent = function (proxyUrl) {
140
+ if (proxyUrl) {
141
+ // Logging
142
+ LogService_1.LogService.log(Logging_1.ELogActions.GET, { target: 'HTTPS_PROXY_AGENT' });
143
+ return new https_proxy_agent_1.HttpsProxyAgent(proxyUrl);
144
+ }
145
+ else {
146
+ // Logging
147
+ LogService_1.LogService.log(Logging_1.ELogActions.GET, { target: 'HTTPS_AGENT' });
148
+ return new https_1.default.Agent();
149
+ }
150
+ };
151
+ /**
152
+ * Validates the given args against the given resource.
153
+ *
154
+ * @param resource - The resource against which validation is to be done.
155
+ * @param args - The args to be validated.
156
+ *
157
+ * @returns The validated args.
158
+ */
159
+ FetcherService.prototype.validateArgs = function (resource, args) {
160
+ if (Groups_1.fetchResources.includes(resource)) {
161
+ // Logging
162
+ LogService_1.LogService.log(Logging_1.ELogActions.VALIDATE, { target: 'FETCH_ARGS' });
163
+ return new FetchArgs_1.FetchArgs(resource, args);
164
+ }
165
+ else if (Groups_1.postResources.includes(resource)) {
166
+ // Logging
167
+ LogService_1.LogService.log(Logging_1.ELogActions.VALIDATE, { target: 'POST_ARGS' });
168
+ return new PostArgs_1.PostArgs(resource, args);
169
+ }
170
+ };
171
+ /**
172
+ * Extracts and deserializes the required data based on the requested resource.
173
+ *
174
+ * @param data - The raw response data from which extraction is to be done.
175
+ * @param resource - The requested resource.
176
+ *
177
+ * @returns The extracted and deserialized data.
178
+ */
179
+ FetcherService.prototype.extract = function (response, resource) {
180
+ // Logging
181
+ LogService_1.LogService.log(Logging_1.ELogActions.EXTRACT, { resource: resource });
182
+ return Extractors_1.extractors[resource](response);
183
+ };
184
+ /**
185
+ * Makes an HTTP request according to the given parameters.
186
+ *
187
+ * @param resource - The requested resource.
188
+ * @param config - The request configuration.
189
+ *
190
+ * @typeParam T - The type of the returned response data.
191
+ *
192
+ * @returns The raw data response received.
193
+ *
194
+ * @example
195
+ * Fetching the raw details of a user with username 'user1'
196
+ * ```
197
+ * import { FetcherService, EResourceType } from 'rettiwt-api';
198
+ *
199
+ * // Creating a new FetcherService instance using the given 'API_KEY'
200
+ * const fetcher = new FetcherService({ apiKey: API_KEY });
201
+ *
202
+ * // Fetching the details of the User with username 'user1'
203
+ * fetcher.request(EResourceType.USER_DETAILS_BY_USERNAME, { id: 'user1' })
204
+ * .then(res => {
205
+ * console.log(res);
206
+ * })
207
+ * .catch(err => {
208
+ * console.log(err);
209
+ * })
210
+ * ```
211
+ */
212
+ FetcherService.prototype.request = function (resource, args) {
213
+ return __awaiter(this, void 0, void 0, function () {
214
+ var httpsAgent, cred, config, error_1;
215
+ return __generator(this, function (_a) {
216
+ switch (_a.label) {
217
+ case 0:
218
+ // Logging
219
+ LogService_1.LogService.log(Logging_1.ELogActions.REQUEST, { resource: resource, args: args });
220
+ // Checking authorization for the requested resource
221
+ this.checkAuthorization(resource);
222
+ // Validating args
223
+ args = this.validateArgs(resource, args);
224
+ httpsAgent = this.getHttpsAgent(this.proxyUrl);
225
+ return [4 /*yield*/, this.getCredential()];
226
+ case 1:
227
+ cred = _a.sent();
228
+ config = Requests_1.requests[resource](args);
229
+ // Setting additional request parameters
230
+ config.headers = __assign(__assign({}, config.headers), cred.toHeader());
231
+ config.httpAgent = httpsAgent;
232
+ config.httpsAgent = httpsAgent;
233
+ config.timeout = this.timeout;
234
+ _a.label = 2;
235
+ case 2:
236
+ _a.trys.push([2, 4, , 5]);
237
+ return [4 /*yield*/, (0, axios_1.default)(config)];
238
+ case 3:
239
+ // Returning the reponse body
240
+ return [2 /*return*/, (_a.sent()).data];
241
+ case 4:
242
+ error_1 = _a.sent();
243
+ // If error, delegate handling to error handler
244
+ this.errorHandler.handle(error_1);
245
+ throw error_1;
246
+ case 5: return [2 /*return*/];
247
+ }
248
+ });
249
+ });
250
+ };
251
+ return FetcherService;
252
+ }());
253
+ exports.FetcherService = FetcherService;
254
+ //# sourceMappingURL=FetcherService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FetcherService.js","sourceRoot":"","sources":["../../../src/services/public/FetcherService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAqC;AAErC,gDAA0B;AAC1B,uDAAoD;AACpD,6CAAoD;AAEpD,2DAA0D;AAC1D,mDAAmG;AACnG,uDAAsD;AACtD,uCAA6C;AAC7C,+CAAkD;AAElD,yDAAwD;AACxD,uDAAsD;AAMtD,yDAAwD;AACxD,qDAAoD;AAEpD;;;;GAIG;AACH;IAsBC;;OAEG;IACH,wBAAmB,MAAuB;;QACzC,uBAAU,CAAC,OAAO,GAAG,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,mCAAI,KAAK,CAAC;QAC9C,IAAI,CAAC,MAAM,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,CAAC;QAC7B,IAAI,CAAC,Q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