rettiwt-api 2.4.1 → 2.5.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 (133) hide show
  1. package/.eslintrc.js +5 -0
  2. package/.github/workflows/documentation.yml +1 -1
  3. package/.github/workflows/publish.yml +1 -1
  4. package/.prettierrc +1 -1
  5. package/README.md +118 -60
  6. package/dist/Rettiwt.d.ts +7 -4
  7. package/dist/Rettiwt.js +4 -2
  8. package/dist/Rettiwt.js.map +1 -1
  9. package/dist/cli.js +1 -1
  10. package/dist/cli.js.map +1 -1
  11. package/dist/commands/Auth.d.ts +2 -1
  12. package/dist/commands/Auth.js +10 -17
  13. package/dist/commands/Auth.js.map +1 -1
  14. package/dist/commands/Tweet.js +36 -3
  15. package/dist/commands/Tweet.js.map +1 -1
  16. package/dist/enums/{ApiErrors.js → Api.js} +1 -1
  17. package/dist/enums/Api.js.map +1 -0
  18. package/dist/enums/Http.d.ts +68 -0
  19. package/dist/enums/Http.js +73 -0
  20. package/dist/enums/Http.js.map +1 -0
  21. package/dist/enums/Logging.d.ts +1 -0
  22. package/dist/enums/Logging.js +1 -0
  23. package/dist/enums/Logging.js.map +1 -1
  24. package/dist/index.d.ts +15 -14
  25. package/dist/index.js +21 -17
  26. package/dist/index.js.map +1 -1
  27. package/dist/models/{public → data}/CursoredData.d.ts +5 -3
  28. package/dist/models/{public → data}/CursoredData.js +1 -0
  29. package/dist/models/data/CursoredData.js.map +1 -0
  30. package/dist/{types/public → models/data}/List.d.ts +8 -1
  31. package/dist/models/data/List.js.map +1 -0
  32. package/dist/models/data/Media.d.ts +14 -0
  33. package/dist/models/data/Media.js +19 -0
  34. package/dist/models/data/Media.js.map +1 -0
  35. package/dist/{types/public → models/data}/Tweet.d.ts +26 -8
  36. package/dist/models/{public → data}/Tweet.js +4 -0
  37. package/dist/models/data/Tweet.js.map +1 -0
  38. package/dist/{types/public → models/data}/User.d.ts +8 -1
  39. package/dist/models/data/User.js.map +1 -0
  40. package/dist/models/errors/ApiError.d.ts +17 -0
  41. package/dist/models/errors/ApiError.js +42 -0
  42. package/dist/models/errors/ApiError.js.map +1 -0
  43. package/dist/models/errors/HttpError.d.ts +17 -0
  44. package/dist/models/errors/HttpError.js +42 -0
  45. package/dist/models/errors/HttpError.js.map +1 -0
  46. package/dist/models/errors/RettiwtError.d.ts +8 -0
  47. package/dist/models/errors/RettiwtError.js +34 -0
  48. package/dist/models/errors/RettiwtError.js.map +1 -0
  49. package/dist/models/errors/TimeoutError.d.ts +14 -0
  50. package/dist/models/errors/TimeoutError.js +39 -0
  51. package/dist/models/errors/TimeoutError.js.map +1 -0
  52. package/dist/services/internal/ErrorService.d.ts +85 -0
  53. package/dist/services/internal/ErrorService.js +144 -0
  54. package/dist/services/internal/ErrorService.js.map +1 -0
  55. package/dist/services/internal/FetcherService.d.ts +22 -24
  56. package/dist/services/internal/FetcherService.js +79 -59
  57. package/dist/services/internal/FetcherService.js.map +1 -1
  58. package/dist/services/public/AuthService.d.ts +66 -0
  59. package/dist/services/public/AuthService.js +160 -0
  60. package/dist/services/public/AuthService.js.map +1 -0
  61. package/dist/services/public/TweetService.d.ts +27 -8
  62. package/dist/services/public/TweetService.js +40 -5
  63. package/dist/services/public/TweetService.js.map +1 -1
  64. package/dist/services/public/UserService.d.ts +5 -5
  65. package/dist/services/public/UserService.js.map +1 -1
  66. package/dist/types/ErrorHandler.d.ts +13 -0
  67. package/dist/types/{public/User.js → ErrorHandler.js} +1 -1
  68. package/dist/types/ErrorHandler.js.map +1 -0
  69. package/dist/types/RettiwtConfig.d.ts +32 -0
  70. package/dist/types/RettiwtConfig.js.map +1 -0
  71. package/dist/types/args/TweetMediaArgs.d.ts +15 -0
  72. package/dist/types/{public/CursoredData.js → args/TweetMediaArgs.js} +1 -1
  73. package/dist/types/args/TweetMediaArgs.js.map +1 -0
  74. package/package.json +5 -5
  75. package/src/Rettiwt.ts +10 -5
  76. package/src/cli.ts +1 -1
  77. package/src/commands/Auth.ts +5 -16
  78. package/src/commands/Tweet.ts +56 -3
  79. package/src/enums/Http.ts +68 -0
  80. package/src/enums/Logging.ts +1 -0
  81. package/src/index.ts +23 -18
  82. package/src/models/{public → data}/CursoredData.ts +6 -5
  83. package/src/models/{public → data}/List.ts +14 -4
  84. package/src/models/data/Media.ts +19 -0
  85. package/src/models/{public → data}/Tweet.ts +39 -5
  86. package/src/models/{public → data}/User.ts +28 -4
  87. package/src/models/errors/ApiError.ts +24 -0
  88. package/src/models/errors/HttpError.ts +24 -0
  89. package/src/models/errors/RettiwtError.ts +12 -0
  90. package/src/models/errors/TimeoutError.ts +18 -0
  91. package/src/services/internal/ErrorService.ts +158 -0
  92. package/src/services/internal/FetcherService.ts +94 -80
  93. package/src/services/public/AuthService.ts +97 -0
  94. package/src/services/public/TweetService.ts +45 -10
  95. package/src/services/public/UserService.ts +7 -5
  96. package/src/types/ErrorHandler.ts +13 -0
  97. package/src/types/RettiwtConfig.ts +40 -0
  98. package/src/types/args/TweetMediaArgs.ts +16 -0
  99. package/dist/enums/ApiErrors.js.map +0 -1
  100. package/dist/enums/HTTP.d.ts +0 -17
  101. package/dist/enums/HTTP.js +0 -22
  102. package/dist/enums/HTTP.js.map +0 -1
  103. package/dist/models/internal/RettiwtConfig.d.ts +0 -18
  104. package/dist/models/internal/RettiwtConfig.js +0 -24
  105. package/dist/models/internal/RettiwtConfig.js.map +0 -1
  106. package/dist/models/public/CursoredData.js.map +0 -1
  107. package/dist/models/public/List.d.ts +0 -22
  108. package/dist/models/public/List.js.map +0 -1
  109. package/dist/models/public/Tweet.d.ts +0 -62
  110. package/dist/models/public/Tweet.js.map +0 -1
  111. package/dist/models/public/User.d.ts +0 -29
  112. package/dist/models/public/User.js.map +0 -1
  113. package/dist/types/internal/RettiwtConfig.d.ts +0 -15
  114. package/dist/types/internal/RettiwtConfig.js.map +0 -1
  115. package/dist/types/public/CursoredData.d.ts +0 -22
  116. package/dist/types/public/CursoredData.js.map +0 -1
  117. package/dist/types/public/List.js +0 -3
  118. package/dist/types/public/List.js.map +0 -1
  119. package/dist/types/public/Tweet.js +0 -3
  120. package/dist/types/public/Tweet.js.map +0 -1
  121. package/dist/types/public/User.js.map +0 -1
  122. package/src/enums/HTTP.ts +0 -17
  123. package/src/models/internal/RettiwtConfig.ts +0 -26
  124. package/src/types/internal/RettiwtConfig.ts +0 -18
  125. package/src/types/public/CursoredData.ts +0 -24
  126. package/src/types/public/List.ts +0 -27
  127. package/src/types/public/Tweet.ts +0 -86
  128. package/src/types/public/User.ts +0 -48
  129. /package/dist/enums/{ApiErrors.d.ts → Api.d.ts} +0 -0
  130. /package/dist/models/{public → data}/List.js +0 -0
  131. /package/dist/models/{public → data}/User.js +0 -0
  132. /package/dist/types/{internal/RettiwtConfig.js → RettiwtConfig.js} +0 -0
  133. /package/src/enums/{ApiErrors.ts → Api.ts} +0 -0
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Represents an error that arises inside the package.
3
+ *
4
+ * @internal
5
+ */
6
+ export declare class RettiwtError extends Error {
7
+ constructor(message?: string);
8
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.RettiwtError = void 0;
19
+ /**
20
+ * Represents an error that arises inside the package.
21
+ *
22
+ * @internal
23
+ */
24
+ var RettiwtError = /** @class */ (function (_super) {
25
+ __extends(RettiwtError, _super);
26
+ function RettiwtError(message) {
27
+ var _this = _super.call(this, message) || this;
28
+ Object.setPrototypeOf(_this, RettiwtError.prototype);
29
+ return _this;
30
+ }
31
+ return RettiwtError;
32
+ }(Error));
33
+ exports.RettiwtError = RettiwtError;
34
+ //# sourceMappingURL=RettiwtError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RettiwtError.js","sourceRoot":"","sources":["../../../src/models/errors/RettiwtError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH;IAAkC,gCAAK;IACtC,sBAAmB,OAAgB;QAAnC,YACC,kBAAM,OAAO,CAAC,SAGd;QADA,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC;;IACrD,CAAC;IACF,mBAAC;AAAD,CAAC,AAND,CAAkC,KAAK,GAMtC;AANY,oCAAY"}
@@ -0,0 +1,14 @@
1
+ import { RettiwtError } from './RettiwtError';
2
+ /**
3
+ * Represents an HTTP error that occues while making a request to Twitter API.
4
+ *
5
+ * @internal
6
+ */
7
+ export declare class TimeoutError extends RettiwtError {
8
+ /**
9
+ * Initializes a new TimeoutError based on the given error details.
10
+ *
11
+ * @param message - Error message with the configured timeout.
12
+ */
13
+ constructor(message?: string);
14
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.TimeoutError = void 0;
19
+ // ERRORS
20
+ var RettiwtError_1 = require("./RettiwtError");
21
+ /**
22
+ * Represents an HTTP error that occues while making a request to Twitter API.
23
+ *
24
+ * @internal
25
+ */
26
+ var TimeoutError = /** @class */ (function (_super) {
27
+ __extends(TimeoutError, _super);
28
+ /**
29
+ * Initializes a new TimeoutError based on the given error details.
30
+ *
31
+ * @param message - Error message with the configured timeout.
32
+ */
33
+ function TimeoutError(message) {
34
+ return _super.call(this, message) || this;
35
+ }
36
+ return TimeoutError;
37
+ }(RettiwtError_1.RettiwtError));
38
+ exports.TimeoutError = TimeoutError;
39
+ //# sourceMappingURL=TimeoutError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TimeoutError.js","sourceRoot":"","sources":["../../../src/models/errors/TimeoutError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,SAAS;AACT,+CAA8C;AAE9C;;;;GAIG;AACH;IAAkC,gCAAY;IAC7C;;;;OAIG;IACH,sBAAmB,OAAgB;eAClC,kBAAM,OAAO,CAAC;IACf,CAAC;IACF,mBAAC;AAAD,CAAC,AATD,CAAkC,2BAAY,GAS7C;AATY,oCAAY"}
@@ -0,0 +1,85 @@
1
+ import { AxiosError, AxiosResponse } from 'axios';
2
+ import { IErrorHandler } from '../../types/ErrorHandler';
3
+ import { ApiError } from '../../models/errors/ApiError';
4
+ import { HttpError } from '../../models/errors/HttpError';
5
+ /**
6
+ * The base service that handles any errors.
7
+ *
8
+ * @public
9
+ */
10
+ export declare class ErrorService implements IErrorHandler {
11
+ /**
12
+ * Error message used when the specific error type is not defined in the required enums.
13
+ */
14
+ protected static readonly DEFAULT_ERROR_MESSAGE = "Unknown error";
15
+ /**
16
+ * The method called when an error response is received from Twitter API.
17
+ *
18
+ * @param error - The error caught while making HTTP request to Twitter API.
19
+ */
20
+ handle(error: unknown): void;
21
+ /**
22
+ * Handles exceeded timeout, configured in RettiwtConfig.
23
+ *
24
+ * @param error - The error object.
25
+ * @throws An error if the configured request timeout has been exceeded.
26
+ */
27
+ protected handleTimeoutError(error: AxiosError): void;
28
+ /**
29
+ * Retrieves the response data from the given error.
30
+ *
31
+ * @param error - The error object.
32
+ * @returns The response data.
33
+ * @throws The original error if it is not an HTTP error with a response.
34
+ */
35
+ protected getAxiosResponse(error: AxiosError): AxiosResponse;
36
+ /**
37
+ * Handles HTTP error in a response.
38
+ *
39
+ * @param response - The response object received.
40
+ * @throws An error with the corresponding HTTP status text if any HTTP-related error has occurred.
41
+ */
42
+ protected handleHttpError(response: AxiosResponse): void;
43
+ /**
44
+ * Handles API error in a response.
45
+ *
46
+ * @param response - The response object received.
47
+ * @throws An error with the corresponding API error message if any API-related error has occurred.
48
+ */
49
+ protected handleApiError(response: AxiosResponse): void;
50
+ /**
51
+ * Creates an HTTP error instance based on the provided HTTP status.
52
+ *
53
+ * @param httpStatus - The HTTP status code.
54
+ * @returns An HTTP error instance.
55
+ */
56
+ protected createHttpError(httpStatus: number): HttpError;
57
+ /**
58
+ * Retrieves the HTTP error message based on the provided HTTP status.
59
+ *
60
+ * @param httpStatus - The HTTP status code.
61
+ * @returns The HTTP error message.
62
+ */
63
+ protected getHttpErrorMessage(httpStatus: number): string;
64
+ /**
65
+ * Retrieves the API error code from the Axios response data.
66
+ *
67
+ * @param response - The response object received.
68
+ * @returns The error code, or undefined if not found.
69
+ */
70
+ protected getErrorCode(response: AxiosResponse): number | undefined;
71
+ /**
72
+ * Creates an API error instance based on the provided error code.
73
+ *
74
+ * @param errorCode - The error code.
75
+ * @returns An API error instance.
76
+ */
77
+ protected createApiError(errorCode: number): ApiError;
78
+ /**
79
+ * Retrieves the API error message based on the provided error code.
80
+ *
81
+ * @param errorCode - The error code.
82
+ * @returns The API error message.
83
+ */
84
+ protected getApiErrorMessage(errorCode: number): string;
85
+ }
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ErrorService = void 0;
7
+ // PACKAGES
8
+ var axios_1 = __importDefault(require("axios"));
9
+ var JsonUtils_1 = require("../../helper/JsonUtils");
10
+ // ENUMS
11
+ var Api_1 = require("../../enums/Api");
12
+ var Http_1 = require("../../enums/Http");
13
+ var rettiwt_core_1 = require("rettiwt-core");
14
+ // ERRORS
15
+ var ApiError_1 = require("../../models/errors/ApiError");
16
+ var HttpError_1 = require("../../models/errors/HttpError");
17
+ var TimeoutError_1 = require("../../models/errors/TimeoutError");
18
+ // TODO Refactor and document this module
19
+ /**
20
+ * The base service that handles any errors.
21
+ *
22
+ * @public
23
+ */
24
+ var ErrorService = exports.ErrorService = /** @class */ (function () {
25
+ function ErrorService() {
26
+ }
27
+ /**
28
+ * The method called when an error response is received from Twitter API.
29
+ *
30
+ * @param error - The error caught while making HTTP request to Twitter API.
31
+ */
32
+ ErrorService.prototype.handle = function (error) {
33
+ if (!axios_1.default.isAxiosError(error)) {
34
+ throw error;
35
+ }
36
+ this.handleTimeoutError(error);
37
+ var axiosResponse = this.getAxiosResponse(error);
38
+ this.handleApiError(axiosResponse);
39
+ this.handleHttpError(axiosResponse);
40
+ };
41
+ /**
42
+ * Handles exceeded timeout, configured in RettiwtConfig.
43
+ *
44
+ * @param error - The error object.
45
+ * @throws An error if the configured request timeout has been exceeded.
46
+ */
47
+ ErrorService.prototype.handleTimeoutError = function (error) {
48
+ if (error.code === 'ECONNABORTED') {
49
+ throw new TimeoutError_1.TimeoutError(error.message);
50
+ }
51
+ };
52
+ /**
53
+ * Retrieves the response data from the given error.
54
+ *
55
+ * @param error - The error object.
56
+ * @returns The response data.
57
+ * @throws The original error if it is not an HTTP error with a response.
58
+ */
59
+ ErrorService.prototype.getAxiosResponse = function (error) {
60
+ if (error.response) {
61
+ return error.response;
62
+ }
63
+ throw error;
64
+ };
65
+ /**
66
+ * Handles HTTP error in a response.
67
+ *
68
+ * @param response - The response object received.
69
+ * @throws An error with the corresponding HTTP status text if any HTTP-related error has occurred.
70
+ */
71
+ ErrorService.prototype.handleHttpError = function (response) {
72
+ throw this.createHttpError(response.status);
73
+ };
74
+ /**
75
+ * Handles API error in a response.
76
+ *
77
+ * @param response - The response object received.
78
+ * @throws An error with the corresponding API error message if any API-related error has occurred.
79
+ */
80
+ ErrorService.prototype.handleApiError = function (response) {
81
+ var errorCode = this.getErrorCode(response);
82
+ if (errorCode === undefined) {
83
+ return;
84
+ }
85
+ throw this.createApiError(errorCode);
86
+ };
87
+ /**
88
+ * Creates an HTTP error instance based on the provided HTTP status.
89
+ *
90
+ * @param httpStatus - The HTTP status code.
91
+ * @returns An HTTP error instance.
92
+ */
93
+ ErrorService.prototype.createHttpError = function (httpStatus) {
94
+ return new HttpError_1.HttpError(httpStatus, this.getHttpErrorMessage(httpStatus));
95
+ };
96
+ /**
97
+ * Retrieves the HTTP error message based on the provided HTTP status.
98
+ *
99
+ * @param httpStatus - The HTTP status code.
100
+ * @returns The HTTP error message.
101
+ */
102
+ ErrorService.prototype.getHttpErrorMessage = function (httpStatus) {
103
+ return Object.values(Http_1.EHttpStatus).includes(httpStatus)
104
+ ? Http_1.EHttpStatus[httpStatus]
105
+ : ErrorService.DEFAULT_ERROR_MESSAGE;
106
+ };
107
+ /**
108
+ * Retrieves the API error code from the Axios response data.
109
+ *
110
+ * @param response - The response object received.
111
+ * @returns The error code, or undefined if not found.
112
+ */
113
+ ErrorService.prototype.getErrorCode = function (response) {
114
+ var errors = response.data.errors;
115
+ return !!errors && errors.length ? errors[0].code : undefined;
116
+ };
117
+ /**
118
+ * Creates an API error instance based on the provided error code.
119
+ *
120
+ * @param errorCode - The error code.
121
+ * @returns An API error instance.
122
+ */
123
+ ErrorService.prototype.createApiError = function (errorCode) {
124
+ return new ApiError_1.ApiError(errorCode, this.getApiErrorMessage(errorCode));
125
+ };
126
+ /**
127
+ * Retrieves the API error message based on the provided error code.
128
+ *
129
+ * @param errorCode - The error code.
130
+ * @returns The API error message.
131
+ */
132
+ ErrorService.prototype.getApiErrorMessage = function (errorCode) {
133
+ var errorCodeKey = (0, JsonUtils_1.findKeyByValue)(rettiwt_core_1.EErrorCodes, errorCode.toString());
134
+ return !!errorCodeKey && errorCodeKey in Api_1.EApiErrors
135
+ ? Api_1.EApiErrors[errorCodeKey]
136
+ : ErrorService.DEFAULT_ERROR_MESSAGE;
137
+ };
138
+ /**
139
+ * Error message used when the specific error type is not defined in the required enums.
140
+ */
141
+ ErrorService.DEFAULT_ERROR_MESSAGE = 'Unknown error';
142
+ return ErrorService;
143
+ }());
144
+ //# sourceMappingURL=ErrorService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorService.js","sourceRoot":"","sources":["../../../src/services/internal/ErrorService.ts"],"names":[],"mappings":"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"}
@@ -1,8 +1,8 @@
1
- import { Args, EResourceType } from 'rettiwt-core';
2
- import { RettiwtConfig } from '../../models/internal/RettiwtConfig';
3
- import { CursoredData } from '../../models/public/CursoredData';
4
- import { Tweet } from '../../models/public/Tweet';
5
- import { User } from '../../models/public/User';
1
+ import { FetchArgs, PostArgs, EResourceType } from 'rettiwt-core';
2
+ import { IRettiwtConfig } from '../../types/RettiwtConfig';
3
+ import { CursoredData } from '../../models/data/CursoredData';
4
+ import { Tweet } from '../../models/data/Tweet';
5
+ import { User } from '../../models/data/User';
6
6
  /**
7
7
  * The base service that handles all HTTP requests.
8
8
  *
@@ -13,14 +13,20 @@ export declare class FetcherService {
13
13
  private cred?;
14
14
  /** Whether the instance is authenticated or not. */
15
15
  private readonly isAuthenticated;
16
+ /** The URL to the proxy server to use for authentication. */
17
+ protected readonly authProxyUrl?: URL;
16
18
  /** The HTTPS Agent to use for requests to Twitter API. */
17
19
  private readonly httpsAgent;
20
+ /** The max wait time for a response. */
21
+ private readonly timeout;
18
22
  /** The log service instance to use to logging. */
19
23
  private readonly logger;
24
+ /** The service used to handle HTTP and API errors */
25
+ private readonly errorHandler;
20
26
  /**
21
27
  * @param config - The config object for configuring the Rettiwt instance.
22
28
  */
23
- constructor(config?: RettiwtConfig);
29
+ constructor(config?: IRettiwtConfig);
24
30
  /**
25
31
  * Returns an AuthCredential generated using the given API key.
26
32
  *
@@ -49,25 +55,10 @@ export declare class FetcherService {
49
55
  * @returns The HttpsAgent to use.
50
56
  */
51
57
  private getHttpsAgent;
52
- /**
53
- * The middleware for handling any http error.
54
- *
55
- * @param res - The response object received.
56
- * @returns The received response, if no HTTP errors are found.
57
- * @throws An error if any HTTP-related error has occured.
58
- */
59
- private handleHttpError;
60
- /**
61
- * The middleware for handling any Twitter API-level errors.
62
- *
63
- * @param res - The response object received.
64
- * @returns The received response, if no API errors are found.
65
- * @throws An error if any API-related error has occured.
66
- */
67
- private handleApiError;
68
58
  /**
69
59
  * Makes an HTTP request according to the given parameters.
70
60
  *
61
+ * @typeParam ResType - The type of the returned response data.
71
62
  * @param config - The request configuration.
72
63
  * @returns The response received.
73
64
  */
@@ -96,7 +87,7 @@ export declare class FetcherService {
96
87
  * @typeParam OutType - The type of deserialized data returned.
97
88
  * @returns The processed data requested from Twitter.
98
89
  */
99
- protected fetch<OutType extends Tweet | User>(resourceType: EResourceType, args: Args): Promise<CursoredData<OutType>>;
90
+ protected fetch<OutType extends Tweet | User>(resourceType: EResourceType, args: FetchArgs): Promise<CursoredData<OutType>>;
100
91
  /**
101
92
  * Posts the requested resource to Twitter and returns the response.
102
93
  *
@@ -104,5 +95,12 @@ export declare class FetcherService {
104
95
  * @param args - Resource specific arguments.
105
96
  * @returns Whether posting was successful or not.
106
97
  */
107
- protected post(resourceType: EResourceType, args: Args): Promise<boolean>;
98
+ protected post(resourceType: EResourceType, args: PostArgs): Promise<boolean>;
99
+ /**
100
+ * Uploads the given media file to Twitter
101
+ *
102
+ * @param media - The path to the media file to upload.
103
+ * @returns The id of the uploaded media.
104
+ */
105
+ protected upload(media: string): Promise<string>;
108
106
  }
@@ -1,4 +1,15 @@
1
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
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -47,16 +58,18 @@ var https_1 = __importDefault(require("https"));
47
58
  var rettiwt_auth_1 = require("rettiwt-auth");
48
59
  var https_proxy_agent_1 = require("https-proxy-agent");
49
60
  // SERVICES
61
+ var ErrorService_1 = require("./ErrorService");
50
62
  var LogService_1 = require("./LogService");
51
63
  // ENUMS
52
- var HTTP_1 = require("../../enums/HTTP");
53
- var ApiErrors_1 = require("../../enums/ApiErrors");
64
+ var Api_1 = require("../../enums/Api");
54
65
  var Logging_1 = require("../../enums/Logging");
55
- var CursoredData_1 = require("../../models/public/CursoredData");
56
- var Tweet_1 = require("../../models/public/Tweet");
57
- var User_1 = require("../../models/public/User");
66
+ // MODELS
67
+ var CursoredData_1 = require("../../models/data/CursoredData");
68
+ var Tweet_1 = require("../../models/data/Tweet");
69
+ var User_1 = require("../../models/data/User");
58
70
  // HELPERS
59
71
  var JsonUtils_1 = require("../../helper/JsonUtils");
72
+ var fs_1 = require("fs");
60
73
  /**
61
74
  * The base service that handles all HTTP requests.
62
75
  *
@@ -67,6 +80,7 @@ var FetcherService = /** @class */ (function () {
67
80
  * @param config - The config object for configuring the Rettiwt instance.
68
81
  */
69
82
  function FetcherService(config) {
83
+ var _a, _b, _c;
70
84
  // If API key is supplied
71
85
  if (config === null || config === void 0 ? void 0 : config.apiKey) {
72
86
  this.cred = this.getAuthCredential(config.apiKey);
@@ -80,8 +94,11 @@ var FetcherService = /** @class */ (function () {
80
94
  this.cred = undefined;
81
95
  }
82
96
  this.isAuthenticated = (config === null || config === void 0 ? void 0 : config.apiKey) ? true : false;
97
+ this.authProxyUrl = (_a = config === null || config === void 0 ? void 0 : config.authProxyUrl) !== null && _a !== void 0 ? _a : config === null || config === void 0 ? void 0 : config.proxyUrl;
83
98
  this.httpsAgent = this.getHttpsAgent(config === null || config === void 0 ? void 0 : config.proxyUrl);
99
+ this.timeout = (_b = config === null || config === void 0 ? void 0 : config.timeout) !== null && _b !== void 0 ? _b : 0;
84
100
  this.logger = new LogService_1.LogService(config === null || config === void 0 ? void 0 : config.logging);
101
+ this.errorHandler = (_c = config === null || config === void 0 ? void 0 : config.errorHandler) !== null && _c !== void 0 ? _c : new ErrorService_1.ErrorService();
85
102
  }
86
103
  /**
87
104
  * Returns an AuthCredential generated using the given API key.
@@ -101,8 +118,6 @@ var FetcherService = /** @class */ (function () {
101
118
  * @returns The generated AuthCredential.
102
119
  */
103
120
  FetcherService.prototype.getGuestCredential = function (guestKey) {
104
- // Converting guestKey from base64 to string
105
- guestKey = Buffer.from(guestKey).toString('ascii');
106
121
  return new rettiwt_auth_1.AuthCredential(undefined, guestKey);
107
122
  };
108
123
  /**
@@ -119,7 +134,7 @@ var FetcherService = /** @class */ (function () {
119
134
  resourceType != rettiwt_core_1.EResourceType.USER_DETAILS &&
120
135
  resourceType != rettiwt_core_1.EResourceType.USER_TWEETS &&
121
136
  this.isAuthenticated == false) {
122
- throw new Error(ApiErrors_1.EApiErrors.RESOURCE_NOT_ALLOWED);
137
+ throw new Error(Api_1.EApiErrors.RESOURCE_NOT_ALLOWED);
123
138
  }
124
139
  };
125
140
  /**
@@ -134,82 +149,46 @@ var FetcherService = /** @class */ (function () {
134
149
  }
135
150
  return new https_1.default.Agent();
136
151
  };
137
- /**
138
- * The middleware for handling any http error.
139
- *
140
- * @param res - The response object received.
141
- * @returns The received response, if no HTTP errors are found.
142
- * @throws An error if any HTTP-related error has occured.
143
- */
144
- FetcherService.prototype.handleHttpError = function (res) {
145
- /**
146
- * If the status code is not 200 =\> the HTTP request was not successful. hence throwing error
147
- */
148
- if (res.status != 200 && res.status in HTTP_1.EHttpStatus) {
149
- throw new Error(HTTP_1.EHttpStatus[res.status]);
150
- }
151
- return res;
152
- };
153
- /**
154
- * The middleware for handling any Twitter API-level errors.
155
- *
156
- * @param res - The response object received.
157
- * @returns The received response, if no API errors are found.
158
- * @throws An error if any API-related error has occured.
159
- */
160
- FetcherService.prototype.handleApiError = function (res) {
161
- // If error exists
162
- if (res.data.errors && res.data.errors.length) {
163
- // Getting the error code
164
- var code = res.data.errors[0].code;
165
- // Getting the error message
166
- var message = ApiErrors_1.EApiErrors[(0, JsonUtils_1.findKeyByValue)(rettiwt_core_1.EErrorCodes, "".concat(code))];
167
- // Throw the error
168
- throw new Error(message);
169
- }
170
- return res;
171
- };
172
152
  /**
173
153
  * Makes an HTTP request according to the given parameters.
174
154
  *
155
+ * @typeParam ResType - The type of the returned response data.
175
156
  * @param config - The request configuration.
176
157
  * @returns The response received.
177
158
  */
178
159
  FetcherService.prototype.request = function (config) {
179
160
  var _a;
180
161
  return __awaiter(this, void 0, void 0, function () {
181
- var _b, _c, axiosRequest;
162
+ var _b, _c;
182
163
  var _this = this;
183
164
  return __generator(this, function (_d) {
184
165
  switch (_d.label) {
185
166
  case 0:
186
167
  // Checking authorization for the requested resource
187
- this.checkAuthorization(config.endpoint);
168
+ this.checkAuthorization(config.url);
188
169
  // If not authenticated, use guest authentication
189
170
  _b = this;
190
171
  if (!((_a = this.cred) !== null && _a !== void 0)) return [3 /*break*/, 1];
191
172
  _c = _a;
192
173
  return [3 /*break*/, 3];
193
- case 1: return [4 /*yield*/, new rettiwt_auth_1.Auth().getGuestCredential()];
174
+ case 1: return [4 /*yield*/, new rettiwt_auth_1.Auth({ proxyUrl: this.authProxyUrl }).getGuestCredential()];
194
175
  case 2:
195
176
  _c = (_d.sent());
196
177
  _d.label = 3;
197
178
  case 3:
198
179
  // If not authenticated, use guest authentication
199
180
  _b.cred = _c;
200
- axiosRequest = {
201
- url: config.url,
202
- method: config.type,
203
- data: config.payload,
204
- headers: JSON.parse(JSON.stringify(this.cred.toHeader())),
205
- httpsAgent: this.httpsAgent,
206
- };
207
- return [4 /*yield*/, (0, axios_1.default)(axiosRequest)
208
- .then(function (res) { return _this.handleHttpError(res); })
209
- .then(function (res) { return _this.handleApiError(res); })];
181
+ // Setting additional request parameters
182
+ config.headers = __assign(__assign({}, config.headers), this.cred.toHeader());
183
+ config.httpAgent = this.httpsAgent;
184
+ config.timeout = this.timeout;
185
+ return [4 /*yield*/, (0, axios_1.default)(config).catch(function (error) {
186
+ _this.errorHandler.handle(error);
187
+ throw error;
188
+ })];
210
189
  case 4:
211
190
  /**
212
- * After making the request, the response is then passed to HTTP error handling middleware for HTTP error handling.
191
+ * If Axios request results in an error, catch it and rethrow a more specific error.
213
192
  */
214
193
  return [2 /*return*/, _d.sent()];
215
194
  }
@@ -301,7 +280,7 @@ var FetcherService = /** @class */ (function () {
301
280
  case 0:
302
281
  // Logging
303
282
  this.logger.log(Logging_1.ELogActions.FETCH, { resourceType: resourceType, args: args });
304
- request = new rettiwt_core_1.Request(resourceType, args);
283
+ request = new rettiwt_core_1.Request(resourceType, args).toAxiosRequestConfig();
305
284
  return [4 /*yield*/, this.request(request).then(function (res) { return res.data; })];
306
285
  case 1:
307
286
  res = _a.sent();
@@ -327,7 +306,7 @@ var FetcherService = /** @class */ (function () {
327
306
  case 0:
328
307
  // Logging
329
308
  this.logger.log(Logging_1.ELogActions.POST, { resourceType: resourceType, args: args });
330
- request = new rettiwt_core_1.Request(resourceType, args);
309
+ request = new rettiwt_core_1.Request(resourceType, args).toAxiosRequestConfig();
331
310
  // Posting the data
332
311
  return [4 /*yield*/, this.request(request)];
333
312
  case 1:
@@ -338,6 +317,47 @@ var FetcherService = /** @class */ (function () {
338
317
  });
339
318
  });
340
319
  };
320
+ /**
321
+ * Uploads the given media file to Twitter
322
+ *
323
+ * @param media - The path to the media file to upload.
324
+ * @returns The id of the uploaded media.
325
+ */
326
+ FetcherService.prototype.upload = function (media) {
327
+ return __awaiter(this, void 0, void 0, function () {
328
+ var id;
329
+ return __generator(this, function (_a) {
330
+ switch (_a.label) {
331
+ case 0:
332
+ // INITIALIZE
333
+ // Logging
334
+ this.logger.log(Logging_1.ELogActions.UPLOAD, { step: rettiwt_core_1.EUploadSteps.INITIALIZE });
335
+ return [4 /*yield*/, this.request(new rettiwt_core_1.Request(rettiwt_core_1.EResourceType.MEDIA_UPLOAD, {
336
+ upload: { step: rettiwt_core_1.EUploadSteps.INITIALIZE, size: (0, fs_1.statSync)(media).size },
337
+ }).toAxiosRequestConfig())];
338
+ case 1:
339
+ id = (_a.sent()).data.media_id_string;
340
+ // APPEND
341
+ // Logging
342
+ this.logger.log(Logging_1.ELogActions.UPLOAD, { step: rettiwt_core_1.EUploadSteps.APPEND });
343
+ return [4 /*yield*/, this.request(new rettiwt_core_1.Request(rettiwt_core_1.EResourceType.MEDIA_UPLOAD, {
344
+ upload: { step: rettiwt_core_1.EUploadSteps.APPEND, id: id, media: media },
345
+ }).toAxiosRequestConfig())];
346
+ case 2:
347
+ _a.sent();
348
+ // FINALIZE
349
+ // Logging
350
+ this.logger.log(Logging_1.ELogActions.UPLOAD, { step: rettiwt_core_1.EUploadSteps.APPEND });
351
+ return [4 /*yield*/, this.request(new rettiwt_core_1.Request(rettiwt_core_1.EResourceType.MEDIA_UPLOAD, {
352
+ upload: { step: rettiwt_core_1.EUploadSteps.FINALIZE, id: id },
353
+ }).toAxiosRequestConfig())];
354
+ case 3:
355
+ _a.sent();
356
+ return [2 /*return*/, id];
357
+ }
358
+ });
359
+ });
360
+ };
341
361
  return FetcherService;
342
362
  }());
343
363
  exports.FetcherService = FetcherService;