@hellotext/hellotext 1.4.1 → 1.5.1

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 (91) hide show
  1. package/.babelrc +6 -3
  2. package/.github/workflows/ci.yml +1 -1
  3. package/.prettierignore +2 -0
  4. package/README.md +30 -170
  5. package/__tests__/api/sessions_test.js +12 -0
  6. package/__tests__/builders/input_builder_test.js +117 -0
  7. package/__tests__/{event_emitter_test.js → core/event_test.js} +26 -6
  8. package/__tests__/hellotext_test.js +12 -0
  9. package/__tests__/models/business_test.js +26 -0
  10. package/__tests__/models/cookies_test.js +25 -0
  11. package/__tests__/models/form_collection_test.js +100 -0
  12. package/__tests__/models/form_test.js +29 -0
  13. package/__tests__/models/query_test.js +97 -0
  14. package/dist/hellotext.js +1 -1
  15. package/docs/forms.md +127 -0
  16. package/docs/tracking.md +155 -0
  17. package/lib/api/businesses.js +45 -0
  18. package/lib/api/events.js +54 -0
  19. package/lib/api/forms.js +65 -0
  20. package/lib/api/index.js +51 -0
  21. package/lib/api/response.js +57 -0
  22. package/lib/api/sessions.js +46 -0
  23. package/lib/api/submissions.js +61 -0
  24. package/lib/api.js +73 -0
  25. package/lib/builders/inputBuilder.js +97 -0
  26. package/lib/builders/input_builder.js +61 -0
  27. package/lib/builders/logo_builder.js +38 -0
  28. package/lib/builders/otp_builder.js +39 -0
  29. package/lib/controllers/form_controller.js +139 -0
  30. package/lib/controllers/otp_controller.js +114 -0
  31. package/lib/core/configuration.js +37 -0
  32. package/lib/core/event.js +73 -0
  33. package/lib/core/index.js +20 -0
  34. package/lib/errors/index.js +19 -0
  35. package/lib/errors/invalid_event.js +34 -0
  36. package/lib/errors/not_initialized_error.js +34 -0
  37. package/lib/event.js +159 -32
  38. package/lib/eventEmitter.js +139 -46
  39. package/lib/forms.js +133 -0
  40. package/lib/hellotext.js +53 -105
  41. package/lib/index.js +7 -0
  42. package/lib/locales/en.js +20 -0
  43. package/lib/locales/es.js +20 -0
  44. package/lib/locales/index.js +14 -0
  45. package/lib/models/business.js +57 -0
  46. package/lib/models/configuration.js +66 -0
  47. package/lib/models/cookies.js +34 -0
  48. package/lib/models/form.js +148 -0
  49. package/lib/models/form_collection.js +96 -0
  50. package/lib/models/index.js +40 -0
  51. package/lib/models/query.js +51 -0
  52. package/lib/models/step.js +45 -0
  53. package/lib/query.js +65 -32
  54. package/lib/response.js +82 -42
  55. package/package.json +6 -3
  56. package/src/api/businesses.js +18 -0
  57. package/src/api/events.js +24 -0
  58. package/src/api/forms.js +30 -0
  59. package/src/api/index.js +24 -0
  60. package/src/{response.js → api/response.js} +9 -4
  61. package/src/api/sessions.js +23 -0
  62. package/src/api/submissions.js +30 -0
  63. package/src/builders/input_builder.js +55 -0
  64. package/src/builders/logo_builder.js +25 -0
  65. package/src/builders/otp_builder.js +42 -0
  66. package/src/controllers/form_controller.js +105 -0
  67. package/src/controllers/otp_controller.js +77 -0
  68. package/src/core/configuration.js +16 -0
  69. package/src/core/event.js +54 -0
  70. package/src/core/index.js +2 -0
  71. package/src/errors/index.js +2 -0
  72. package/src/hellotext.js +50 -72
  73. package/src/index.js +10 -0
  74. package/src/locales/en.js +13 -0
  75. package/src/locales/es.js +13 -0
  76. package/src/locales/index.js +7 -0
  77. package/src/models/business.js +41 -0
  78. package/src/models/cookies.js +16 -0
  79. package/src/models/form.js +133 -0
  80. package/src/models/form_collection.js +81 -0
  81. package/src/models/index.js +5 -0
  82. package/src/models/query.js +33 -0
  83. package/styles/index.css +32 -0
  84. package/webpack.config.js +15 -9
  85. package/__tests__/event_test.js +0 -21
  86. package/__tests__/query_test.js +0 -13
  87. package/src/event.js +0 -15
  88. package/src/eventEmitter.js +0 -28
  89. package/src/query.js +0 -21
  90. /package/src/errors/{invalidEvent.js → invalid_event.js} +0 -0
  91. /package/src/errors/{notInitializedError.js → not_initialized_error.js} +0 -0
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "InvalidEvent", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _invalid_event.InvalidEvent;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "NotInitializedError", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _not_initialized_error.NotInitializedError;
16
+ }
17
+ });
18
+ var _invalid_event = require("./invalid_event");
19
+ var _not_initialized_error = require("./not_initialized_error");
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.InvalidEvent = void 0;
7
+ 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); } }
8
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
9
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
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
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
13
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
14
+ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
15
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
16
+ function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
17
+ function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
18
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
19
+ function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
20
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
21
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
22
+ var InvalidEvent = /*#__PURE__*/function (_Error) {
23
+ _inherits(InvalidEvent, _Error);
24
+ var _super = _createSuper(InvalidEvent);
25
+ function InvalidEvent(event) {
26
+ var _this;
27
+ _classCallCheck(this, InvalidEvent);
28
+ _this = _super.call(this, "".concat(event, " is not valid. Please provide a valid event name"));
29
+ _this.name = 'InvalidEvent';
30
+ return _this;
31
+ }
32
+ return _createClass(InvalidEvent);
33
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
34
+ exports.InvalidEvent = InvalidEvent;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.NotInitializedError = void 0;
7
+ 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); } }
8
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
9
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
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
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
13
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
14
+ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
15
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
16
+ function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
17
+ function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
18
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
19
+ function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
20
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
21
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
22
+ var NotInitializedError = /*#__PURE__*/function (_Error) {
23
+ _inherits(NotInitializedError, _Error);
24
+ var _super = _createSuper(NotInitializedError);
25
+ function NotInitializedError() {
26
+ var _this;
27
+ _classCallCheck(this, NotInitializedError);
28
+ _this = _super.call(this, 'You need to initialize before tracking events. Call Hellotext.initialize and pass your public business id');
29
+ _this.name = 'NotInitializedError';
30
+ return _this;
31
+ }
32
+ return _createClass(NotInitializedError);
33
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
34
+ exports.NotInitializedError = NotInitializedError;
package/lib/event.js CHANGED
@@ -1,35 +1,162 @@
1
- "use strict";
1
+ 'use strict'
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
- 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); } }
9
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
10
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
11
- 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); }
12
- var Event = /*#__PURE__*/function () {
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true,
5
+ })
6
+ exports.default = void 0
7
+ var _invalidEvent = require('./errors/invalidEvent')
8
+ function ownKeys(object, enumerableOnly) {
9
+ var keys = Object.keys(object)
10
+ if (Object.getOwnPropertySymbols) {
11
+ var symbols = Object.getOwnPropertySymbols(object)
12
+ enumerableOnly &&
13
+ (symbols = symbols.filter(function (sym) {
14
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable
15
+ })),
16
+ keys.push.apply(keys, symbols)
17
+ }
18
+ return keys
19
+ }
20
+ function _objectSpread(target) {
21
+ for (var i = 1; i < arguments.length; i++) {
22
+ var source = null != arguments[i] ? arguments[i] : {}
23
+ i % 2
24
+ ? ownKeys(Object(source), !0).forEach(function (key) {
25
+ _defineProperty(target, key, source[key])
26
+ })
27
+ : Object.getOwnPropertyDescriptors
28
+ ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
29
+ : ownKeys(Object(source)).forEach(function (key) {
30
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key))
31
+ })
32
+ }
33
+ return target
34
+ }
35
+ function _defineProperty(obj, key, value) {
36
+ key = _toPropertyKey(key)
37
+ if (key in obj) {
38
+ Object.defineProperty(obj, key, {
39
+ value: value,
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ })
44
+ } else {
45
+ obj[key] = value
46
+ }
47
+ return obj
48
+ }
49
+ function _classCallCheck(instance, Constructor) {
50
+ if (!(instance instanceof Constructor)) {
51
+ throw new TypeError('Cannot call a class as a function')
52
+ }
53
+ }
54
+ function _defineProperties(target, props) {
55
+ for (var i = 0; i < props.length; i++) {
56
+ var descriptor = props[i]
57
+ descriptor.enumerable = descriptor.enumerable || false
58
+ descriptor.configurable = true
59
+ if ('value' in descriptor) descriptor.writable = true
60
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
61
+ }
62
+ }
63
+ function _createClass(Constructor, protoProps, staticProps) {
64
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps)
65
+ if (staticProps) _defineProperties(Constructor, staticProps)
66
+ Object.defineProperty(Constructor, 'prototype', { writable: false })
67
+ return Constructor
68
+ }
69
+ function _toPropertyKey(arg) {
70
+ var key = _toPrimitive(arg, 'string')
71
+ return typeof key === 'symbol' ? key : String(key)
72
+ }
73
+ function _toPrimitive(input, hint) {
74
+ if (typeof input !== 'object' || input === null) return input
75
+ var prim = input[Symbol.toPrimitive]
76
+ if (prim !== undefined) {
77
+ var res = prim.call(input, hint || 'default')
78
+ if (typeof res !== 'object') return res
79
+ throw new TypeError('@@toPrimitive must return a primitive value.')
80
+ }
81
+ return (hint === 'string' ? String : Number)(input)
82
+ }
83
+ var Event = /*#__PURE__*/ (function () {
13
84
  function Event() {
14
- _classCallCheck(this, Event);
85
+ _classCallCheck(this, Event)
86
+ this.subscribers = {}
15
87
  }
16
- _createClass(Event, null, [{
17
- key: "valid",
18
- value: function valid(name) {
19
- return Event.exists(name);
20
- }
21
- }, {
22
- key: "invalid",
23
- value: function invalid(name) {
24
- return !this.valid(name);
25
- }
26
- }, {
27
- key: "exists",
28
- value: function exists(name) {
29
- return this.events.find(eventName => eventName === name) !== undefined;
30
- }
31
- }]);
32
- return Event;
33
- }();
34
- exports.default = Event;
35
- Event.events = ["session-set"];
88
+ _createClass(
89
+ Event,
90
+ [
91
+ {
92
+ key: 'addSubscriber',
93
+ value: function addSubscriber(eventName, callback) {
94
+ if (Event.invalid(eventName)) {
95
+ throw new _invalidEvent.InvalidEvent(eventName)
96
+ }
97
+ this.subscribers = _objectSpread(
98
+ _objectSpread({}, this.subscribers),
99
+ {},
100
+ {
101
+ [eventName]: this.subscribers[eventName]
102
+ ? [...this.subscribers[eventName], callback]
103
+ : [callback],
104
+ },
105
+ )
106
+ },
107
+ },
108
+ {
109
+ key: 'removeSubscriber',
110
+ value: function removeSubscriber(eventName, callback) {
111
+ if (Event.invalid(eventName)) {
112
+ throw new _invalidEvent.InvalidEvent(eventName)
113
+ }
114
+ if (this.subscribers[eventName]) {
115
+ this.subscribers[eventName] = this.subscribers[eventName].filter(cb => cb !== callback)
116
+ }
117
+ },
118
+ },
119
+ {
120
+ key: 'dispatch',
121
+ value: function dispatch(eventName, data) {
122
+ var _this$subscribers$eve
123
+ ;(_this$subscribers$eve = this.subscribers[eventName]) === null ||
124
+ _this$subscribers$eve === void 0
125
+ ? void 0
126
+ : _this$subscribers$eve.forEach(subscriber => {
127
+ subscriber(data)
128
+ })
129
+ },
130
+ },
131
+ {
132
+ key: 'listeners',
133
+ get: function get() {
134
+ return Object.keys(this.subscribers).length !== 0
135
+ },
136
+ },
137
+ ],
138
+ [
139
+ {
140
+ key: 'valid',
141
+ value: function valid(name) {
142
+ return Event.exists(name)
143
+ },
144
+ },
145
+ {
146
+ key: 'invalid',
147
+ value: function invalid(name) {
148
+ return !this.valid(name)
149
+ },
150
+ },
151
+ {
152
+ key: 'exists',
153
+ value: function exists(name) {
154
+ return this.events.find(eventName => eventName === name) !== undefined
155
+ },
156
+ },
157
+ ],
158
+ )
159
+ return Event
160
+ })()
161
+ exports.default = Event
162
+ Event.events = ['session-set', 'forms:collected', 'form:completed']
@@ -1,49 +1,142 @@
1
- "use strict";
1
+ 'use strict'
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
8
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
9
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11
- 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); } }
12
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
13
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
14
- 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); }
15
- var EventEmitter = /*#__PURE__*/function () {
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true,
5
+ })
6
+ exports.default = void 0
7
+ var _event = _interopRequireDefault(require('./event'))
8
+ var _invalidEvent = require('./errors/invalidEvent')
9
+ function _interopRequireDefault(obj) {
10
+ return obj && obj.__esModule ? obj : { default: obj }
11
+ }
12
+ function ownKeys(object, enumerableOnly) {
13
+ var keys = Object.keys(object)
14
+ if (Object.getOwnPropertySymbols) {
15
+ var symbols = Object.getOwnPropertySymbols(object)
16
+ enumerableOnly &&
17
+ (symbols = symbols.filter(function (sym) {
18
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable
19
+ })),
20
+ keys.push.apply(keys, symbols)
21
+ }
22
+ return keys
23
+ }
24
+ function _objectSpread(target) {
25
+ for (var i = 1; i < arguments.length; i++) {
26
+ var source = null != arguments[i] ? arguments[i] : {}
27
+ i % 2
28
+ ? ownKeys(Object(source), !0).forEach(function (key) {
29
+ _defineProperty(target, key, source[key])
30
+ })
31
+ : Object.getOwnPropertyDescriptors
32
+ ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
33
+ : ownKeys(Object(source)).forEach(function (key) {
34
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key))
35
+ })
36
+ }
37
+ return target
38
+ }
39
+ function _defineProperty(obj, key, value) {
40
+ key = _toPropertyKey(key)
41
+ if (key in obj) {
42
+ Object.defineProperty(obj, key, {
43
+ value: value,
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ })
48
+ } else {
49
+ obj[key] = value
50
+ }
51
+ return obj
52
+ }
53
+ function _classCallCheck(instance, Constructor) {
54
+ if (!(instance instanceof Constructor)) {
55
+ throw new TypeError('Cannot call a class as a function')
56
+ }
57
+ }
58
+ function _defineProperties(target, props) {
59
+ for (var i = 0; i < props.length; i++) {
60
+ var descriptor = props[i]
61
+ descriptor.enumerable = descriptor.enumerable || false
62
+ descriptor.configurable = true
63
+ if ('value' in descriptor) descriptor.writable = true
64
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
65
+ }
66
+ }
67
+ function _createClass(Constructor, protoProps, staticProps) {
68
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps)
69
+ if (staticProps) _defineProperties(Constructor, staticProps)
70
+ Object.defineProperty(Constructor, 'prototype', { writable: false })
71
+ return Constructor
72
+ }
73
+ function _toPropertyKey(arg) {
74
+ var key = _toPrimitive(arg, 'string')
75
+ return typeof key === 'symbol' ? key : String(key)
76
+ }
77
+ function _toPrimitive(input, hint) {
78
+ if (typeof input !== 'object' || input === null) return input
79
+ var prim = input[Symbol.toPrimitive]
80
+ if (prim !== undefined) {
81
+ var res = prim.call(input, hint || 'default')
82
+ if (typeof res !== 'object') return res
83
+ throw new TypeError('@@toPrimitive must return a primitive value.')
84
+ }
85
+ return (hint === 'string' ? String : Number)(input)
86
+ }
87
+ var EventEmitter = /*#__PURE__*/ (function () {
16
88
  function EventEmitter() {
17
- _classCallCheck(this, EventEmitter);
18
- this.subscribers = {};
89
+ _classCallCheck(this, EventEmitter)
90
+ this.subscribers = {}
19
91
  }
20
- _createClass(EventEmitter, [{
21
- key: "addSubscriber",
22
- value: function addSubscriber(eventName, callback) {
23
- this.subscribers = _objectSpread(_objectSpread({}, this.subscribers), {}, {
24
- [eventName]: this.subscribers[eventName] ? [...this.subscribers[eventName], callback] : [callback]
25
- });
26
- }
27
- }, {
28
- key: "removeSubscriber",
29
- value: function removeSubscriber(eventName, callback) {
30
- if (this.subscribers[eventName]) {
31
- this.subscribers[eventName] = this.subscribers[eventName].filter(cb => cb !== callback);
32
- }
33
- }
34
- }, {
35
- key: "emit",
36
- value: function emit(eventName, data) {
37
- this.subscribers[eventName].forEach(subscriber => {
38
- subscriber(data);
39
- });
40
- }
41
- }, {
42
- key: "listeners",
43
- get: function get() {
44
- return Object.keys(this.subscribers).length !== 0;
45
- }
46
- }]);
47
- return EventEmitter;
48
- }();
49
- exports.default = EventEmitter;
92
+ _createClass(EventEmitter, [
93
+ {
94
+ key: 'addSubscriber',
95
+ value: function addSubscriber(eventName, callback) {
96
+ if (_event.default.invalid(eventName)) {
97
+ throw new _invalidEvent.InvalidEvent(eventName)
98
+ }
99
+ this.subscribers = _objectSpread(
100
+ _objectSpread({}, this.subscribers),
101
+ {},
102
+ {
103
+ [eventName]: this.subscribers[eventName]
104
+ ? [...this.subscribers[eventName], callback]
105
+ : [callback],
106
+ },
107
+ )
108
+ },
109
+ },
110
+ {
111
+ key: 'removeSubscriber',
112
+ value: function removeSubscriber(eventName, callback) {
113
+ if (_event.default.invalid(eventName)) {
114
+ throw new _invalidEvent.InvalidEvent(eventName)
115
+ }
116
+ if (this.subscribers[eventName]) {
117
+ this.subscribers[eventName] = this.subscribers[eventName].filter(cb => cb !== callback)
118
+ }
119
+ },
120
+ },
121
+ {
122
+ key: 'emit',
123
+ value: function emit(eventName, data) {
124
+ var _this$subscribers$eve
125
+ ;(_this$subscribers$eve = this.subscribers[eventName]) === null ||
126
+ _this$subscribers$eve === void 0
127
+ ? void 0
128
+ : _this$subscribers$eve.forEach(subscriber => {
129
+ subscriber(data)
130
+ })
131
+ },
132
+ },
133
+ {
134
+ key: 'listeners',
135
+ get: function get() {
136
+ return Object.keys(this.subscribers).length !== 0
137
+ },
138
+ },
139
+ ])
140
+ return EventEmitter
141
+ })()
142
+ exports.default = EventEmitter
package/lib/forms.js ADDED
@@ -0,0 +1,133 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true,
5
+ })
6
+ exports.Forms = void 0
7
+ var _hellotext = _interopRequireDefault(require('./hellotext.js'))
8
+ var _models = require('./models')
9
+ var _forms = _interopRequireDefault(require('./api/forms'))
10
+ function _interopRequireDefault(obj) {
11
+ return obj && obj.__esModule ? obj : { default: obj }
12
+ }
13
+ function _classCallCheck(instance, Constructor) {
14
+ if (!(instance instanceof Constructor)) {
15
+ throw new TypeError('Cannot call a class as a function')
16
+ }
17
+ }
18
+ function _defineProperties(target, props) {
19
+ for (var i = 0; i < props.length; i++) {
20
+ var descriptor = props[i]
21
+ descriptor.enumerable = descriptor.enumerable || false
22
+ descriptor.configurable = true
23
+ if ('value' in descriptor) descriptor.writable = true
24
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
25
+ }
26
+ }
27
+ function _createClass(Constructor, protoProps, staticProps) {
28
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps)
29
+ if (staticProps) _defineProperties(Constructor, staticProps)
30
+ Object.defineProperty(Constructor, 'prototype', { writable: false })
31
+ return Constructor
32
+ }
33
+ function _toPropertyKey(arg) {
34
+ var key = _toPrimitive(arg, 'string')
35
+ return typeof key === 'symbol' ? key : String(key)
36
+ }
37
+ function _toPrimitive(input, hint) {
38
+ if (typeof input !== 'object' || input === null) return input
39
+ var prim = input[Symbol.toPrimitive]
40
+ if (prim !== undefined) {
41
+ var res = prim.call(input, hint || 'default')
42
+ if (typeof res !== 'object') return res
43
+ throw new TypeError('@@toPrimitive must return a primitive value.')
44
+ }
45
+ return (hint === 'string' ? String : Number)(input)
46
+ }
47
+ function _classPrivateFieldLooseBase(receiver, privateKey) {
48
+ if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
49
+ throw new TypeError('attempted to use private field on non-instance')
50
+ }
51
+ return receiver
52
+ }
53
+ var id = 0
54
+ function _classPrivateFieldLooseKey(name) {
55
+ return '__private_' + id++ + '_' + name
56
+ }
57
+ var _formIdsToFetch = /*#__PURE__*/ _classPrivateFieldLooseKey('formIdsToFetch')
58
+ var Forms = /*#__PURE__*/ (function () {
59
+ function Forms() {
60
+ _classCallCheck(this, Forms)
61
+ Object.defineProperty(this, _formIdsToFetch, {
62
+ get: _get_formIdsToFetch,
63
+ set: void 0,
64
+ })
65
+ this.forms = []
66
+ this.includes = this.includes.bind(this)
67
+ this.excludes = this.excludes.bind(this)
68
+ }
69
+ _createClass(Forms, [
70
+ {
71
+ key: 'collect',
72
+ value: function collect() {
73
+ var formsIdsToFetch = _classPrivateFieldLooseBase(this, _formIdsToFetch)[_formIdsToFetch]
74
+ if (formsIdsToFetch.length === 0) return
75
+ var promises = formsIdsToFetch.map(id => {
76
+ return _forms.default.get(id).then(response => response.json())
77
+ })
78
+ if (!_hellotext.default.business.enabledWhitelist) {
79
+ console.warn(
80
+ 'No whitelist has been configured. It is advised to whitelist the domain to avoid bots from submitting forms.',
81
+ )
82
+ }
83
+ Promise.all(promises)
84
+ .then(forms => forms.forEach(form => this.add(form)))
85
+ .then(() => _hellotext.default.eventEmitter.emit('forms:collected', this))
86
+ },
87
+ },
88
+ {
89
+ key: 'add',
90
+ value: function add(data) {
91
+ if (this.includes(data.id)) return
92
+ this.forms.push(new _models.Form(data))
93
+ },
94
+ },
95
+ {
96
+ key: 'getById',
97
+ value: function getById(id) {
98
+ return this.forms.find(form => form.id === id)
99
+ },
100
+ },
101
+ {
102
+ key: 'getByIndex',
103
+ value: function getByIndex(index) {
104
+ return this.forms[index]
105
+ },
106
+ },
107
+ {
108
+ key: 'includes',
109
+ value: function includes(formId) {
110
+ return this.forms.some(form => form.id === formId)
111
+ },
112
+ },
113
+ {
114
+ key: 'excludes',
115
+ value: function excludes(id) {
116
+ return !this.includes(id)
117
+ },
118
+ },
119
+ {
120
+ key: 'length',
121
+ get: function get() {
122
+ return this.forms.length
123
+ },
124
+ },
125
+ ])
126
+ return Forms
127
+ })()
128
+ exports.Forms = Forms
129
+ function _get_formIdsToFetch() {
130
+ return Array.from(document.querySelectorAll('[data-hello-form]'))
131
+ .map(form => form.dataset.helloForm)
132
+ .filter(this.excludes)
133
+ }