@hpcc-js/comms
Version:
hpcc-js - Communications
154 lines • 5.85 kB
JavaScript
import { __assign, __extends } from "tslib";
import { join } from "@hpcc-js/util";
import { createConnection } from "./connection";
export function isArray(arg) {
return Object.prototype.toString.call(arg) === "[object Array]";
}
var ESPExceptions = /** @class */ (function (_super) {
__extends(ESPExceptions, _super);
function ESPExceptions(action, request, exceptions) {
var _this = _super.call(this, "ESPException: " + exceptions.Source) || this;
_this.isESPExceptions = true;
_this.action = action;
_this.request = request;
_this.Source = exceptions.Source;
_this.Exception = exceptions.Exception;
if (exceptions.Exception.length) {
_this.message = "".concat(exceptions.Exception[0].Code, ": ").concat(exceptions.Exception[0].Message);
}
else {
_this.message = "";
}
return _this;
}
return ESPExceptions;
}(Error));
export { ESPExceptions };
export function isExceptions(err) {
return err instanceof ESPExceptions || (err.isESPExceptions && Array.isArray(err.Exception));
}
function isConnection(optsConnection) {
return optsConnection.send !== undefined;
}
var ESPConnection = /** @class */ (function () {
function ESPConnection(optsConnection, service, version) {
this._connection = isConnection(optsConnection) ? optsConnection : createConnection(optsConnection);
this._service = service;
this._version = version;
}
Object.defineProperty(ESPConnection.prototype, "baseUrl", {
get: function () { return this._connection.opts().baseUrl; },
enumerable: false,
configurable: true
});
ESPConnection.prototype.service = function (_) {
if (_ === void 0)
return this._service;
this._service = _;
return this;
};
ESPConnection.prototype.version = function (_) {
if (_ === void 0)
return this._version;
this._version = _;
return this;
};
ESPConnection.prototype.toESPStringArray = function (target, arrayName) {
if (isArray(target[arrayName])) {
for (var i = 0; i < target[arrayName].length; ++i) {
target[arrayName + "_i" + i] = target[arrayName][i];
}
delete target[arrayName];
}
return target;
};
ESPConnection.prototype.opts = function (_) {
if (_ === void 0)
return this._connection.opts();
this._connection.opts(_);
return this;
};
ESPConnection.prototype.send = function (action, _request, espResponseType, largeUpload, abortSignal, espResponseField) {
if (_request === void 0) { _request = {}; }
if (espResponseType === void 0) { espResponseType = "json"; }
if (largeUpload === void 0) { largeUpload = false; }
var request = __assign(__assign({}, _request), { ver_: this._version });
if (largeUpload) {
request["upload_"] = true;
}
if (abortSignal) {
request["abortSignal_"] = abortSignal;
}
var serviceAction;
var responseType = "json";
switch (espResponseType) {
case "text":
serviceAction = join(this._service, action);
responseType = "text";
break;
case "xsd":
serviceAction = join(this._service, action + ".xsd");
responseType = "text";
break;
case "json2":
serviceAction = join(this._service, action + "/json");
espResponseType = "json";
var actionParts = action.split("/");
action = actionParts.pop();
break;
default:
serviceAction = join(this._service, action + ".json");
}
return this._connection.send(serviceAction, request, responseType).then(function (response) {
if (espResponseType === "json") {
var retVal = void 0;
if (response && response.Exceptions) {
throw new ESPExceptions(action, request, response.Exceptions);
}
else if (response) {
retVal = response[espResponseField || (action + "Response")];
}
if (!retVal) {
throw new ESPExceptions(action, request, {
Source: "ESPConnection.send",
Exception: [{ Code: 0, Message: "Missing Response" }]
});
}
return retVal;
}
return response;
}).catch(function (e) {
if (e.isESPExceptions) {
throw e;
}
throw new ESPExceptions(action, request, {
Source: "ESPConnection.send",
Exception: [{ Code: 0, Message: e.message }]
});
});
};
ESPConnection.prototype.clone = function () {
return new ESPConnection(this._connection.clone(), this._service, this._version);
};
return ESPConnection;
}());
export { ESPConnection };
var Service = /** @class */ (function () {
function Service(optsConnection, service, version) {
this._connection = new ESPConnection(optsConnection, service, version);
}
Object.defineProperty(Service.prototype, "baseUrl", {
get: function () { return this._connection.opts().baseUrl; },
enumerable: false,
configurable: true
});
Service.prototype.opts = function () {
return this._connection.opts();
};
Service.prototype.connection = function () {
return this._connection.clone();
};
return Service;
}());
export { Service };
//# sourceMappingURL=espConnection.js.map