@hellotext/hellotext 2.4.52 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,8 +27,15 @@ Object.defineProperty(exports, "Webchat", {
27
27
  return _webchat.Webchat;
28
28
  }
29
29
  });
30
+ Object.defineProperty(exports, "WhatsApp", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _whatsapp.WhatsApp;
34
+ }
35
+ });
30
36
  var _configuration = require("./configuration");
31
37
  var _locale = require("./configuration/locale");
32
38
  var _webchat = require("./configuration/webchat");
39
+ var _whatsapp = require("./configuration/whatsapp");
33
40
  var _event = _interopRequireDefault(require("./event"));
34
41
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
package/lib/core/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { Configuration } from './configuration';
2
2
  export { Locale } from './configuration/locale';
3
3
  export { Webchat } from './configuration/webchat';
4
+ export { WhatsApp } from './configuration/whatsapp';
4
5
  export { default as Event } from './event';
package/lib/hellotext.cjs CHANGED
@@ -29,19 +29,24 @@ let Hellotext = /*#__PURE__*/function () {
29
29
  */
30
30
  async function initialize(business, config = {}) {
31
31
  this.business = new _models.Business(business);
32
- _core.Configuration.assign(config);
33
- _models.Session.initialize();
34
32
  this.page = new _models.Page();
33
+ _core.Configuration.assign(config);
34
+ _models.Session.initialize(this.page);
35
35
  this.forms = new _models.FormCollection();
36
36
  this.query = new _models.Query();
37
37
  const businessData = await this.business.hydrate();
38
38
  const webchatConfig = config.webchat === false ? false : this.mergeWebchatConfig(businessData && businessData.webchat || {}, config.webchat || {});
39
+ const whatsappConfig = config.whatsappWidget === false ? false : this.mergeWhatsAppConfig(businessData && businessData.whatsapp || {}, config.whatsappWidget || {});
39
40
  const hasExplicitBehaviourOverride = config.webchat && config.webchat !== false && Object.prototype.hasOwnProperty.call(config.webchat, 'behaviour');
40
41
  _core.Configuration.webchat.behaviourOverride = hasExplicitBehaviourOverride;
41
42
  if (webchatConfig && webchatConfig.id) {
42
43
  _core.Configuration.webchat.assign(webchatConfig);
43
44
  this.webchat = await _models.Webchat.load(webchatConfig.id);
44
45
  }
46
+ if (whatsappConfig && whatsappConfig.id) {
47
+ _core.Configuration.whatsapp.assign(whatsappConfig);
48
+ this.whatsapp = await _models.WhatsAppWidget.load(whatsappConfig.id);
49
+ }
45
50
  if (typeof MutationObserver !== 'undefined') {
46
51
  this.forms.collectExistingFormsOnPage();
47
52
  }
@@ -51,6 +56,11 @@ let Hellotext = /*#__PURE__*/function () {
51
56
  value: function mergeWebchatConfig(dashboardConfig, localConfig) {
52
57
  return this.deepMergePlainObjects(dashboardConfig, localConfig);
53
58
  }
59
+ }, {
60
+ key: "mergeWhatsAppConfig",
61
+ value: function mergeWhatsAppConfig(dashboardConfig, localConfig) {
62
+ return this.deepMergePlainObjects(dashboardConfig, localConfig);
63
+ }
54
64
  }, {
55
65
  key: "deepMergePlainObjects",
56
66
  value: function deepMergePlainObjects(base, override) {
@@ -226,5 +236,6 @@ Hellotext.eventEmitter = new _core.Event();
226
236
  Hellotext.forms = void 0;
227
237
  Hellotext.business = void 0;
228
238
  Hellotext.webchat = void 0;
239
+ Hellotext.whatsapp = void 0;
229
240
  var _default = Hellotext;
230
241
  exports.default = _default;
package/lib/hellotext.js CHANGED
@@ -10,7 +10,7 @@ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typ
10
10
  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); }
11
11
  import { Configuration, Event } from './core';
12
12
  import API, { Response, keepaliveFor } from './api';
13
- import { Business, Fingerprint, FormCollection, Page, Query, Session, User, Webchat } from './models';
13
+ import { Business, Fingerprint, FormCollection, Page, Query, Session, User, Webchat, WhatsAppWidget } from './models';
14
14
  import { NotInitializedError } from './errors';
15
15
  var Hellotext = /*#__PURE__*/function () {
16
16
  function Hellotext() {
@@ -28,19 +28,24 @@ var Hellotext = /*#__PURE__*/function () {
28
28
  var _initialize = _asyncToGenerator(function* (business) {
29
29
  var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
30
30
  this.business = new Business(business);
31
- Configuration.assign(config);
32
- Session.initialize();
33
31
  this.page = new Page();
32
+ Configuration.assign(config);
33
+ Session.initialize(this.page);
34
34
  this.forms = new FormCollection();
35
35
  this.query = new Query();
36
36
  var businessData = yield this.business.hydrate();
37
37
  var webchatConfig = config.webchat === false ? false : this.mergeWebchatConfig(businessData && businessData.webchat || {}, config.webchat || {});
38
+ var whatsappConfig = config.whatsappWidget === false ? false : this.mergeWhatsAppConfig(businessData && businessData.whatsapp || {}, config.whatsappWidget || {});
38
39
  var hasExplicitBehaviourOverride = config.webchat && config.webchat !== false && Object.prototype.hasOwnProperty.call(config.webchat, 'behaviour');
39
40
  Configuration.webchat.behaviourOverride = hasExplicitBehaviourOverride;
40
41
  if (webchatConfig && webchatConfig.id) {
41
42
  Configuration.webchat.assign(webchatConfig);
42
43
  this.webchat = yield Webchat.load(webchatConfig.id);
43
44
  }
45
+ if (whatsappConfig && whatsappConfig.id) {
46
+ Configuration.whatsapp.assign(whatsappConfig);
47
+ this.whatsapp = yield WhatsAppWidget.load(whatsappConfig.id);
48
+ }
44
49
  if (typeof MutationObserver !== 'undefined') {
45
50
  this.forms.collectExistingFormsOnPage();
46
51
  }
@@ -55,6 +60,11 @@ var Hellotext = /*#__PURE__*/function () {
55
60
  value: function mergeWebchatConfig(dashboardConfig, localConfig) {
56
61
  return this.deepMergePlainObjects(dashboardConfig, localConfig);
57
62
  }
63
+ }, {
64
+ key: "mergeWhatsAppConfig",
65
+ value: function mergeWhatsAppConfig(dashboardConfig, localConfig) {
66
+ return this.deepMergePlainObjects(dashboardConfig, localConfig);
67
+ }
58
68
  }, {
59
69
  key: "deepMergePlainObjects",
60
70
  value: function deepMergePlainObjects(base, override) {
@@ -240,4 +250,5 @@ Hellotext.eventEmitter = new Event();
240
250
  Hellotext.forms = void 0;
241
251
  Hellotext.business = void 0;
242
252
  Hellotext.webchat = void 0;
253
+ Hellotext.whatsapp = void 0;
243
254
  export default Hellotext;
@@ -69,6 +69,12 @@ Object.defineProperty(exports, "Webchat", {
69
69
  return _webchat.Webchat;
70
70
  }
71
71
  });
72
+ Object.defineProperty(exports, "WhatsAppWidget", {
73
+ enumerable: true,
74
+ get: function () {
75
+ return _whatsapp_widget.WhatsAppWidget;
76
+ }
77
+ });
72
78
  var _business = require("./business");
73
79
  var _cookies = require("./cookies");
74
80
  var _fingerprint = require("./fingerprint");
@@ -79,4 +85,5 @@ var _query = require("./query");
79
85
  var _session = require("./session");
80
86
  var _user = require("./user");
81
87
  var _utm = require("./utm");
82
- var _webchat = require("./webchat");
88
+ var _webchat = require("./webchat");
89
+ var _whatsapp_widget = require("./whatsapp_widget");
@@ -8,4 +8,5 @@ export { Query } from './query';
8
8
  export { Session } from './session';
9
9
  export { User } from './user';
10
10
  export { UTM } from './utm';
11
- export { Webchat } from './webchat';
11
+ export { Webchat } from './webchat';
12
+ export { WhatsAppWidget } from './whatsapp_widget';
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.Session = void 0;
7
7
  var _core = require("../core");
8
8
  var _cookies = require("./cookies");
9
+ var _page2 = require("./page");
9
10
  var _query2 = require("./query");
10
11
  var _api = _interopRequireDefault(require("../api"));
11
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -19,6 +20,7 @@ var id = 0;
19
20
  function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; }
20
21
  var _session = /*#__PURE__*/_classPrivateFieldLooseKey("session");
21
22
  var _query = /*#__PURE__*/_classPrivateFieldLooseKey("query");
23
+ var _page = /*#__PURE__*/_classPrivateFieldLooseKey("page");
22
24
  let Session = /*#__PURE__*/function () {
23
25
  function Session() {
24
26
  _classCallCheck(this, Session);
@@ -36,14 +38,23 @@ let Session = /*#__PURE__*/function () {
36
38
  _cookies.Cookies.delete('hello_session_ack_at');
37
39
  }
38
40
  if (!_cookies.Cookies.get('hello_session_ack_at')) {
39
- _api.default.acks.send();
41
+ _api.default.acks.send(this.ackPayload);
40
42
  _cookies.Cookies.set('hello_session_ack_at', new Date().toISOString());
41
43
  }
42
44
  return _classPrivateFieldLooseBase(this, _session)[_session];
43
45
  }
46
+ }, {
47
+ key: "ackPayload",
48
+ get: function () {
49
+ var _classPrivateFieldLoo;
50
+ return {
51
+ utm_params: ((_classPrivateFieldLoo = _classPrivateFieldLooseBase(this, _page)[_page]) === null || _classPrivateFieldLoo === void 0 ? void 0 : _classPrivateFieldLoo.utmParams) || {}
52
+ };
53
+ }
44
54
  }, {
45
55
  key: "initialize",
46
- value: function initialize() {
56
+ value: function initialize(page = new _page2.Page()) {
57
+ _classPrivateFieldLooseBase(this, _page)[_page] = page;
47
58
  _classPrivateFieldLooseBase(this, _query)[_query] = new _query2.Query();
48
59
  this.session = _classPrivateFieldLooseBase(this, _query)[_query].session || _core.Configuration.session || _cookies.Cookies.get('hello_session');
49
60
  if (!this.session && _core.Configuration.autoGenerateSession) {
@@ -61,4 +72,8 @@ Object.defineProperty(Session, _session, {
61
72
  Object.defineProperty(Session, _query, {
62
73
  writable: true,
63
74
  value: void 0
75
+ });
76
+ Object.defineProperty(Session, _page, {
77
+ writable: true,
78
+ value: void 0
64
79
  });
@@ -8,10 +8,12 @@ var id = 0;
8
8
  function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; }
9
9
  import { Configuration } from '../core';
10
10
  import { Cookies } from './cookies';
11
+ import { Page } from './page';
11
12
  import { Query } from './query';
12
13
  import API from '../api';
13
14
  var _session = /*#__PURE__*/_classPrivateFieldLooseKey("session");
14
15
  var _query = /*#__PURE__*/_classPrivateFieldLooseKey("query");
16
+ var _page = /*#__PURE__*/_classPrivateFieldLooseKey("page");
15
17
  var Session = /*#__PURE__*/function () {
16
18
  function Session() {
17
19
  _classCallCheck(this, Session);
@@ -29,14 +31,24 @@ var Session = /*#__PURE__*/function () {
29
31
  Cookies.delete('hello_session_ack_at');
30
32
  }
31
33
  if (!Cookies.get('hello_session_ack_at')) {
32
- API.acks.send();
34
+ API.acks.send(this.ackPayload);
33
35
  Cookies.set('hello_session_ack_at', new Date().toISOString());
34
36
  }
35
37
  return _classPrivateFieldLooseBase(this, _session)[_session];
36
38
  }
39
+ }, {
40
+ key: "ackPayload",
41
+ get: function get() {
42
+ var _classPrivateFieldLoo;
43
+ return {
44
+ utm_params: ((_classPrivateFieldLoo = _classPrivateFieldLooseBase(this, _page)[_page]) === null || _classPrivateFieldLoo === void 0 ? void 0 : _classPrivateFieldLoo.utmParams) || {}
45
+ };
46
+ }
37
47
  }, {
38
48
  key: "initialize",
39
49
  value: function initialize() {
50
+ var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Page();
51
+ _classPrivateFieldLooseBase(this, _page)[_page] = page;
40
52
  _classPrivateFieldLooseBase(this, _query)[_query] = new Query();
41
53
  this.session = _classPrivateFieldLooseBase(this, _query)[_query].session || Configuration.session || Cookies.get('hello_session');
42
54
  if (!this.session && Configuration.autoGenerateSession) {
@@ -54,4 +66,8 @@ Object.defineProperty(Session, _query, {
54
66
  writable: true,
55
67
  value: void 0
56
68
  });
69
+ Object.defineProperty(Session, _page, {
70
+ writable: true,
71
+ value: void 0
72
+ });
57
73
  export { Session };
@@ -29,6 +29,7 @@ let Webchat = /*#__PURE__*/function () {
29
29
  return false;
30
30
  }
31
31
  this.containerToAppendTo.appendChild(this.data.html);
32
+ this.markCoexistingWidgets();
32
33
  this.mounted = true;
33
34
  return true;
34
35
  }
@@ -66,6 +67,15 @@ let Webchat = /*#__PURE__*/function () {
66
67
  get: function () {
67
68
  return _business.Business.waitForStylesheet(_business.Business.latestStylesheet);
68
69
  }
70
+ }, {
71
+ key: "markCoexistingWidgets",
72
+ value: function markCoexistingWidgets() {
73
+ const webchat = this.data.html;
74
+ const whatsapp = document.querySelector('.hellotext--whatsapp-widget');
75
+ if (!webchat || !whatsapp) return;
76
+ webchat.classList.add('hellotext--with-whatsapp-widget');
77
+ whatsapp.classList.add('hellotext--with-webchat');
78
+ }
69
79
  }], [{
70
80
  key: "load",
71
81
  value: async function load(id) {
@@ -25,6 +25,7 @@ var Webchat = /*#__PURE__*/function () {
25
25
  return false;
26
26
  }
27
27
  this.containerToAppendTo.appendChild(this.data.html);
28
+ this.markCoexistingWidgets();
28
29
  this.mounted = true;
29
30
  return true;
30
31
  });
@@ -67,6 +68,15 @@ var Webchat = /*#__PURE__*/function () {
67
68
  get: function get() {
68
69
  return Business.waitForStylesheet(Business.latestStylesheet);
69
70
  }
71
+ }, {
72
+ key: "markCoexistingWidgets",
73
+ value: function markCoexistingWidgets() {
74
+ var webchat = this.data.html;
75
+ var whatsapp = document.querySelector('.hellotext--whatsapp-widget');
76
+ if (!webchat || !whatsapp) return;
77
+ webchat.classList.add('hellotext--with-whatsapp-widget');
78
+ whatsapp.classList.add('hellotext--with-webchat');
79
+ }
70
80
  }], [{
71
81
  key: "load",
72
82
  value: function () {
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.WhatsAppWidget = void 0;
7
+ var _core = require("../core");
8
+ var _api = _interopRequireDefault(require("../api"));
9
+ var _business = require("./business");
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
+ 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); } }
13
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
14
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
15
+ 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); }
16
+ let WhatsAppWidget = /*#__PURE__*/function () {
17
+ function WhatsAppWidget(data) {
18
+ _classCallCheck(this, WhatsAppWidget);
19
+ this.data = data;
20
+ this.mounted = false;
21
+ this.rendered = Promise.resolve(false);
22
+ }
23
+ _createClass(WhatsAppWidget, [{
24
+ key: "render",
25
+ value: async function render() {
26
+ if (!this.data.html) return false;
27
+ const container = this.containerToAppendTo;
28
+ if (!container) {
29
+ console.warn(`Hellotext WhatsApp widget was not mounted because the container ${_core.Configuration.whatsapp.container} was not found.`);
30
+ return false;
31
+ }
32
+ if (!(await this.stylesheetLoaded)) {
33
+ console.warn('Hellotext WhatsApp widget was not mounted because its stylesheet failed to load.');
34
+ return false;
35
+ }
36
+ container.appendChild(this.data.html);
37
+ this.markCoexistingWidgets();
38
+ this.mounted = true;
39
+ return true;
40
+ }
41
+ }, {
42
+ key: "containerToAppendTo",
43
+ get: function () {
44
+ try {
45
+ return document.querySelector(_core.Configuration.whatsapp.container);
46
+ } catch (_) {
47
+ return null;
48
+ }
49
+ }
50
+ }, {
51
+ key: "stylesheetLoaded",
52
+ get: function () {
53
+ return _business.Business.waitForStylesheet(_business.Business.latestStylesheet);
54
+ }
55
+ }, {
56
+ key: "markCoexistingWidgets",
57
+ value: function markCoexistingWidgets() {
58
+ const webchat = document.querySelector('.hellotext--webchat:not(.hellotext--whatsapp-widget)');
59
+ const whatsapp = this.data.html;
60
+ if (!webchat || !whatsapp) return;
61
+ webchat.classList.add('hellotext--with-whatsapp-widget');
62
+ whatsapp.classList.add('hellotext--with-webchat');
63
+ }
64
+ }], [{
65
+ key: "load",
66
+ value: async function load(id) {
67
+ const widget = new WhatsAppWidget({
68
+ id,
69
+ html: await _api.default.whatsappWidgets.get(id)
70
+ });
71
+ widget.rendered = widget.render();
72
+ return widget;
73
+ }
74
+ }]);
75
+ return WhatsAppWidget;
76
+ }();
77
+ exports.WhatsAppWidget = WhatsAppWidget;
@@ -0,0 +1,84 @@
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
2
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
3
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
+ 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); } }
5
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
6
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
7
+ 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); }
8
+ import { Configuration } from '../core';
9
+ import API from '../api';
10
+ import { Business } from './business';
11
+ var WhatsAppWidget = /*#__PURE__*/function () {
12
+ function WhatsAppWidget(data) {
13
+ _classCallCheck(this, WhatsAppWidget);
14
+ this.data = data;
15
+ this.mounted = false;
16
+ this.rendered = Promise.resolve(false);
17
+ }
18
+ _createClass(WhatsAppWidget, [{
19
+ key: "render",
20
+ value: function () {
21
+ var _render = _asyncToGenerator(function* () {
22
+ if (!this.data.html) return false;
23
+ var container = this.containerToAppendTo;
24
+ if (!container) {
25
+ console.warn("Hellotext WhatsApp widget was not mounted because the container ".concat(Configuration.whatsapp.container, " was not found."));
26
+ return false;
27
+ }
28
+ if (!(yield this.stylesheetLoaded)) {
29
+ console.warn('Hellotext WhatsApp widget was not mounted because its stylesheet failed to load.');
30
+ return false;
31
+ }
32
+ container.appendChild(this.data.html);
33
+ this.markCoexistingWidgets();
34
+ this.mounted = true;
35
+ return true;
36
+ });
37
+ function render() {
38
+ return _render.apply(this, arguments);
39
+ }
40
+ return render;
41
+ }()
42
+ }, {
43
+ key: "containerToAppendTo",
44
+ get: function get() {
45
+ try {
46
+ return document.querySelector(Configuration.whatsapp.container);
47
+ } catch (_) {
48
+ return null;
49
+ }
50
+ }
51
+ }, {
52
+ key: "stylesheetLoaded",
53
+ get: function get() {
54
+ return Business.waitForStylesheet(Business.latestStylesheet);
55
+ }
56
+ }, {
57
+ key: "markCoexistingWidgets",
58
+ value: function markCoexistingWidgets() {
59
+ var webchat = document.querySelector('.hellotext--webchat:not(.hellotext--whatsapp-widget)');
60
+ var whatsapp = this.data.html;
61
+ if (!webchat || !whatsapp) return;
62
+ webchat.classList.add('hellotext--with-whatsapp-widget');
63
+ whatsapp.classList.add('hellotext--with-webchat');
64
+ }
65
+ }], [{
66
+ key: "load",
67
+ value: function () {
68
+ var _load = _asyncToGenerator(function* (id) {
69
+ var widget = new WhatsAppWidget({
70
+ id,
71
+ html: yield API.whatsappWidgets.get(id)
72
+ });
73
+ widget.rendered = widget.render();
74
+ return widget;
75
+ });
76
+ function load(_x) {
77
+ return _load.apply(this, arguments);
78
+ }
79
+ return load;
80
+ }()
81
+ }]);
82
+ return WhatsAppWidget;
83
+ }();
84
+ export { WhatsAppWidget };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hellotext/hellotext",
3
- "version": "2.4.52",
3
+ "version": "2.5.0",
4
4
  "description": "Hellotext JavaScript Client",
5
5
  "source": "src/index.js",
6
6
  "main": "lib/index.cjs",
package/src/api/acks.js CHANGED
@@ -7,8 +7,9 @@ class AcksAPI {
7
7
  return Configuration.endpoint('public/acks')
8
8
  }
9
9
 
10
- static async send() {
10
+ static async send(params = {}) {
11
11
  const payload = {
12
+ ...params,
12
13
  session: Hellotext.session,
13
14
  at: new Date().toISOString(),
14
15
  }
package/src/api/index.js CHANGED
@@ -3,6 +3,7 @@ import EventsAPI from './events'
3
3
  import FormsAPI from './forms'
4
4
  import IdentificationsAPI from './identifications'
5
5
  import WebchatsAPI from './webchats'
6
+ import WhatsAppWidgetsAPI from './whatsapp_widgets'
6
7
  import AcksAPI from './acks'
7
8
 
8
9
  // Browsers keep `fetch(..., { keepalive: true })` requests alive during page
@@ -45,6 +46,10 @@ export default class API {
45
46
  return WebchatsAPI
46
47
  }
47
48
 
49
+ static get whatsappWidgets() {
50
+ return WhatsAppWidgetsAPI
51
+ }
52
+
48
53
  static get identifications() {
49
54
  return IdentificationsAPI
50
55
  }
@@ -0,0 +1,71 @@
1
+ import { Configuration, Locale } from '../core'
2
+ import Hellotext from '../hellotext'
3
+
4
+ class WhatsAppWidgetsAPI {
5
+ static get endpoint() {
6
+ return Configuration.endpoint('public/widgets/whatsapp')
7
+ }
8
+
9
+ static async get(id) {
10
+ const url = new URL(`${this.endpoint}/${id}`)
11
+
12
+ url.searchParams.append('locale', Locale.toString())
13
+ url.searchParams.append('placement', Configuration.whatsapp.placement)
14
+ this.appendWhatsAppOverrides(url)
15
+
16
+ const response = await this.fetchWidget(url)
17
+
18
+ if (!response.ok) return null
19
+
20
+ const data = await this.parseWidgetResponse(response)
21
+
22
+ if (!data) return null
23
+
24
+ if (!Hellotext.business.data) {
25
+ Hellotext.business.setData(data.business)
26
+ Hellotext.business.setLocale(data.locale)
27
+ }
28
+
29
+ return new DOMParser().parseFromString(data.html, 'text/html').querySelector('article')
30
+ }
31
+
32
+ static appendWhatsAppOverrides(url) {
33
+ const { appearance, body, number } = Configuration.whatsapp
34
+
35
+ this.appendIfSupplied(
36
+ url,
37
+ 'whatsapp[appearance][launcher][icon_url]',
38
+ appearance.launcher?.iconUrl,
39
+ )
40
+
41
+ this.appendIfSupplied(url, 'whatsapp[number]', number)
42
+ this.appendIfSupplied(url, 'whatsapp[body]', body)
43
+ }
44
+
45
+ static appendIfSupplied(url, key, value) {
46
+ if (value === undefined || value === null) return
47
+
48
+ url.searchParams.append(key, String(value))
49
+ }
50
+
51
+ static async fetchWidget(url) {
52
+ try {
53
+ return await fetch(url, {
54
+ method: 'GET',
55
+ headers: Hellotext.headers,
56
+ })
57
+ } catch (_) {
58
+ return { ok: false }
59
+ }
60
+ }
61
+
62
+ static async parseWidgetResponse(response) {
63
+ try {
64
+ return await response.json()
65
+ } catch (_) {
66
+ return null
67
+ }
68
+ }
69
+ }
70
+
71
+ export default WhatsAppWidgetsAPI