@saasquatch/squatch-js 2.6.0-4 → 2.6.0-5

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.
Files changed (36) hide show
  1. package/coverage/clover.xml +843 -64
  2. package/coverage/coverage-final.json +19 -1
  3. package/coverage/lcov-report/DeclarativeWidget.ts.html +298 -118
  4. package/coverage/lcov-report/Widgets.ts.html +70 -22
  5. package/coverage/lcov-report/api/AnalyticsApi.ts.html +38 -38
  6. package/coverage/lcov-report/api/EventsApi.ts.html +352 -0
  7. package/coverage/lcov-report/api/WidgetApi.ts.html +88 -61
  8. package/coverage/lcov-report/api/graphql.ts.html +1 -1
  9. package/coverage/lcov-report/squatch.ts.html +721 -0
  10. package/coverage/lcov-report/utils/cookieUtils.ts.html +1 -1
  11. package/coverage/lcov-report/utils/decodeUserJwt.ts.html +1 -1
  12. package/coverage/lcov-report/utils/domready.ts.html +1 -1
  13. package/coverage/lcov-report/utils/io.ts.html +7 -7
  14. package/coverage/lcov-report/utils/utmUtils.ts.html +22 -22
  15. package/coverage/lcov-report/utils/validate.ts.html +41 -41
  16. package/coverage/lcov-report/validate.ts.html +1 -1
  17. package/coverage/lcov-report/widgets/EmbedWidget.ts.html +24 -3
  18. package/coverage/lcov-report/widgets/PopupWidget.ts.html +17 -2
  19. package/coverage/lcov-report/widgets/Widget.ts.html +1 -1
  20. package/coverage/lcov-report/widgets/Widgets.ts.html +61 -16
  21. package/coverage/lcov-report/widgets/declarative/DeclarativeWidget.ts.html +368 -74
  22. package/coverage/lcov-report/widgets/declarative/DeclarativeWidgets.ts.html +388 -0
  23. package/coverage/lcov.info +1553 -125
  24. package/dist/api/WidgetApi.d.ts +2 -2
  25. package/dist/squatch.d.ts +28 -1
  26. package/dist/squatch.esm.js +176 -29
  27. package/dist/squatch.esm.js.map +1 -1
  28. package/dist/squatch.js +176 -29
  29. package/dist/squatch.js.map +1 -1
  30. package/dist/squatch.min.js +1 -1
  31. package/dist/widgets/EmbedWidget.d.ts +5 -0
  32. package/dist/widgets/PopupWidget.d.ts +6 -1
  33. package/dist/widgets/Widgets.d.ts +15 -2
  34. package/dist/widgets/declarative/DeclarativeWidget.d.ts +57 -1
  35. package/dist/widgets/declarative/DeclarativeWidgets.d.ts +22 -0
  36. package/package.json +1 -1
@@ -26,7 +26,7 @@ export default class WidgetApi {
26
26
  */
27
27
  constructor(config?: ConfigOptions);
28
28
  /**
29
- * Creates/upserts user.
29
+ * Creates/upserts user, requests widget template.
30
30
  *
31
31
  * @param {Object} params Parameters for request
32
32
  * @param {Object?} params.user The user details
@@ -41,7 +41,7 @@ export default class WidgetApi {
41
41
  */
42
42
  upsertUser(params: WithRequired<WidgetConfig, "user">): Promise<any>;
43
43
  /**
44
- * Description here.
44
+ * Requests widget template
45
45
  *
46
46
  * @param {Object} params Parameters for request
47
47
  * @param {Object} params.user The user details
package/dist/squatch.d.ts CHANGED
@@ -14,6 +14,10 @@ export { Widgets, EmbedWidget, PopupWidget, DeclarativeEmbedWidget, DeclarativeP
14
14
  * Read the {@link WidgetApi} docs.
15
15
  *
16
16
  * @returns WidgetApi static instance
17
+ * @example
18
+ * squatch.api().render({ ... })
19
+ * squatch.api().upsertUser({ ... })
20
+ * squatch.api().squatchReferralCookie()
17
21
  */
18
22
  export declare function api(): WidgetApi | null;
19
23
  /**
@@ -22,6 +26,10 @@ export declare function api(): WidgetApi | null;
22
26
  * Read the {@link Widgets} docs.
23
27
  *
24
28
  * @returns static instance
29
+ * @example
30
+ * squatch.widgets().render({ widgetType: "w/widget-type" })
31
+ * squatch.widgets().upsertUser({ user: { ... }, widgetType: "w/widget-type" })
32
+ * squatch.widgets().autofill(".referral-code")
25
33
  */
26
34
  export declare function widgets(): Widgets | null;
27
35
  /**
@@ -30,14 +38,27 @@ export declare function widgets(): Widgets | null;
30
38
  * Read the {@link EventsApi} docs.
31
39
  *
32
40
  * @returns EventsApi static instance
41
+ *
42
+ * @example
43
+ * squatch.events().track({ ... })
33
44
  */
34
45
  export declare function events(): EventsApi | null;
35
46
  /**
36
47
  * Entry-point for high level API to render a widget using the instance of {@link Widgets} created when you call {@link #init init}.
48
+ *
49
+ * Read the {@link Widgets.render} docs.
50
+ *
51
+ * @example
52
+ * squatch.widget().then(res => {
53
+ * const widget = res.widget
54
+ * }).catch(e => console.error("Did not render widget:", e))
37
55
  */
38
56
  export declare function widget(widgetConfig: WidgetConfig): Promise<WidgetResult | undefined> | undefined;
39
57
  /**
40
58
  * Extracts widget configuration from `_saasquatchExtra` UTM parameter. Initialises `squatch` and renders the widget as a {@link PopupWidget} via static instanct of {@link Widgets}.
59
+ *
60
+ * Called by default on startup via the loader script.
61
+ * @private
41
62
  */
42
63
  export declare function _auto(configIn: ConfigOptions): Promise<WidgetResult | undefined> | undefined;
43
64
  /**
@@ -50,7 +71,9 @@ export declare function _auto(configIn: ConfigOptions): Promise<WidgetResult | u
50
71
  * @param config Configuration details
51
72
  *
52
73
  * @example
53
- * squatch.init({tenantAlias:'test_basbtabstq51v'});
74
+ * squatch.init({
75
+ * tenantAlias:'test_basbtabstq51v',
76
+ * });
54
77
  */
55
78
  export declare function init(configIn: ConfigOptions): void;
56
79
  /**
@@ -72,6 +95,10 @@ export declare function ready(fn: () => any): void;
72
95
  *
73
96
  * @param {string} selector Element class/id
74
97
  * @returns {void}
98
+ *
99
+ * @example
100
+ * squatch.autofill("input.referral-code")
101
+ * squatch.autofill("input#referral-code")
75
102
  */
76
103
  export declare function autofill(selector: string): void;
77
104
  /**
@@ -211,7 +211,7 @@ class WidgetApi {
211
211
  this.npmCdn = clean.npmCdn;
212
212
  }
213
213
  /**
214
- * Creates/upserts user.
214
+ * Creates/upserts user, requests widget template.
215
215
  *
216
216
  * @param {Object} params Parameters for request
217
217
  * @param {Object?} params.user The user details
@@ -253,7 +253,7 @@ class WidgetApi {
253
253
  return doPut(url, JSON.stringify(user), jwt);
254
254
  }
255
255
  /**
256
- * Description here.
256
+ * Requests widget template
257
257
  *
258
258
  * @param {Object} params Parameters for request
259
259
  * @param {Object} params.user The user details
@@ -475,8 +475,7 @@ class Widget {
475
475
  }
476
476
 
477
477
  _findFrame() {
478
- var element = this._findElement();
479
-
478
+ var element = this.container ? this._findElement() : document.body;
480
479
  var parent = element.shadowRoot || element;
481
480
  return parent.querySelector("iframe#squatchFrame");
482
481
  }
@@ -682,10 +681,7 @@ class Widget {
682
681
 
683
682
  function delay(duration) {
684
683
  return new Promise(function (resolve, reject) {
685
- setTimeout(function () {
686
- /* istanbul ignore next */
687
- resolve(() => {});
688
- }, duration);
684
+ setTimeout(resolve, duration);
689
685
  });
690
686
  }
691
687
 
@@ -720,6 +716,11 @@ var _log$6 = debug("squatch-js:EMBEDwidget");
720
716
  *
721
717
  * To create an EmbedWidget use {@link Widgets}
722
718
  *
719
+ * @example
720
+ * const widget = new EmbedWidget({ ... })
721
+ * widget.load() // Loads widget into the DOM
722
+ * widget.open() // Makes the iframe container visible
723
+ * widget.close() // Hides the iframe container
723
724
  */
724
725
 
725
726
 
@@ -771,6 +772,8 @@ class EmbedWidget extends Widget {
771
772
 
772
773
  frame.height = frameDoc.body.scrollHeight; // Adjust frame height when size of body changes
773
774
 
775
+ /* istanbul ignore next: hard to test */
776
+
774
777
  var ro = new contentWindow["ResizeObserver"](entries => {
775
778
  for (var entry of entries) {
776
779
  var {
@@ -780,9 +783,10 @@ class EmbedWidget extends Widget {
780
783
  frame.height = height;
781
784
  }
782
785
  });
783
- ro.observe(await this._findInnerContainer(frame)); // Regular load - trigger event
786
+ var container = await this._findInnerContainer(frame);
787
+ ro.observe(container); // Regular load - trigger event
784
788
 
785
- if (!this.container) {
789
+ if (!this.container || this.container instanceof HTMLElement && this.container.tagName.startsWith("SQUATCH-")) {
786
790
  this._loadEvent(_sqh);
787
791
 
788
792
  _log$6("loaded");
@@ -854,6 +858,11 @@ var popupId = 0;
854
858
  *
855
859
  * To create a PopupWidget use {@link Widgets}
856
860
  *
861
+ * @example
862
+ * const widget = new PopupWidget({ ... })
863
+ * widget.load() // Loads the widget into a dialog element
864
+ * widget.open() // Opens the dialog element
865
+ * widget.close() // Hides the dialog element
857
866
  */
858
867
 
859
868
  class PopupWidget extends Widget {
@@ -879,7 +888,7 @@ class PopupWidget extends Widget {
879
888
  document.head.insertAdjacentHTML("beforeend", "<style>#" + this.id + "::-webkit-scrollbar { display: none; }</style>");
880
889
  }
881
890
 
882
- _initialiseCTA(frame) {
891
+ _initialiseCTA() {
883
892
  if (!this.trigger) return;
884
893
  var triggerElement;
885
894
 
@@ -927,10 +936,9 @@ class PopupWidget extends Widget {
927
936
 
928
937
  var frame = this._createFrame();
929
938
 
930
- this._initialiseCTA(frame);
931
-
932
- var element = this._findElement();
939
+ this._initialiseCTA();
933
940
 
941
+ var element = this.container ? this._findElement() : document.body;
934
942
  var dialogParent = element.shadowRoot || element;
935
943
 
936
944
  var dialog = this._createPopupDialog();
@@ -996,8 +1004,7 @@ class PopupWidget extends Widget {
996
1004
  }
997
1005
 
998
1006
  open() {
999
- var element = this._findElement();
1000
-
1007
+ var element = this.container ? this._findElement() : document.body;
1001
1008
  var parent = element.shadowRoot || element;
1002
1009
  var dialog = parent.querySelector("#" + this.id);
1003
1010
  if (!dialog) throw new Error("Could not determine container div");
@@ -1026,8 +1033,7 @@ class PopupWidget extends Widget {
1026
1033
  }
1027
1034
 
1028
1035
  close() {
1029
- var element = this._findElement();
1030
-
1036
+ var element = this.container ? this._findElement() : document.body;
1031
1037
  var parent = element.shadowRoot || element;
1032
1038
  var dialog = parent.querySelector("#" + this.id);
1033
1039
  if (!dialog) throw new Error("Could not determine container div");
@@ -1056,14 +1062,31 @@ class PopupWidget extends Widget {
1056
1062
 
1057
1063
  var _log$4 = debug("squatch-js:widgets");
1058
1064
  /**
1059
- *
1060
1065
  * `Widgets` is a factory for creating widgets. It's possible to build your own widgets using the
1061
1066
  * {@link WidgetApi} but most people will prefer to use these easy methods.
1062
- *
1067
+ * @class
1063
1068
  */
1064
1069
 
1065
1070
 
1066
1071
  class Widgets {
1072
+ /**
1073
+ * Instance of {@link WidgetApi}
1074
+ */
1075
+
1076
+ /**
1077
+ * Tenant alias of SaaSquatch tenant.
1078
+ */
1079
+
1080
+ /**
1081
+ * SaaSquatch domain for API requests.
1082
+ * @default "https://app.referralsaasquatch.com"
1083
+ */
1084
+
1085
+ /**
1086
+ * Hosted CDN for npm packages
1087
+ * @default "https://fast.ssqt.io/npm"
1088
+ */
1089
+
1067
1090
  /**
1068
1091
  * Initialize a new {@link Widgets} instance.
1069
1092
  *
@@ -1089,7 +1112,7 @@ class Widgets {
1089
1112
  this.tenantAlias = config.tenantAlias;
1090
1113
  this.domain = config.domain;
1091
1114
  this.npmCdn = config.npmCdn;
1092
- this.api = new WidgetApi(config); // listens to a 'submit_email' event in the theme.
1115
+ this.api = new WidgetApi(config);
1093
1116
  }
1094
1117
  /**
1095
1118
  * This function calls the {@link WidgetApi.upsertUser} method, and it renders
@@ -1161,7 +1184,9 @@ class Widgets {
1161
1184
  return {
1162
1185
  widget: this._renderWidget(response, clean, {
1163
1186
  type: "passwordless",
1164
- engagementMedium: clean.engagementMedium
1187
+ engagementMedium: clean.engagementMedium,
1188
+ container: clean.container,
1189
+ trigger: clean.trigger
1165
1190
  }),
1166
1191
  user: response.user
1167
1192
  };
@@ -1370,9 +1395,6 @@ class Widgets {
1370
1395
 
1371
1396
  static _matchesUrl(rule) {
1372
1397
  // If there were no matches, null is returned.
1373
- console.log({
1374
- href: window.location.href
1375
- });
1376
1398
  return window.location.href.match(new RegExp(rule)) ? true : false;
1377
1399
  }
1378
1400
 
@@ -1667,8 +1689,62 @@ function decodeUserJwt(tokenStr) {
1667
1689
  }
1668
1690
 
1669
1691
  debug$1("sqh:DeclarativeWidget");
1692
+ /**
1693
+ * Abstract class for building web-components that render SaaSquatch widgets to the DOM.
1694
+ * @abstract
1695
+ * @example
1696
+ * class TestWidgetElement extends DeclarativeWidget {}
1697
+ * const testWidget = new TestWidgetElement()
1698
+ * testWidget.widgetType = 'w/widget-type'
1699
+ * testWidget.type = 'EMBED'
1700
+ * testWidget.renderWidget()
1701
+ */
1702
+
1670
1703
 
1671
1704
  class DeclarativeWidget extends HTMLElement {
1705
+ /**
1706
+ * Configuration overrides
1707
+ * @default window.squatchConfig
1708
+ */
1709
+
1710
+ /**
1711
+ * Signed JWT containing user information
1712
+ * @default window.squatchToken
1713
+ */
1714
+
1715
+ /**
1716
+ * Tenant alias of SaaSquatch tenant
1717
+ * @default window.squatchTenant
1718
+ */
1719
+
1720
+ /**
1721
+ * widgetType of widget to load
1722
+ */
1723
+
1724
+ /**
1725
+ * Locale to render the widget in
1726
+ */
1727
+
1728
+ /**
1729
+ * Instance of {@link WidgetApi}
1730
+ */
1731
+
1732
+ /**
1733
+ * Instance of {@link AnalyticsApi}
1734
+ */
1735
+
1736
+ /**
1737
+ * Instance of {@link EmbedWidget} or {@link PopupWidget}
1738
+ */
1739
+
1740
+ /**
1741
+ * Determines whether to render the widget as an embedding widget or popup widget.
1742
+ */
1743
+
1744
+ /**
1745
+ * Container element to contain the widget iframe
1746
+ * @default this
1747
+ */
1672
1748
  constructor() {
1673
1749
  super();
1674
1750
  this.config = void 0;
@@ -1678,8 +1754,8 @@ class DeclarativeWidget extends HTMLElement {
1678
1754
  this.locale = void 0;
1679
1755
  this.widgetApi = void 0;
1680
1756
  this.analyticsApi = void 0;
1681
- this.type = void 0;
1682
1757
  this.widgetInstance = void 0;
1758
+ this.type = void 0;
1683
1759
  this.container = void 0;
1684
1760
  this.element = void 0;
1685
1761
 
@@ -1738,7 +1814,6 @@ class DeclarativeWidget extends HTMLElement {
1738
1814
  this.token = window.squatchToken;
1739
1815
  this.tenant = window.squatchTenant;
1740
1816
  this.container = this;
1741
- this.locale = validateLocale(navigator.language.replace(/\-/g, "_"));
1742
1817
  }
1743
1818
 
1744
1819
  _setupApis(config) {
@@ -1787,6 +1862,11 @@ class DeclarativeWidget extends HTMLElement {
1787
1862
  return widgetInstance;
1788
1863
  }
1789
1864
 
1865
+ /**
1866
+ * Fetches widget content from SaaSquatch and builds a Widget instance to support rendering the widget in the DOM.
1867
+ * @returns Instance of either {@link EmbedWidget} or {@link PopupWidget} depending on `this.type`
1868
+ * @throws Throws an Error if `widgetType` is undefined
1869
+ */
1790
1870
  async getWidgetInstance() {
1791
1871
  var widgetInstance;
1792
1872
  this.widgetType = this.getAttribute("widget") || undefined;
@@ -1799,15 +1879,23 @@ class DeclarativeWidget extends HTMLElement {
1799
1879
  widgetInstance = await this.renderUserUpsertVariant();
1800
1880
  }
1801
1881
 
1802
- if (!widgetInstance) throw new Error("Could not create widget.");
1803
1882
  this.widgetInstance = widgetInstance;
1804
1883
  return widgetInstance;
1805
1884
  }
1885
+ /**
1886
+ * Calls {@link getWidgetInstance} to build the Widget instance and loads the widget iframe into the DOM.
1887
+ */
1888
+
1806
1889
 
1807
1890
  async renderWidget() {
1808
1891
  await this.getWidgetInstance();
1809
1892
  await this.widgetInstance.load();
1810
1893
  }
1894
+ /**
1895
+ * Builds a Widget instance for the default error widget.
1896
+ * @returns Instance of either {@link EmbedWidget} or {@link PopupWidget} depending on `this.type`
1897
+ */
1898
+
1811
1899
 
1812
1900
  /**
1813
1901
  * Calls `open` method of `widgetInstance`
@@ -1830,9 +1918,25 @@ class DeclarativeWidget extends HTMLElement {
1830
1918
 
1831
1919
  }
1832
1920
 
1921
+ /**
1922
+ * Base class for `squatch-embed` web-component
1923
+ * @extends {DeclarativeWidget}
1924
+ * @class
1925
+ * @example
1926
+ * window.createCustomElement('squatch-embed', DeclarativeEmbedWidget)
1927
+ * const widget = document.querySelector('squatch-embed') as DeclarativeEmbedWidget
1928
+ * widget.open()
1929
+ * widget.close()
1930
+ * widget.reload()
1931
+ */
1932
+
1833
1933
  class DeclarativeEmbedWidget extends DeclarativeWidget {
1834
1934
  constructor() {
1835
1935
  super();
1936
+ /**
1937
+ * @static
1938
+ */
1939
+
1836
1940
  this.type = "EMBED";
1837
1941
  }
1838
1942
 
@@ -1861,9 +1965,25 @@ class DeclarativeEmbedWidget extends DeclarativeWidget {
1861
1965
  }
1862
1966
 
1863
1967
  }
1968
+ /**
1969
+ * Base class for `squatch-popup` web-component
1970
+ * @extends {DeclarativeWidget}
1971
+ * @class
1972
+ * @example
1973
+ * window.createCustomElement('squatch-popup', DeclarativePopupWidget)
1974
+ * const widget = document.querySelector('squatch-popup') as DeclarativePopupWidget
1975
+ * widget.open()
1976
+ * widget.close()
1977
+ * widget.reload()
1978
+ */
1979
+
1864
1980
  class DeclarativePopupWidget extends DeclarativeWidget {
1865
1981
  constructor() {
1866
1982
  super();
1983
+ /**
1984
+ * @static
1985
+ */
1986
+
1867
1987
  this.type = "POPUP";
1868
1988
  this.addEventListener("click", e => {
1869
1989
  e.stopPropagation(); // SQUATCH-POPUP target means something in the shadowDOM was clicked (i.e. the dialog element)
@@ -1922,6 +2042,10 @@ var _events = null;
1922
2042
  * Read the {@link WidgetApi} docs.
1923
2043
  *
1924
2044
  * @returns WidgetApi static instance
2045
+ * @example
2046
+ * squatch.api().render({ ... })
2047
+ * squatch.api().upsertUser({ ... })
2048
+ * squatch.api().squatchReferralCookie()
1925
2049
  */
1926
2050
 
1927
2051
  function api() {
@@ -1934,6 +2058,10 @@ function api() {
1934
2058
  * Read the {@link Widgets} docs.
1935
2059
  *
1936
2060
  * @returns static instance
2061
+ * @example
2062
+ * squatch.widgets().render({ widgetType: "w/widget-type" })
2063
+ * squatch.widgets().upsertUser({ user: { ... }, widgetType: "w/widget-type" })
2064
+ * squatch.widgets().autofill(".referral-code")
1937
2065
  */
1938
2066
 
1939
2067
  function widgets() {
@@ -1946,6 +2074,9 @@ function widgets() {
1946
2074
  * Read the {@link EventsApi} docs.
1947
2075
  *
1948
2076
  * @returns EventsApi static instance
2077
+ *
2078
+ * @example
2079
+ * squatch.events().track({ ... })
1949
2080
  */
1950
2081
 
1951
2082
  function events() {
@@ -1954,6 +2085,13 @@ function events() {
1954
2085
  }
1955
2086
  /**
1956
2087
  * Entry-point for high level API to render a widget using the instance of {@link Widgets} created when you call {@link #init init}.
2088
+ *
2089
+ * Read the {@link Widgets.render} docs.
2090
+ *
2091
+ * @example
2092
+ * squatch.widget().then(res => {
2093
+ * const widget = res.widget
2094
+ * }).catch(e => console.error("Did not render widget:", e))
1957
2095
  */
1958
2096
 
1959
2097
  function widget(widgetConfig) {
@@ -1963,6 +2101,9 @@ function widget(widgetConfig) {
1963
2101
  }
1964
2102
  /**
1965
2103
  * Extracts widget configuration from `_saasquatchExtra` UTM parameter. Initialises `squatch` and renders the widget as a {@link PopupWidget} via static instanct of {@link Widgets}.
2104
+ *
2105
+ * Called by default on startup via the loader script.
2106
+ * @private
1966
2107
  */
1967
2108
 
1968
2109
  function _auto(configIn) {
@@ -1989,7 +2130,9 @@ function _auto(configIn) {
1989
2130
  * @param config Configuration details
1990
2131
  *
1991
2132
  * @example
1992
- * squatch.init({tenantAlias:'test_basbtabstq51v'});
2133
+ * squatch.init({
2134
+ * tenantAlias:'test_basbtabstq51v',
2135
+ * });
1993
2136
  */
1994
2137
 
1995
2138
  function init(configIn) {
@@ -2034,6 +2177,10 @@ function ready(fn) {
2034
2177
  *
2035
2178
  * @param {string} selector Element class/id
2036
2179
  * @returns {void}
2180
+ *
2181
+ * @example
2182
+ * squatch.autofill("input.referral-code")
2183
+ * squatch.autofill("input#referral-code")
2037
2184
  */
2038
2185
 
2039
2186
  function autofill(selector) {