UNPKG

@hpcc-js/comms

Version:
136 lines (135 loc) 5.33 kB
import * as tslib_1 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) { tslib_1.__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 = exceptions.Exception[0].Code + ": " + exceptions.Exception[0].Message; } return _this; } return ESPExceptions; }(Error)); export { ESPExceptions }; 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; } 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) { if (_request === void 0) { _request = {}; } if (espResponseType === void 0) { espResponseType = "json"; } if (largeUpload === void 0) { largeUpload = false; } var request = tslib_1.__assign({}, _request, { ver_: this._version }); if (largeUpload) { request["upload_"] = true; } 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[(action === "WUCDebug" ? "WUDebug" : 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 }; /* export class WsECLConnection extends ESPConnection { send(action: string, _request: any = {}, responseType: ResponseType = "json"): Promise<any> { if (responseType === "json") { serviceAction = join(this._service, action + "/json"); responseType = "json"; const actionParts = action.split("/"); action = actionParts.pop()!; } return super.send(action, _request, responseType); } } */ //# sourceMappingURL=espConnection.js.map