UNPKG

@hellotext/hellotext

Version:
230 lines (221 loc) 9.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _core = require("./core"); var _api = _interopRequireWildcard(require("./api")); var _models = require("./models"); var _errors = require("./errors"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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); } let Hellotext = /*#__PURE__*/function () { function Hellotext() { _classCallCheck(this, Hellotext); } _createClass(Hellotext, null, [{ key: "initialize", value: /** * initialize the module. * @param business public business id * @param { Configuration } config */ async function initialize(business, config = {}) { this.business = new _models.Business(business); this.page = new _models.Page(); _core.Configuration.assign(config); _models.Session.initialize(this.page); this.forms = new _models.FormCollection(); this.query = new _models.Query(); const businessData = await this.business.hydrate(); const webchatConfig = config.webchat === false ? false : this.mergeWebchatConfig(businessData && businessData.webchat || {}, config.webchat || {}); const hasExplicitBehaviourOverride = config.webchat && config.webchat !== false && Object.prototype.hasOwnProperty.call(config.webchat, 'behaviour'); _core.Configuration.webchat.behaviourOverride = hasExplicitBehaviourOverride; if (webchatConfig && webchatConfig.id) { _core.Configuration.webchat.assign(webchatConfig); this.webchat = await _models.Webchat.load(webchatConfig.id); } if (typeof MutationObserver !== 'undefined') { this.forms.collectExistingFormsOnPage(); } } }, { key: "mergeWebchatConfig", value: function mergeWebchatConfig(dashboardConfig, localConfig) { return this.deepMergePlainObjects(dashboardConfig, localConfig); } }, { key: "deepMergePlainObjects", value: function deepMergePlainObjects(base, override) { const result = { ...base }; Object.entries(override).forEach(([key, value]) => { if (this.isPlainObject(value) && this.isPlainObject(result[key])) { result[key] = this.deepMergePlainObjects(result[key], value); } else { result[key] = value; } }); return result; } }, { key: "isPlainObject", value: function isPlainObject(value) { return typeof value === 'object' && value !== null && !Array.isArray(value); } /** * Tracks an action that has happened on the page * * @param { String } action a valid action name * @param { Object } params * @returns {Promise<Response>} */ }, { key: "track", value: async function track(action, params = {}) { if (this.notInitialized) { throw new _errors.NotInitializedError(); } const headers = { ...(params && params.headers || {}), ...this.headers }; const user_parameters = { ..._models.User.identificationData, ...(params.user_parameters || {}) }; const pageInstance = params && params.url ? new _models.Page(params.url) : this.page; const body = { session: this.session, user_parameters, action, ...params, ...pageInstance.trackingData }; delete body.headers; return await _api.default.events.create({ headers, body, // Track is the SDK's unload-sensitive analytics path. Keepalive belongs // here rather than on identify/forms/webchat calls because event tracking // is allowed to be fire-and-navigate, while those other calls have // stronger request/response or interaction contracts. keepalive: (0, _api.keepaliveFor)(body) }); } /** * @typedef { Object } IdentificationOptions * @property { String } [email] - the email of the user * @property { String } [phone] - the phone number of the user * @property { String } [name] - the name of the user * @property { String } [source] - the platform specific identifier where this pixel is running on. * * Identifies a user and attaches the hello_session to the user ID. * Repeated calls are skipped only when the last successful identify payload * for the current session remains unchanged. * @param { String } externalId - the user ID * @param { IdentificationOptions } options - the options for the identification * @returns {Promise<Response>} */ }, { key: "identify", value: async function identify(externalId, options = {}) { const fingerprint = await _models.Fingerprint.generate(this.session, externalId, options); if (_models.Fingerprint.matches(_models.User.fingerprint, fingerprint)) { return new _api.Response(true, { json: async () => ({ already_identified: true }) }); } const response = await _api.default.identifications.create({ user_id: externalId, ...options }); if (response.succeeded) { _models.User.remember(externalId, options.source, fingerprint); } return response; } /** * Clears the user session, use when the user logs out to clear the hello cookies * * @returns {void} */ }, { key: "forget", value: function forget() { _models.User.forget(); } /** * Registers an event listener * @param event the name of the event to listen to * @param callback the callback. This method will be called with the payload */ }, { key: "on", value: function on(event, callback) { this.eventEmitter.addSubscriber(event, callback); } /** * Removes an event listener * @param event the name of the event to remove * @param callback the callback to remove */ }, { key: "removeEventListener", value: function removeEventListener(event, callback) { this.eventEmitter.removeSubscriber(event, callback); } /** * * @returns {String} */ }, { key: "session", get: function () { return _models.Session.session; } /** * Determines if the session is set or not * @returns {boolean} */ }, { key: "isInitialized", get: function () { return _models.Session.session !== undefined; } // private }, { key: "notInitialized", get: function () { return !this.business || this.business.id === undefined; } }, { key: "headers", get: function () { if (this.notInitialized) { throw new _errors.NotInitializedError(); } return { Authorization: `Bearer ${this.business.id}`, Accept: 'application/json', 'Content-Type': 'application/json' }; } }]); return Hellotext; }(); Hellotext.eventEmitter = new _core.Event(); Hellotext.forms = void 0; Hellotext.business = void 0; Hellotext.webchat = void 0; var _default = Hellotext; exports.default = _default;