1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var ErrorHandler_1 = require("./errors/ErrorHandler");
|
4 | var HttpClient_1 = require("./HttpClient");
|
5 | var packageJson = require("../../package.json");
|
6 | var CLIENT_VERSION = packageJson.version;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var BaseClient = (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 |
|
28 |
|
29 |
|
30 |
|
31 | BaseClient.prototype.processRequestWithBody = function (method, path, body, callback) {
|
32 | return this.processRequest(method, path, {}, body, callback);
|
33 | };
|
34 | |
35 |
|
36 |
|
37 |
|
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 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
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 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
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 |
|
76 |
|
77 |
|
78 |
|
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 |
|
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["User-Agent"] = "Postmark.JS - ".concat(this.clientVersion),
|
96 | _a;
|
97 | };
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 | BaseClient.prototype.verifyToken = function (token) {
|
104 | if (!token || token.trim() === "") {
|
105 | throw this.errorHandler.buildError("A valid API token must be provided.");
|
106 | }
|
107 | };
|
108 | |
109 |
|
110 |
|
111 | BaseClient.prototype.setDefaultPaginationValues = function (filter) {
|
112 | filter.count = filter.count || 100;
|
113 | filter.offset = filter.offset || 0;
|
114 | };
|
115 | return BaseClient;
|
116 | }());
|
117 | exports.default = BaseClient;
|
118 |
|
\ | No newline at end of file |