UNPKG

5.35 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import { join } from "@hpcc-js/util";
3import { createConnection } from "./connection";
4export function isArray(arg) {
5 return Object.prototype.toString.call(arg) === "[object Array]";
6}
7var ESPExceptions = /** @class */ (function (_super) {
8 __extends(ESPExceptions, _super);
9 function ESPExceptions(action, request, exceptions) {
10 var _this = _super.call(this, "ESPException: " + exceptions.Source) || this;
11 _this.isESPExceptions = true;
12 _this.action = action;
13 _this.request = request;
14 _this.Source = exceptions.Source;
15 _this.Exception = exceptions.Exception;
16 if (exceptions.Exception.length) {
17 _this.message = exceptions.Exception[0].Code + ": " + exceptions.Exception[0].Message;
18 }
19 return _this;
20 }
21 return ESPExceptions;
22}(Error));
23export { ESPExceptions };
24function isConnection(optsConnection) {
25 return optsConnection.send !== undefined;
26}
27var ESPConnection = /** @class */ (function () {
28 function ESPConnection(optsConnection, service, version) {
29 this._connection = isConnection(optsConnection) ? optsConnection : createConnection(optsConnection);
30 this._service = service;
31 this._version = version;
32 }
33 Object.defineProperty(ESPConnection.prototype, "baseUrl", {
34 get: function () { return this._connection.opts().baseUrl; },
35 enumerable: false,
36 configurable: true
37 });
38 ESPConnection.prototype.service = function (_) {
39 if (_ === void 0)
40 return this._service;
41 this._service = _;
42 return this;
43 };
44 ESPConnection.prototype.version = function (_) {
45 if (_ === void 0)
46 return this._version;
47 this._version = _;
48 return this;
49 };
50 ESPConnection.prototype.toESPStringArray = function (target, arrayName) {
51 if (isArray(target[arrayName])) {
52 for (var i = 0; i < target[arrayName].length; ++i) {
53 target[arrayName + "_i" + i] = target[arrayName][i];
54 }
55 delete target[arrayName];
56 }
57 return target;
58 };
59 ESPConnection.prototype.opts = function (_) {
60 if (_ === void 0)
61 return this._connection.opts();
62 this._connection.opts(_);
63 return this;
64 };
65 ESPConnection.prototype.send = function (action, _request, espResponseType, largeUpload) {
66 if (_request === void 0) { _request = {}; }
67 if (espResponseType === void 0) { espResponseType = "json"; }
68 if (largeUpload === void 0) { largeUpload = false; }
69 var request = __assign(__assign({}, _request), { ver_: this._version });
70 if (largeUpload) {
71 request["upload_"] = true;
72 }
73 var serviceAction;
74 var responseType = "json";
75 switch (espResponseType) {
76 case "text":
77 serviceAction = join(this._service, action);
78 responseType = "text";
79 break;
80 case "xsd":
81 serviceAction = join(this._service, action + ".xsd");
82 responseType = "text";
83 break;
84 case "json2":
85 serviceAction = join(this._service, action + "/json");
86 espResponseType = "json";
87 var actionParts = action.split("/");
88 action = actionParts.pop();
89 break;
90 default:
91 serviceAction = join(this._service, action + ".json");
92 }
93 return this._connection.send(serviceAction, request, responseType).then(function (response) {
94 if (espResponseType === "json") {
95 var retVal = void 0;
96 if (response && response.Exceptions) {
97 throw new ESPExceptions(action, request, response.Exceptions);
98 }
99 else if (response) {
100 retVal = response[(action === "WUCDebug" ? "WUDebug" : action) + "Response"];
101 }
102 if (!retVal) {
103 throw new ESPExceptions(action, request, {
104 Source: "ESPConnection.send",
105 Exception: [{ Code: 0, Message: "Missing Response" }]
106 });
107 }
108 return retVal;
109 }
110 return response;
111 }).catch(function (e) {
112 if (e.isESPExceptions) {
113 throw e;
114 }
115 throw new ESPExceptions(action, request, {
116 Source: "ESPConnection.send",
117 Exception: [{ Code: 0, Message: e.message }]
118 });
119 });
120 };
121 ESPConnection.prototype.clone = function () {
122 return new ESPConnection(this._connection.clone(), this._service, this._version);
123 };
124 return ESPConnection;
125}());
126export { ESPConnection };
127var Service = /** @class */ (function () {
128 function Service(optsConnection, service, version) {
129 this._connection = new ESPConnection(optsConnection, service, version);
130 }
131 Object.defineProperty(Service.prototype, "baseUrl", {
132 get: function () { return this._connection.opts().baseUrl; },
133 enumerable: false,
134 configurable: true
135 });
136 return Service;
137}());
138export { Service };
139//# sourceMappingURL=espConnection.js.map
\No newline at end of file