backendless-rt-client
Version:
Backendless RT Client for connect to Backendless RT Server
172 lines (171 loc) • 5.89 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var isUndefined = function isUndefined(value) {
return typeof value === 'undefined';
};
var isString = function isString(value) {
return typeof value === 'string';
};
var isFunction = function isFunction(value) {
return typeof value === 'function';
};
var isObject = function isObject(value) {
return (0, _typeof2["default"])(value) === 'object' && value !== null;
};
var RTConfig = exports["default"] = /*#__PURE__*/function () {
function RTConfig(config) {
(0, _classCallCheck2["default"])(this, RTConfig);
this.appId = null;
this.debugMode = false;
this.connectQuery = {};
this.socketConfigTransform = null;
this.hostResolver = null;
this.socketConfig = null;
this.set(config);
}
return (0, _createClass2["default"])(RTConfig, [{
key: "set",
value: function set(config) {
if (!config) {
return;
}
if (!isUndefined(config.hostResolver)) {
if (isFunction(config.hostResolver)) {
this.hostResolver = config.hostResolver;
}
}
if (!isUndefined(config.appId)) {
if (!isString(config.appId)) {
throw new Error('"appId" must be String.');
}
this.appId = config.appId;
}
if (!isUndefined(config.debugMode)) {
this.debugMode = !!config.debugMode;
}
if (!isUndefined(config.connectQuery)) {
if (isFunction(config.connectQuery)) {
this.getConnectQuery = config.connectQuery;
} else if (isObject(config.connectQuery)) {
this.connectQuery = config.connectQuery;
} else {
throw new Error('"connectQuery" must be Function or Object.');
}
}
if (!isUndefined(config.socketConfigTransform)) {
if (isFunction(config.socketConfigTransform)) {
this.socketConfigTransform = config.socketConfigTransform;
} else {
throw new Error('"socketConfigTransform" must be Function.');
}
}
}
}, {
key: "getConnectQuery",
value: function getConnectQuery() {
return this.connectQuery;
}
}, {
key: "getSocketConfig",
value: function getSocketConfig() {
return this.socketConfig;
}
}, {
key: "getHost",
value: function () {
var _getHost = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
var host;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
if (!(isUndefined(this.hostResolver) || !isFunction(this.hostResolver))) {
_context.next = 2;
break;
}
throw new Error('"hostResolver" must be a function');
case 2:
_context.next = 4;
return this.hostResolver();
case 4:
host = _context.sent;
if (!(!host || typeof host !== 'string')) {
_context.next = 7;
break;
}
throw new Error('"hostResolver" must return a valid string');
case 7:
return _context.abrupt("return", host);
case 8:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function getHost() {
return _getHost.apply(this, arguments);
}
return getHost;
}()
}, {
key: "prepare",
value: function () {
var _prepare = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
var query, host, url, path;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
query = this.getConnectQuery();
_context2.next = 3;
return this.getHost();
case 3:
host = _context2.sent;
url = this.appId ? "".concat(host, "/").concat(this.appId) : host;
path = this.appId ? "/".concat(this.appId) : undefined;
this.socketConfig = {
host: host,
url: url,
options: {
path: path,
query: query,
forceNew: true,
autoConnect: false,
reconnection: false
}
};
if (!this.socketConfigTransform) {
_context2.next = 14;
break;
}
_context2.next = 10;
return this.socketConfigTransform(this.socketConfig);
case 10:
_context2.t0 = _context2.sent;
if (_context2.t0) {
_context2.next = 13;
break;
}
_context2.t0 = this.socketConfig;
case 13:
this.socketConfig = _context2.t0;
case 14:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function prepare() {
return _prepare.apply(this, arguments);
}
return prepare;
}()
}]);
}();