rettiwt-api 2.4.2 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +5 -0
- package/.github/workflows/documentation.yml +1 -1
- package/.github/workflows/publish.yml +1 -1
- package/.prettierrc +1 -1
- package/README.md +69 -19
- package/dist/Rettiwt.d.ts +7 -4
- package/dist/Rettiwt.js +4 -2
- package/dist/Rettiwt.js.map +1 -1
- package/dist/cli.js +4 -2
- package/dist/cli.js.map +1 -1
- package/dist/commands/Auth.d.ts +2 -1
- package/dist/commands/Auth.js +10 -17
- package/dist/commands/Auth.js.map +1 -1
- package/dist/commands/Tweet.js +36 -3
- package/dist/commands/Tweet.js.map +1 -1
- package/dist/enums/{ApiErrors.js → Api.js} +1 -1
- package/dist/enums/Api.js.map +1 -0
- package/dist/enums/Http.d.ts +68 -0
- package/dist/enums/Http.js +73 -0
- package/dist/enums/Http.js.map +1 -0
- package/dist/enums/Logging.d.ts +1 -0
- package/dist/enums/Logging.js +1 -0
- package/dist/enums/Logging.js.map +1 -1
- package/dist/index.d.ts +15 -14
- package/dist/index.js +22 -17
- package/dist/index.js.map +1 -1
- package/dist/models/args/TweetArgs.d.ts +44 -0
- package/dist/models/args/TweetArgs.js +82 -0
- package/dist/models/args/TweetArgs.js.map +1 -0
- package/dist/models/{public → data}/CursoredData.d.ts +5 -3
- package/dist/models/{public → data}/CursoredData.js +1 -0
- package/dist/models/data/CursoredData.js.map +1 -0
- package/dist/{types/public → models/data}/List.d.ts +8 -1
- package/dist/models/data/List.js.map +1 -0
- package/dist/models/data/Media.d.ts +14 -0
- package/dist/models/data/Media.js +19 -0
- package/dist/models/data/Media.js.map +1 -0
- package/dist/{types/public → models/data}/Tweet.d.ts +26 -8
- package/dist/models/{public → data}/Tweet.js +4 -0
- package/dist/models/data/Tweet.js.map +1 -0
- package/dist/{types/public → models/data}/User.d.ts +8 -1
- package/dist/models/data/User.js.map +1 -0
- package/dist/models/errors/ApiError.d.ts +17 -0
- package/dist/models/errors/ApiError.js +42 -0
- package/dist/models/errors/ApiError.js.map +1 -0
- package/dist/models/errors/HttpError.d.ts +17 -0
- package/dist/models/errors/HttpError.js +42 -0
- package/dist/models/errors/HttpError.js.map +1 -0
- package/dist/models/errors/RettiwtError.d.ts +8 -0
- package/dist/models/errors/RettiwtError.js +34 -0
- package/dist/models/errors/RettiwtError.js.map +1 -0
- package/dist/models/errors/TimeoutError.d.ts +14 -0
- package/dist/models/errors/TimeoutError.js +39 -0
- package/dist/models/errors/TimeoutError.js.map +1 -0
- package/dist/services/internal/ErrorService.d.ts +85 -0
- package/dist/services/internal/ErrorService.js +144 -0
- package/dist/services/internal/ErrorService.js.map +1 -0
- package/dist/services/internal/FetcherService.d.ts +22 -24
- package/dist/services/internal/FetcherService.js +79 -59
- package/dist/services/internal/FetcherService.js.map +1 -1
- package/dist/services/public/AuthService.d.ts +66 -0
- package/dist/services/public/AuthService.js +160 -0
- package/dist/services/public/AuthService.js.map +1 -0
- package/dist/services/public/TweetService.d.ts +27 -8
- package/dist/services/public/TweetService.js +45 -8
- package/dist/services/public/TweetService.js.map +1 -1
- package/dist/services/public/UserService.d.ts +5 -5
- package/dist/services/public/UserService.js.map +1 -1
- package/dist/types/ErrorHandler.d.ts +13 -0
- package/dist/types/{public/User.js → ErrorHandler.js} +1 -1
- package/dist/types/ErrorHandler.js.map +1 -0
- package/dist/types/RettiwtConfig.d.ts +32 -0
- package/dist/types/RettiwtConfig.js.map +1 -0
- package/package.json +6 -5
- package/src/Rettiwt.ts +10 -5
- package/src/cli.ts +4 -2
- package/src/commands/Auth.ts +5 -16
- package/src/commands/Tweet.ts +56 -3
- package/src/enums/Http.ts +68 -0
- package/src/enums/Logging.ts +1 -0
- package/src/index.ts +25 -18
- package/src/models/args/TweetArgs.ts +98 -0
- package/src/models/{public → data}/CursoredData.ts +6 -5
- package/src/models/{public → data}/List.ts +14 -4
- package/src/models/data/Media.ts +19 -0
- package/src/models/{public → data}/Tweet.ts +39 -5
- package/src/models/{public → data}/User.ts +28 -4
- package/src/models/errors/ApiError.ts +24 -0
- package/src/models/errors/HttpError.ts +24 -0
- package/src/models/errors/RettiwtError.ts +12 -0
- package/src/models/errors/TimeoutError.ts +18 -0
- package/src/services/internal/ErrorService.ts +158 -0
- package/src/services/internal/FetcherService.ts +94 -80
- package/src/services/public/AuthService.ts +97 -0
- package/src/services/public/TweetService.ts +48 -10
- package/src/services/public/UserService.ts +7 -5
- package/src/types/ErrorHandler.ts +13 -0
- package/src/types/RettiwtConfig.ts +40 -0
- package/dist/enums/ApiErrors.js.map +0 -1
- package/dist/enums/HTTP.d.ts +0 -17
- package/dist/enums/HTTP.js +0 -22
- package/dist/enums/HTTP.js.map +0 -1
- package/dist/models/internal/RettiwtConfig.d.ts +0 -18
- package/dist/models/internal/RettiwtConfig.js +0 -24
- package/dist/models/internal/RettiwtConfig.js.map +0 -1
- package/dist/models/public/CursoredData.js.map +0 -1
- package/dist/models/public/List.d.ts +0 -22
- package/dist/models/public/List.js.map +0 -1
- package/dist/models/public/Tweet.d.ts +0 -62
- package/dist/models/public/Tweet.js.map +0 -1
- package/dist/models/public/User.d.ts +0 -29
- package/dist/models/public/User.js.map +0 -1
- package/dist/types/internal/RettiwtConfig.d.ts +0 -15
- package/dist/types/internal/RettiwtConfig.js.map +0 -1
- package/dist/types/public/CursoredData.d.ts +0 -22
- package/dist/types/public/CursoredData.js +0 -3
- package/dist/types/public/CursoredData.js.map +0 -1
- package/dist/types/public/List.js +0 -3
- package/dist/types/public/List.js.map +0 -1
- package/dist/types/public/Tweet.js +0 -3
- package/dist/types/public/Tweet.js.map +0 -1
- package/dist/types/public/User.js.map +0 -1
- package/src/enums/HTTP.ts +0 -17
- package/src/models/internal/RettiwtConfig.ts +0 -26
- package/src/types/internal/RettiwtConfig.ts +0 -18
- package/src/types/public/CursoredData.ts +0 -24
- package/src/types/public/List.ts +0 -27
- package/src/types/public/Tweet.ts +0 -86
- package/src/types/public/User.ts +0 -48
- /package/dist/enums/{ApiErrors.d.ts → Api.d.ts} +0 -0
- /package/dist/models/{public → data}/List.js +0 -0
- /package/dist/models/{public → data}/User.js +0 -0
- /package/dist/types/{internal/RettiwtConfig.js → RettiwtConfig.js} +0 -0
- /package/src/enums/{ApiErrors.ts → Api.ts} +0 -0
|
@@ -0,0 +1,17 @@
|
|
|
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 HttpError extends RettiwtError {
|
|
8
|
+
/** The HTTP status code. */
|
|
9
|
+
status: number;
|
|
10
|
+
/**
|
|
11
|
+
* Initializes a new HttpError based on the given error details.
|
|
12
|
+
*
|
|
13
|
+
* @param httpStatus - The HTTP status code received upon making the request
|
|
14
|
+
* @param message - Any additional error message.
|
|
15
|
+
*/
|
|
16
|
+
constructor(httpStatus: number, message?: string);
|
|
17
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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.HttpError = 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 HttpError = /** @class */ (function (_super) {
|
|
27
|
+
__extends(HttpError, _super);
|
|
28
|
+
/**
|
|
29
|
+
* Initializes a new HttpError based on the given error details.
|
|
30
|
+
*
|
|
31
|
+
* @param httpStatus - The HTTP status code received upon making the request
|
|
32
|
+
* @param message - Any additional error message.
|
|
33
|
+
*/
|
|
34
|
+
function HttpError(httpStatus, message) {
|
|
35
|
+
var _this = _super.call(this, message) || this;
|
|
36
|
+
_this.status = httpStatus;
|
|
37
|
+
return _this;
|
|
38
|
+
}
|
|
39
|
+
return HttpError;
|
|
40
|
+
}(RettiwtError_1.RettiwtError));
|
|
41
|
+
exports.HttpError = HttpError;
|
|
42
|
+
//# sourceMappingURL=HttpError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpError.js","sourceRoot":"","sources":["../../../src/models/errors/HttpError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,SAAS;AACT,+CAA8C;AAE9C;;;;GAIG;AACH;IAA+B,6BAAY;IAI1C;;;;;OAKG;IACH,mBAAmB,UAAkB,EAAE,OAAgB;QAAvD,YACC,kBAAM,OAAO,CAAC,SAGd;QADA,KAAI,CAAC,MAAM,GAAG,UAAU,CAAC;;IAC1B,CAAC;IACF,gBAAC;AAAD,CAAC,AAfD,CAA+B,2BAAY,GAe1C;AAfY,8BAAS"}
|
|
@@ -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 {
|
|
2
|
-
import {
|
|
3
|
-
import { CursoredData } from '../../models/
|
|
4
|
-
import { Tweet } from '../../models/
|
|
5
|
-
import { User } from '../../models/
|
|
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?:
|
|
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:
|
|
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:
|
|
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
|
}
|