UNPKG

4.93 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var ErrorHandler_1 = require("./errors/ErrorHandler");
4var HttpClient_1 = require("./HttpClient");
5var packageJson = require("../../package.json");
6var CLIENT_VERSION = packageJson.version;
7/**
8 * Base client class from which client classes can be implemented, in our case, AccountClient and ServerClient classes.
9 * This class is NOT intended to be instantiated directly.
10 */
11var BaseClient = /** @class */ (function () {
12 function BaseClient(token, authHeader, configOptions) {
13 this.errorHandler = new ErrorHandler_1.ErrorHandler();
14 this.verifyToken(token);
15 this.token = token.trim();
16 this.authHeader = authHeader;
17 this.clientVersion = CLIENT_VERSION;
18 this.httpClient = new HttpClient_1.AxiosHttpClient(configOptions);
19 }
20 BaseClient.prototype.setClientOptions = function (configOptions) {
21 this.httpClient.initHttpClient(configOptions);
22 };
23 BaseClient.prototype.getClientOptions = function () {
24 return this.httpClient.clientOptions;
25 };
26 /**
27 * Process http request with sending body - data.
28 *
29 * @see processRequest for more details.
30 */
31 BaseClient.prototype.processRequestWithBody = function (method, path, body, callback) {
32 return this.processRequest(method, path, {}, body, callback);
33 };
34 /**
35 * Process http request without sending body - data.
36 *
37 * @see processRequest for more details.
38 */
39 BaseClient.prototype.processRequestWithoutBody = function (method, path, queryParameters, callback) {
40 if (queryParameters === void 0) { queryParameters = {}; }
41 return this.processRequest(method, path, queryParameters, null, callback);
42 };
43 /**
44 * Process request for Postmark ClientOptions.
45 *
46 * @param method - see processHttpRequest for details
47 * @param path - see processHttpRequest for details
48 * @param queryParameters - see processHttpRequest for details
49 * @param body - see processHttpRequest for details
50 * @param callback - callback function to be executed.
51 *
52 * @returns A promise that will complete when the API responds (or an error occurs).
53 */
54 BaseClient.prototype.processRequest = function (method, path, queryParameters, body, callback) {
55 var httpRequest = this.processHttpRequest(method, path, queryParameters, body);
56 this.processCallbackRequest(httpRequest, callback);
57 return httpRequest;
58 };
59 /**
60 * Process HTTP request.
61 *
62 * @param method - Which type of http request will be executed.
63 * @param path - API URL endpoint.
64 * @param queryParameters - Querystring parameters used for http request.
65 * @param body - Data sent with http request.
66 *
67 * @returns A promise that will complete when the API responds (or an error occurs).
68 */
69 BaseClient.prototype.processHttpRequest = function (method, path, queryParameters, body) {
70 return this.httpClient.httpRequest(method, path, queryParameters, body, this.getComposedHttpRequestHeaders())
71 .then(function (response) { return response; })
72 .catch(function (error) { return Promise.reject(error); });
73 };
74 /**
75 * Process callback function for HTTP request.
76 *
77 * @param httpRequest - HTTP request for which callback will be executed
78 * @param callback - callback function to be executed.
79 */
80 BaseClient.prototype.processCallbackRequest = function (httpRequest, callback) {
81 if (callback) {
82 httpRequest
83 .then(function (response) { return callback(null, response); })
84 .catch(function (error) { return callback(error, null); });
85 }
86 };
87 /**
88 * JSON object with default headers sent by HTTP request.
89 */
90 BaseClient.prototype.getComposedHttpRequestHeaders = function () {
91 var _a;
92 return _a = {},
93 _a[this.authHeader] = this.token,
94 _a["Accept"] = "application/json",
95 _a["Content-Type"] = "application/json",
96 _a["User-Agent"] = "Postmark.JS - ".concat(this.clientVersion),
97 _a;
98 };
99 /**
100 * Token can't be empty.
101 *
102 * @param {string} token - HTTP request token
103 */
104 BaseClient.prototype.verifyToken = function (token) {
105 if (!token || token.trim() === "") {
106 throw this.errorHandler.buildError("A valid API token must be provided.");
107 }
108 };
109 /**
110 * Set default values for count and offset when doing filtering with API requests if they are not specified by filter.
111 */
112 BaseClient.prototype.setDefaultPaginationValues = function (filter) {
113 filter.count = filter.count || 100;
114 filter.offset = filter.offset || 0;
115 };
116 return BaseClient;
117}());
118exports.default = BaseClient;
119//# sourceMappingURL=BaseClient.js.map
\No newline at end of file