UNPKG

7.26 kBJavaScriptView Raw
1"use strict";
2var __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};
10Object.defineProperty(exports, "__esModule", { value: true });
11var axios_1 = require("axios");
12var ErrorHandler_1 = require("./ErrorHandler");
13var packageJson = require("../../package.json");
14var CLIENT_VERSION = packageJson.version;
15/**
16 * Base client class from which client classes can be implemented, in our case, AccountClient and ServerClient classes.
17 * This class is NOT intended to be instantiated directly.
18 */
19var BaseClient = /** @class */ (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 * JSON object with default headers sent by HTTP request.
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 * Process http request with sending body - data.
49 *
50 * @see processRequest for more details.
51 */
52 BaseClient.prototype.processRequestWithBody = function (method, path, body, callback) {
53 return this.processRequest(method, path, {}, body, callback);
54 };
55 /**
56 * Process http request without sending body - data.
57 *
58 * @see processRequest for more details.
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 * Set default values for count and offset when doing filtering with API requests if they are not specified by filter.
66 */
67 BaseClient.prototype.setDefaultPaginationValues = function (filter) {
68 filter.count = filter.count || 100;
69 filter.offset = filter.offset || 0;
70 };
71 /**
72 * Process request for Postmark ClientOptions.
73 *
74 * @param method - see processHttpRequest for details
75 * @param path - see processHttpRequest for details
76 * @param queryParameters - see processHttpRequest for details
77 * @param body - see processHttpRequest for details
78 * @param callback - callback function to be executed.
79 *
80 * @returns A promise that will complete when the API responds (or an error occurs).
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 * Process HTTP request.
89 *
90 * @param method - Which type of http request will be executed.
91 * @param path - API URL endpoint.
92 * @param queryParameters - Querystring parameters used for http request.
93 * @param body - Data sent with http request.
94 *
95 * @returns A promise that will complete when the API responds (or an error occurs).
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 * Process callback function for HTTP request.
107 *
108 * @param httpRequest - HTTP request for which callback will be executed
109 * @param callback - callback function to be executed.
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 * Process http request.
120 *
121 * @param method - Which type of http request will be executed.
122 * @param path - API URL endpoint.
123 * @param queryParameters - Querystring parameters used for http request.
124 * @param body - Data sent with http request.
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 * Create http client instance with default settings.
137 *
138 * @return {AxiosInstance}
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 * Token can't be empty.
163 *
164 * @param {string} token - HTTP request token
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 * Client connection configuration options.
173 * You may modify these values and new clients will use them.
174 * Any values provided to a Client constructor will override default options.
175 */
176 BaseClient.DefaultOptions = {
177 useHttps: true,
178 requestHost: "api.postmarkapp.com",
179 timeout: 180,
180 };
181 return BaseClient;
182}());
183exports.default = BaseClient;
184//# sourceMappingURL=BaseClient.js.map
\No newline at end of file