UNPKG

4.72 kBJavaScriptView Raw
1"use strict";
2var __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})();
17var __assign = (this && this.__assign) || function () {
18 __assign = Object.assign || function(t) {
19 for (var s, i = 1, n = arguments.length; i < n; i++) {
20 s = arguments[i];
21 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22 t[p] = s[p];
23 }
24 return t;
25 };
26 return __assign.apply(this, arguments);
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.AxiosHttpClient = void 0;
30var axios_1 = require("axios");
31var models_1 = require("./models");
32var index_1 = require("./errors/index");
33var AxiosHttpClient = /** @class */ (function (_super) {
34 __extends(AxiosHttpClient, _super);
35 function AxiosHttpClient(configOptions) {
36 var _this = _super.call(this, configOptions) || this;
37 _this.errorHandler = new index_1.ErrorHandler();
38 return _this;
39 }
40 /**
41 * Create http client instance with default settings.
42 *
43 * @return {AxiosInstance}
44 */
45 AxiosHttpClient.prototype.initHttpClient = function (configOptions) {
46 this.clientOptions = __assign(__assign({}, models_1.HttpClient.DefaultOptions), configOptions);
47 var httpClient = axios_1.default.create({
48 baseURL: this.getBaseHttpRequestURL(),
49 timeout: this.getRequestTimeoutInMilliseconds(),
50 responseType: "json",
51 maxContentLength: Infinity,
52 maxBodyLength: Infinity,
53 validateStatus: function (status) {
54 return status >= 200 && status < 300;
55 },
56 });
57 httpClient.interceptors.response.use(function (response) { return (response.data); });
58 this.client = httpClient;
59 };
60 /**
61 * Process http request.
62 *
63 * @param method - Which type of http request will be executed.
64 * @param path - API URL endpoint.
65 * @param queryParameters - Querystring parameters used for http request.
66 * @param body - Data sent with http request.
67 */
68 AxiosHttpClient.prototype.httpRequest = function (method, path, queryParameters, body, requestHeaders) {
69 var _this = this;
70 return this.client.request({
71 method: method,
72 url: path,
73 data: body,
74 headers: requestHeaders,
75 params: queryParameters,
76 }).catch(function (errorThrown) {
77 return Promise.reject(_this.transformError(errorThrown));
78 });
79 };
80 /**
81 * Process callback function for HTTP request.
82 *
83 * @param error - request error that needs to be transformed to proper Postmark error.
84 *
85 * @return {PostmarkError} - formatted Postmark error
86 */
87 AxiosHttpClient.prototype.transformError = function (errorThrown) {
88 var response = errorThrown.response;
89 if (response !== undefined) {
90 var status_1 = this.adjustValue(0, response.status);
91 var errorCode = this.adjustValue(0, response.data.ErrorCode);
92 var message = this.adjustValue(errorThrown.message, response.data.Message);
93 return this.errorHandler.buildError(message, errorCode, status_1);
94 }
95 else if (errorThrown.message !== undefined) {
96 return this.errorHandler.buildError(errorThrown.message);
97 }
98 else {
99 return this.errorHandler.buildError(JSON.stringify(errorThrown, Object.getOwnPropertyNames(errorThrown)));
100 }
101 };
102 /**
103 * Timeout in seconds is adjusted to Axios format.
104 *
105 * @private
106 */
107 AxiosHttpClient.prototype.getRequestTimeoutInMilliseconds = function () {
108 return (this.clientOptions.timeout || 60) * 1000;
109 };
110 AxiosHttpClient.prototype.adjustValue = function (defaultValue, data) {
111 return (data === undefined) ? defaultValue : data;
112 };
113 return AxiosHttpClient;
114}(models_1.HttpClient));
115exports.AxiosHttpClient = AxiosHttpClient;
116//# sourceMappingURL=HttpClient.js.map
\No newline at end of file