@hellotext/hellotext
Version:
Hellotext JavaScript Client
91 lines (90 loc) • 4.01 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { Forms } from './configuration/forms';
import { Locale } from './configuration/locale';
import { Webchat } from './configuration/webchat';
/**
* @class Configuration
* @classdesc
* Configuration for Hellotext
* @property {Boolean} [autoGenerateSession=true] - whether to auto generate session or not
* @property {String} [session] - session id
* @property {Forms} [forms] - form configuration
* @property {Webchat} [webchat] - webchat configuration
* @property {Locale} [locale] - locale configuration
*/
var Configuration = /*#__PURE__*/function () {
function Configuration() {
_classCallCheck(this, Configuration);
}
_createClass(Configuration, null, [{
key: "assign",
value:
/**
*
* @param props
* @param {Boolean} [props.autoGenerateSession=true] - whether to auto generate session or not
* @param {String} [props.session] - session id
* @param {Object} [props.forms] - form configuration
* @returns {Configuration}
*/
function assign(props) {
if (props) {
var shouldInferActionCableUrl = Object.prototype.hasOwnProperty.call(props, 'apiRoot') && !Object.prototype.hasOwnProperty.call(props, 'actionCableUrl');
Object.entries(props).forEach(_ref => {
var [key, value] = _ref;
if (key === 'forms') {
this.forms = Forms.assign(value);
} else if (key === 'webchat') {
this.webchat = Webchat.assign(value);
} else {
this[key] = value;
}
});
if (shouldInferActionCableUrl) {
this.actionCableUrl = this.actionCableUrlForApiRoot(this.apiRoot);
}
}
return this;
}
}, {
key: "locale",
get: function get() {
return Locale.toString();
},
set: function set(locale) {
Locale.identifier = locale;
}
}, {
key: "endpoint",
value: function endpoint(path) {
return "".concat(this.apiRoot, "/").concat(path);
}
}, {
key: "actionCableUrlForApiRoot",
value: function actionCableUrlForApiRoot(apiRoot) {
try {
var url = new URL(apiRoot);
var protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
url.protocol = protocol;
url.pathname = '/cable';
url.search = '';
url.hash = '';
return url.toString();
} catch (_) {
return this.actionCableUrl;
}
}
}]);
return Configuration;
}();
Configuration.apiRoot = 'https://api.hellotext.com/v1';
Configuration.actionCableUrl = 'wss://www.hellotext.com/cable';
Configuration.autoGenerateSession = true;
Configuration.session = null;
Configuration.forms = Forms;
Configuration.webchat = Webchat;
export { Configuration };