1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | var axios_1 = require("axios");
|
12 | var ErrorHandler_1 = require("./ErrorHandler");
|
13 | var packageJson = require("../../package.json");
|
14 | var CLIENT_VERSION = packageJson.version;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var BaseClient = (function () {
|
20 | function BaseClient(token, authHeader, configOptions) {
|
21 | this.clientVersion = CLIENT_VERSION;
|
22 | this.token = token.trim();
|
23 | this.authHeader = authHeader;
|
24 | this.clientOptions = __assign({}, BaseClient.DefaultOptions, configOptions);
|
25 | this.httpClient = this.buildDefaultHttpClient();
|
26 | this.errorHandler = new ErrorHandler_1.ErrorHandler();
|
27 | this.verifyToken(token);
|
28 | }
|
29 | BaseClient.prototype.setClientOptions = function (configOptions) {
|
30 | this.clientOptions = __assign({}, BaseClient.DefaultOptions, configOptions);
|
31 | this.buildDefaultHttpClient();
|
32 | };
|
33 | BaseClient.prototype.getClientOptions = function () {
|
34 | return this.clientOptions;
|
35 | };
|
36 | |
37 |
|
38 |
|
39 | BaseClient.prototype.getComposedHttpRequestHeaders = function () {
|
40 | var _a;
|
41 | return _a = {},
|
42 | _a[this.authHeader] = this.token,
|
43 | _a["Accept"] = "application/json",
|
44 | _a["User-Agent"] = "Postmark.JS - " + this.clientVersion,
|
45 | _a;
|
46 | };
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 | BaseClient.prototype.processRequestWithBody = function (method, path, body, callback) {
|
53 | return this.processRequest(method, path, {}, body, callback);
|
54 | };
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | BaseClient.prototype.processRequestWithoutBody = function (method, path, queryParameters, callback) {
|
61 | if (queryParameters === void 0) { queryParameters = {}; }
|
62 | return this.processRequest(method, path, queryParameters, null, callback);
|
63 | };
|
64 | |
65 |
|
66 |
|
67 | BaseClient.prototype.setDefaultPaginationValues = function (filter) {
|
68 | filter.count = filter.count || 100;
|
69 | filter.offset = filter.offset || 0;
|
70 | };
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | BaseClient.prototype.processRequest = function (method, path, queryParameters, body, callback) {
|
83 | var httpRequest = this.processHttpRequest(method, path, queryParameters, body);
|
84 | this.processCallbackRequest(httpRequest, callback);
|
85 | return httpRequest;
|
86 | };
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | BaseClient.prototype.processHttpRequest = function (method, path, queryParameters, body) {
|
98 | var _this = this;
|
99 | return this.httpRequest(method, path, queryParameters, body)
|
100 | .then(function (response) { return response; })
|
101 | .catch(function (error) {
|
102 | throw _this.errorHandler.buildRequestError(error);
|
103 | });
|
104 | };
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | BaseClient.prototype.processCallbackRequest = function (httpRequest, callback) {
|
112 | if (callback) {
|
113 | httpRequest
|
114 | .then(function (response) { return callback(null, response); })
|
115 | .catch(function (error) { return callback(error, null); });
|
116 | }
|
117 | };
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | BaseClient.prototype.httpRequest = function (method, path, queryParameters, body) {
|
127 | return this.httpClient.request({
|
128 | method: method,
|
129 | url: path,
|
130 | data: body,
|
131 | headers: this.getComposedHttpRequestHeaders(),
|
132 | params: queryParameters,
|
133 | });
|
134 | };
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 | BaseClient.prototype.buildDefaultHttpClient = function () {
|
141 | var httpClient = axios_1.default.create({
|
142 | baseURL: this.getBaseHttpRequestURL(),
|
143 | timeout: this.getRequestTimeoutInSeconds(),
|
144 | responseType: "json",
|
145 | maxContentLength: Infinity,
|
146 | maxBodyLength: Infinity,
|
147 | validateStatus: function (status) {
|
148 | return status >= 200 && status < 300;
|
149 | },
|
150 | });
|
151 | httpClient.interceptors.response.use(function (response) { return (response.data); });
|
152 | return httpClient;
|
153 | };
|
154 | BaseClient.prototype.getRequestTimeoutInSeconds = function () {
|
155 | return (this.clientOptions.timeout || 60) * 1000;
|
156 | };
|
157 | BaseClient.prototype.getBaseHttpRequestURL = function () {
|
158 | var scheme = this.clientOptions.useHttps ? "https" : "http";
|
159 | return scheme + "://" + this.clientOptions.requestHost;
|
160 | };
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 | BaseClient.prototype.verifyToken = function (token) {
|
167 | if (!token || token.trim() === "") {
|
168 | throw this.errorHandler.buildGeneralError("A valid API token must be provided.");
|
169 | }
|
170 | };
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 | BaseClient.DefaultOptions = {
|
177 | useHttps: true,
|
178 | requestHost: "api.postmarkapp.com",
|
179 | timeout: 180,
|
180 | };
|
181 | return BaseClient;
|
182 | }());
|
183 | exports.default = BaseClient;
|
184 |
|
\ | No newline at end of file |