@hellotext/hellotext 1.4.0 → 1.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.
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 +64 -102
  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 +7 -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,30 @@
1
+ import Hellotext from '../hellotext'
2
+ import { Configuration } from '../core'
3
+
4
+ import { Response } from './response'
5
+
6
+ class SubmissionsAPI {
7
+ static get endpoint() {
8
+ return Configuration.endpoint('public/submissions')
9
+ }
10
+
11
+ static async resendOTP(id) {
12
+ const response = await fetch(`${this.endpoint}/${id}/otps`, {
13
+ method: 'POST',
14
+ headers: Hellotext.headers,
15
+ })
16
+
17
+ return new Response(response.ok, response)
18
+ }
19
+
20
+ static async verifyOTP(id, otp) {
21
+ const response = await fetch(`${this.endpoint}/${id}/otps/${otp}/verify`, {
22
+ method: 'POST',
23
+ headers: Hellotext.headers,
24
+ })
25
+
26
+ return new Response(response.ok, response)
27
+ }
28
+ }
29
+
30
+ export default SubmissionsAPI
@@ -0,0 +1,55 @@
1
+ import Hellotext from '../hellotext'
2
+
3
+ class InputBuilder {
4
+ static build(data) {
5
+ const article = document.createElement('article')
6
+ const label = document.createElement('label')
7
+ const input = document.createElement('input')
8
+
9
+ label.innerText = data.label
10
+
11
+ input.type = data.type
12
+ input.required = data.required
13
+ input.placeholder = data.placeholder
14
+
15
+ if (['first_name', 'last_name'].includes(data.kind)) {
16
+ input.type = 'text'
17
+ input.id = input.name = data.kind
18
+ label.setAttribute('for', data.kind)
19
+ } else {
20
+ input.type = data.type
21
+
22
+ if (data.type === 'email') {
23
+ input.id = input.name = 'email'
24
+ label.setAttribute('for', 'email')
25
+ } else if (input.type === 'tel') {
26
+ input.id = input.name = 'phone'
27
+ label.setAttribute('for', 'phone')
28
+ input.value = `+${Hellotext.business.country.prefix}`
29
+ input.setAttribute('data-default-value', `+${Hellotext.business.country.prefix}`)
30
+ } else {
31
+ input.name = input.id = `property_by_id[${data.property}]`
32
+ label.setAttribute('for', `property_by_id[${data.property}]`)
33
+ }
34
+ }
35
+
36
+ const main = document.createElement('main')
37
+
38
+ main.appendChild(label)
39
+ main.appendChild(input)
40
+
41
+ article.appendChild(main)
42
+
43
+ article.setAttribute('data-hellotext--form-target', 'inputContainer')
44
+ input.setAttribute('data-hellotext--form-target', 'input')
45
+
46
+ const errorContainer = document.createElement('div')
47
+ errorContainer.setAttribute('data-error-container', '')
48
+
49
+ article.appendChild(errorContainer)
50
+
51
+ return article
52
+ }
53
+ }
54
+
55
+ export { InputBuilder }
@@ -0,0 +1,25 @@
1
+ import Hellotext from '../hellotext'
2
+
3
+ class LogoBuilder {
4
+ static build() {
5
+ const container = document.createElement('div')
6
+ container.innerHTML = this.#template()
7
+
8
+ return container.firstElementChild
9
+ }
10
+
11
+ static #template() {
12
+ return `
13
+ <div data-logo-container>
14
+ <small>${Hellotext.business.locale.white_label.powered_by}</small>
15
+
16
+ <svg data-hello-brand viewBox="0 0 224 43" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
17
+ <title>Hellotext</title>
18
+ <path d="M28.2760919,19.3224299 L28.2760919,31.1946615 C28.2760919,32.2088722 27.5005873,32.9844452 26.4864658,32.9844452 L21.0579334,32.9844452 C20.043812,32.9844452 19.2683073,32.2088722 19.2683073,31.1946615 L19.2683073,19.9786839 C19.2683073,16.7570733 17.4190271,14.7883113 14.4959712,14.7883113 C11.0360274,14.7883113 8.76916774,17.11503 8.76916774,21.1122135 L8.76916774,31.3139804 C8.76916774,32.3281911 7.99366311,33.1037641 6.97954167,33.1037641 L1.78962607,33.1037641 C0.77550463,33.1037641 0,32.3281911 0,31.3139804 L0,3.45301476 C0,2.43880402 0.77550463,1.6632311 1.78962607,1.6632311 L6.97954167,1.6632311 C7.99366311,1.6632311 8.76916774,2.43880402 8.76916774,3.45301476 L8.76916774,9.06100356 L8.11297151,16.3991166 L8.70951354,16.458776 C9.78328918,11.6860196 13.5415039,8.52406846 18.4331485,8.52406846 C24.3389145,8.52406846 28.2760919,12.8195492 28.2760919,19.3224299 Z M61.1455574,26.3622456 C58.9980061,30.7173858 53.6291279,33.5213803 47.0671656,33.5213803 C37.9997269,33.5213803 31.4377646,28.2116887 31.4377646,20.7542568 C31.4377646,13.6547816 37.2838764,8.52406846 46.0530442,8.52406846 C53.7484363,8.52406846 59.1769687,12.4615925 59.8928191,18.8451542 C60.0121275,19.9190244 59.1769687,20.8735757 58.103193,20.8735757 L40.1472781,20.8735757 C40.4455492,24.8110998 43.1299883,27.2571374 47.186474,27.2571374 C50.2288383,27.2571374 52.8536232,25.7656511 54.1660157,23.4389323 C54.5835951,22.6633594 55.5380623,22.3054026 56.3732212,22.6036999 L60.1314359,23.9758674 C61.0859032,24.2741647 61.622791,25.4076943 61.1455574,26.3622456 Z M40.5648576,17.3536679 L50.706072,17.2343489 C50.0498757,14.7286518 48.3795581,13.5354627 45.8740816,13.5354627 C43.3089509,13.5354627 41.4000164,14.9672896 40.5648576,17.3536679 Z M120.143563,20.9928946 C120.143563,28.2116887 113.641255,33.4617208 104.693125,33.4617208 C95.8046489,33.4617208 89.361995,28.2116887 89.361995,20.9928946 C89.361995,13.7741005 95.8046489,8.52406846 104.693125,8.52406846 C113.641255,8.52406846 120.143563,13.7741005 120.143563,20.9928946 Z M111.076125,21.1122135 C111.076125,17.2940084 108.45134,14.7883113 104.633471,14.7883113 C100.93491,14.7883113 98.3697796,17.2940084 98.3697796,20.9332352 C98.3697796,24.7514403 100.994564,27.2571374 104.812433,27.2571374 C108.570648,27.2571374 111.076125,24.7514403 111.076125,21.1122135 Z M141.559422,26.8395213 C141.261151,25.5866727 139.82945,25.0497376 138.815329,25.8253105 C137.443282,26.8395213 136.190544,27.2571374 134.99746,27.2571374 C132.909563,27.2571374 131.835787,26.0042889 131.835787,23.796889 L131.835787,13.9530789 L139.471525,13.9530789 C140.24703,13.9530789 140.903226,13.2968249 140.903226,12.521252 L140.903226,10.4928305 C140.903226,9.71725757 140.24703,9.06100356 139.471525,9.06100356 L131.835787,9.06100356 L131.835787,3.45301476 C131.835787,2.43880402 131.060282,1.6632311 130.046161,1.6632311 L125.214171,1.6632311 C124.200049,1.6632311 123.424545,2.43880402 123.424545,3.45301476 L123.424545,9.06100356 L123.424545,13.9530789 L123.424545,24.7514403 C123.424545,30.2401102 127.182759,33.5213803 133.565759,33.5213803 C136.727432,33.5213803 139.471525,32.6861479 141.499768,31.0753426 C142.036656,30.6577264 142.275272,29.9418129 142.09631,29.2855589 L141.559422,26.8395213 Z M173.414766,26.3622456 C171.267215,30.7173858 165.898337,33.5213803 159.336374,33.5213803 C150.268936,33.5213803 143.706973,28.2116887 143.706973,20.7542568 C143.706973,13.6547816 149.553085,8.52406846 158.322253,8.52406846 C166.017645,8.52406846 171.446177,12.4615925 172.162028,18.8451542 C172.281336,19.9190244 171.446177,20.8735757 170.372402,20.8735757 L152.416487,20.8735757 C152.714758,24.8110998 155.399197,27.2571374 159.455683,27.2571374 C162.498047,27.2571374 165.122832,25.7656511 166.435224,23.4389323 C166.852804,22.6633594 167.807271,22.3054026 168.64243,22.6036999 L172.400645,23.9758674 C173.414766,24.2741647 173.892,25.4076943 173.414766,26.3622456 Z M152.834066,17.3536679 L162.975281,17.2343489 C162.319084,14.7286518 160.648767,13.5354627 158.14329,13.5354627 C155.637814,13.5354627 153.669225,14.9672896 152.834066,17.3536679 Z M192.862036,20.8139163 L201.094316,12.0439763 C202.168092,10.9104467 201.332933,9.00134411 199.781923,9.00134411 L194.711316,9.00134411 C194.174428,9.00134411 193.637541,9.23998193 193.279615,9.71725757 L188.507279,15.8621815 L183.734943,9.71725757 C183.377018,9.29964138 182.899784,9.00134411 182.303242,9.00134411 L177.113327,9.00134411 C175.562317,9.00134411 174.727159,10.8507872 175.800934,12.0439763 L184.092868,20.8735757 L175.502663,30.0014724 C174.428888,31.135002 175.204392,33.0441046 176.815056,33.0441046 L181.945317,33.0441046 C182.482205,33.0441046 183.019093,32.8054668 183.377018,32.3878506 L188.447625,25.9446294 L193.577886,32.3878506 C193.935812,32.8054668 194.413045,33.0441046 194.949933,33.0441046 L200.139849,33.0441046 C201.690858,33.0441046 202.526017,31.1946615 201.452241,30.0611318 L192.862036,20.8139163 Z M223.941875,29.2258995 L223.345333,26.8395213 C223.047062,25.5866727 221.615362,25.0497376 220.60124,25.8253105 C219.229193,26.8395213 217.976455,27.2571374 216.783371,27.2571374 C214.695474,27.2571374 213.621698,26.0042889 213.621698,23.796889 L213.621698,13.9530789 L221.257436,13.9530789 C222.032941,13.9530789 222.689137,13.2968249 222.689137,12.521252 L222.689137,10.4928305 C222.689137,9.71725757 222.032941,9.06100356 221.257436,9.06100356 L213.621698,9.06100356 L213.621698,3.45301476 C213.621698,2.43880402 212.846194,1.6632311 211.832072,1.6632311 L207.000082,1.6632311 C205.985961,1.6632311 205.210456,2.43880402 205.210456,3.45301476 L205.210456,9.06100356 L205.210456,13.9530789 L205.210456,24.7514403 C205.210456,30.2401102 208.968671,33.5213803 215.35167,33.5213803 C218.513343,33.5213803 221.257436,32.6861479 223.285679,31.0753426 C223.882221,30.5980669 224.120838,29.8821535 223.941875,29.2258995 Z M77.6697714,6.01837134 C78.2663134,5.30245788 78.2663134,4.22858768 77.6101172,3.57233367 L74.5677529,0.529701449 C73.8519024,-0.186212015 72.6588184,-0.186212015 71.942968,0.589360904 C61.3841742,12.4615925 61.3841742,30.5384075 71.942968,42.4106391 C72.5991642,43.186212 73.8519024,43.186212 74.5677529,42.4702986 L77.6101172,39.4276663 C78.2663134,38.7714123 78.3259676,37.6975421 77.6697714,36.9816287 C69.9147251,28.1520293 69.9147251,14.8479707 77.6697714,6.01837134 Z M87.1547896,10.6718089 L84.1124252,7.62917663 C83.3965748,6.91326317 82.1438366,6.91326317 81.4876403,7.74849554 C78.2663134,11.6263601 76.4766873,16.458776 76.4766873,21.5298297 C76.4766873,26.6008834 78.2663134,31.4332993 81.4876403,35.3111639 C82.1438366,36.0867368 83.3965748,36.1463963 84.1124252,35.4304828 L87.1547896,32.3878506 C87.8109858,31.7315966 87.87064,30.7173858 87.3337522,30.0611318 C85.4248177,27.6747536 84.351042,24.6917809 84.351042,21.5894892 C84.351042,18.4871975 85.3651635,15.5042247 87.3337522,13.1178465 C87.87064,12.3422736 87.8109858,11.3280629 87.1547896,10.6718089 Z" id="hellotext" fill="currentColor"></path>
19
+ </svg>
20
+ </div>
21
+ `
22
+ }
23
+ }
24
+
25
+ export { LogoBuilder }
@@ -0,0 +1,42 @@
1
+ import Hellotext from '../hellotext'
2
+
3
+ class OTPBuilder {
4
+ static build(submissionId, label) {
5
+ const element = this.#template(submissionId, label)
6
+ const container = document.createElement('div')
7
+
8
+ container.innerHTML = element
9
+
10
+ return container
11
+ }
12
+
13
+ static #template(submissionId, label) {
14
+ return `
15
+ <article
16
+ data-controller="hellotext--otp"
17
+ data-hellotext--otp-submission-id-value="${submissionId}"
18
+ data-hellotext--form-target="otpContainer"
19
+ data-form-otp
20
+ >
21
+ <header data-otp-header>
22
+ <p>${label}</p>
23
+ <input
24
+ type="text"
25
+ name="otp"
26
+ data-hellotext--otp-target="input"
27
+ placeholder="Enter your OTP"
28
+ maxlength="6"
29
+ />
30
+ </header>
31
+
32
+ <footer data-otp-footer>
33
+ <button type="button" data-hellotext--otp-target="resendButton" data-action="hellotext--otp#resend">
34
+ ${Hellotext.business.locale.otp.resend}
35
+ </button>
36
+ </footer>
37
+ </article>
38
+ `
39
+ }
40
+ }
41
+
42
+ export { OTPBuilder }
@@ -0,0 +1,105 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+
3
+ import Hellotext from '../hellotext'
4
+ import { Form } from '../models'
5
+
6
+ import FormsAPI from '../api/forms'
7
+
8
+ import { OTPBuilder } from '../builders/otp_builder'
9
+
10
+ export default class extends Controller {
11
+ static values = {
12
+ data: Object,
13
+ step: { type: Number, default: 1 },
14
+ }
15
+
16
+ static targets = ['inputContainer', 'input', 'button', 'otpContainer']
17
+
18
+ initialize() {
19
+ this.form = new Form(this.dataValue, this.element)
20
+ }
21
+
22
+ connect() {
23
+ super.connect()
24
+ this.element.addEventListener('submit', this.submit.bind(this))
25
+
26
+ if (document.activeElement.tagName !== 'INPUT') {
27
+ this.inputTargets[0].focus()
28
+ }
29
+ }
30
+
31
+ async submit(e) {
32
+ e.preventDefault()
33
+
34
+ if (this.invalid) {
35
+ return this.showErrorMessages()
36
+ }
37
+
38
+ this.clearErrorMessages()
39
+
40
+ this.formData = Object.fromEntries(new FormData(this.element))
41
+ this.buttonTarget.disabled = true
42
+
43
+ const response = await FormsAPI.submit(this.form.id, this.formData)
44
+ this.buttonTarget.disabled = false
45
+
46
+ if(response.failed) {
47
+ return
48
+ }
49
+
50
+ this.buttonTarget.style.display = 'none'
51
+ this.element.querySelectorAll('input').forEach(input => (input.disabled = true))
52
+
53
+ const submission = await response.json()
54
+
55
+ if(submission.identified) {
56
+ this.completed()
57
+ } else {
58
+ Hellotext.setSession(submission.session)
59
+ this.revealOTPContainer(submission.id)
60
+ }
61
+ }
62
+
63
+ revealOTPContainer(submissionId) {
64
+ const paragraph = this.requiredInputs.find(input => input.name === 'email')
65
+ ? Hellotext.business.locale.otp.sent_to_email
66
+ : Hellotext.business.locale.otp.sent_to_phone
67
+
68
+ this.element.appendChild(OTPBuilder.build(submissionId, paragraph))
69
+ }
70
+
71
+ completed() {
72
+ this.form.markAsCompleted(this.formData)
73
+ this.element.remove()
74
+ }
75
+
76
+ // private
77
+
78
+ showErrorMessages() {
79
+ this.element.querySelectorAll('input:invalid').forEach(input => {
80
+ const parent = input.closest('article')
81
+ parent.querySelector('[data-error-container]').innerText = input.validationMessage
82
+ })
83
+ }
84
+
85
+ clearErrorMessages() {
86
+ this.element.querySelectorAll('input').forEach(input => {
87
+ const parent = input.closest('article')
88
+ parent.querySelector('[data-error-container]').innerText = ''
89
+ })
90
+ }
91
+
92
+ inputTargetConnected(target) {
93
+ if (target.getAttribute('data-default-value')) {
94
+ target.value = target.getAttribute('data-default-value')
95
+ }
96
+ }
97
+
98
+ get requiredInputs() {
99
+ return this.inputTargets.filter(input => input.required)
100
+ }
101
+
102
+ get invalid() {
103
+ return !this.element.checkValidity()
104
+ }
105
+ }
@@ -0,0 +1,77 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+
3
+ import Hellotext from '../hellotext'
4
+ import SubmissionsAPI from '../api/submissions'
5
+
6
+ export default class extends Controller {
7
+ static values = {
8
+ submissionId: String,
9
+ }
10
+
11
+ static targets = ['input', 'resendButton']
12
+
13
+ initialize() {
14
+ super.initialize()
15
+
16
+ this.attempts = 0
17
+ this.onInputChange = this.onInputChange.bind(this)
18
+
19
+ this.throttleInterval = setInterval(() => {
20
+ this.attempts = 0
21
+ }, 60000)
22
+ }
23
+
24
+ connect() {
25
+ super.connect()
26
+ this.inputTarget.addEventListener('input', this.onInputChange)
27
+ }
28
+
29
+ disconnect() {
30
+ clearInterval(this.throttleInterval)
31
+ this.inputTarget.removeEventListener('input', this.onInputChange)
32
+
33
+ super.disconnect()
34
+ }
35
+
36
+ async resend() {
37
+ if (this.throttled) {
38
+ return alert(Hellotext.business.locale.otp.throttled)
39
+ }
40
+
41
+ this.resendButtonTarget.disabled = true
42
+ const response = await SubmissionsAPI.resendOTP(this.submissionIdValue)
43
+
44
+ if (response.succeeded) {
45
+ this.resendButtonTarget.disabled = false
46
+ }
47
+
48
+ alert(Hellotext.business.locale.otp.resend_successful)
49
+ this.attempts += 1
50
+ }
51
+
52
+ async onInputChange() {
53
+ if (this.inputTarget.value.length !== 6) return
54
+
55
+ this.inputTarget.disabled = true
56
+ this.resendButtonTarget.disabled = true
57
+
58
+ const response = await SubmissionsAPI.verifyOTP(this.submissionIdValue, this.inputTarget.value)
59
+
60
+ if (response.succeeded) {
61
+ this.dispatch('verified', {
62
+ detail: { submissionId: this.submissionIdValue },
63
+ })
64
+ } else {
65
+ alert(Hellotext.business.locale.otp.invalid)
66
+ }
67
+
68
+ this.inputTarget.disabled = false
69
+ this.resendButtonTarget.disabled = false
70
+ }
71
+
72
+ // private
73
+
74
+ get throttled() {
75
+ return this.attempts >= 3
76
+ }
77
+ }
@@ -0,0 +1,16 @@
1
+ class Configuration {
2
+ static apiRoot = 'https://api.hellotext.com/v1'
3
+ static autoGenerateSession = true
4
+
5
+ static assign({ apiRoot, autoGenerateSession }) {
6
+ this.apiRoot = apiRoot || this.apiRoot
7
+ this.autoGenerateSession = autoGenerateSession
8
+ return this
9
+ }
10
+
11
+ static endpoint(path) {
12
+ return `${this.apiRoot}/${path}`
13
+ }
14
+ }
15
+
16
+ export { Configuration }
@@ -0,0 +1,54 @@
1
+ import { InvalidEvent } from '../errors'
2
+
3
+ export default class Event {
4
+ static events = ['session-set', 'forms:collected', 'form:completed']
5
+
6
+ static valid(name) {
7
+ return Event.exists(name)
8
+ }
9
+
10
+ static invalid(name) {
11
+ return !this.valid(name)
12
+ }
13
+
14
+ static exists(name) {
15
+ return this.events.find(eventName => eventName === name) !== undefined
16
+ }
17
+
18
+ constructor() {
19
+ this.subscribers = {}
20
+ }
21
+
22
+ addSubscriber(eventName, callback) {
23
+ if (Event.invalid(eventName)) {
24
+ throw new InvalidEvent(eventName)
25
+ }
26
+
27
+ this.subscribers = {
28
+ ...this.subscribers,
29
+ [eventName]: this.subscribers[eventName]
30
+ ? [...this.subscribers[eventName], callback]
31
+ : [callback],
32
+ }
33
+ }
34
+
35
+ removeSubscriber(eventName, callback) {
36
+ if (Event.invalid(eventName)) {
37
+ throw new InvalidEvent(eventName)
38
+ }
39
+
40
+ if (this.subscribers[eventName]) {
41
+ this.subscribers[eventName] = this.subscribers[eventName].filter(cb => cb !== callback)
42
+ }
43
+ }
44
+
45
+ dispatch(eventName, data) {
46
+ this.subscribers[eventName]?.forEach(subscriber => {
47
+ subscriber(data)
48
+ })
49
+ }
50
+
51
+ get listeners() {
52
+ return Object.keys(this.subscribers).length !== 0
53
+ }
54
+ }
@@ -0,0 +1,2 @@
1
+ export { default as Event } from './event'
2
+ export { Configuration } from './configuration'
@@ -0,0 +1,2 @@
1
+ export { InvalidEvent } from './invalid_event'
2
+ export { NotInitializedError } from './not_initialized_error'
package/src/hellotext.js CHANGED
@@ -1,53 +1,55 @@
1
- import Event from "./event"
2
- import EventEmitter from "./eventEmitter"
3
- import Response from "./response";
4
- import Query from "./query";
1
+ import { Event, Configuration } from './core'
5
2
 
6
- import { NotInitializedError } from './errors/notInitializedError'
7
- import { InvalidEvent } from "./errors/invalidEvent"
3
+ import API from './api'
4
+ import { Business, Query, Cookies, FormCollection } from './models'
5
+
6
+ import { NotInitializedError } from './errors'
8
7
 
9
8
  /**
10
9
  * @typedef {Object} Config
11
10
  * @property {Boolean} autogenerateSession
12
11
  */
13
12
 
14
-
15
13
  class Hellotext {
16
- static __apiURL = 'https://api.hellotext.com/v1/'
17
-
18
14
  static #session
19
- static #business
20
15
  static #config
21
- static #eventEmitter = new EventEmitter()
22
16
  static #query
23
17
 
18
+ static eventEmitter = new Event()
19
+ static forms
20
+ static business
21
+
24
22
  /**
25
23
  * initialize the module.
26
24
  * @param business public business id
27
25
  * @param { Config } config
28
26
  */
29
27
  static initialize(business, config = { autogenerateSession: true }) {
30
- this.#business = business
31
- this.#config = config
28
+ this.business = new Business(business)
29
+ this.forms = new FormCollection()
32
30
 
33
- this.#query = new Query(window.location.search)
31
+ this.#config = Configuration.assign(config)
32
+ this.#query = new Query()
34
33
 
35
- if(this.#query.has("preview")) return
34
+ addEventListener('load', () => {
35
+ this.forms.collect()
36
+ })
36
37
 
37
- const session = this.#query.get("session") || this.#cookie
38
+ if (this.#query.inPreviewMode) return
38
39
 
39
- if (session && session !== "undefined" && session !== "null") {
40
- this.#session = session
41
- this.#setSessionCookie()
42
- } else if(config.autogenerateSession) {
43
- this.#mintAnonymousSession()
44
- .then(response => {
45
- this.#session = response.id
46
- this.#setSessionCookie()
47
- })
40
+ if (this.#query.session) {
41
+ this.#session = Cookies.set('hello_session', this.#query.session)
42
+ } else if (config.autogenerateSession) {
43
+ this.#mintAnonymousSession().then(response => {
44
+ this.#session = Cookies.set('hello_session', response.id)
45
+ })
48
46
  }
49
47
  }
50
48
 
49
+ static setSession(value) {
50
+ this.#session = Cookies.set('hello_session', value)
51
+ }
52
+
51
53
  /**
52
54
  * Tracks an action that has happened on the page
53
55
  *
@@ -56,24 +58,19 @@ class Hellotext {
56
58
  * @returns {Promise<Response>}
57
59
  */
58
60
  static async track(action, params = {}) {
59
- if (this.#notInitialized) { throw new NotInitializedError() }
60
-
61
- if(this.#query.has("preview")) {
62
- return new Response(true, { received: true })
61
+ if (this.notInitialized) {
62
+ throw new NotInitializedError()
63
63
  }
64
64
 
65
- const response = await fetch(this.__apiURL + 'track/events', {
66
- headers: this.#headers,
67
- method: 'post',
68
- body: JSON.stringify({
65
+ return await API.events.create({
66
+ headers: this.headers,
67
+ body: {
69
68
  session: this.session,
70
69
  action,
71
70
  ...params,
72
- url: (params && params.url) || window.location.href
73
- }),
71
+ url: (params && params.url) || window.location.href,
72
+ },
74
73
  })
75
-
76
- return new Response(response.status === 200, await response.json())
77
74
  }
78
75
 
79
76
  /**
@@ -82,9 +79,7 @@ class Hellotext {
82
79
  * @param callback the callback. This method will be called with the payload
83
80
  */
84
81
  static on(event, callback) {
85
- if(Event.invalid(event)) { throw new InvalidEvent(event) }
86
-
87
- this.#eventEmitter.addSubscriber(event, callback)
82
+ this.eventEmitter.addSubscriber(event, callback)
88
83
  }
89
84
 
90
85
  /**
@@ -93,9 +88,7 @@ class Hellotext {
93
88
  * @param callback the callback to remove
94
89
  */
95
90
  static removeEventListener(event, callback) {
96
- if(Event.invalid(event)) { throw new InvalidEvent(event) }
97
-
98
- this.#eventEmitter.removeSubscriber(event, callback)
91
+ this.eventEmitter.removeSubscriber(event, callback)
99
92
  }
100
93
 
101
94
  /**
@@ -103,7 +96,9 @@ class Hellotext {
103
96
  * @returns {String}
104
97
  */
105
98
  static get session() {
106
- if (this.#notInitialized) { throw new NotInitializedError() }
99
+ if (this.notInitialized) {
100
+ throw new NotInitializedError()
101
+ }
107
102
 
108
103
  return this.#session
109
104
  }
@@ -118,46 +113,29 @@ class Hellotext {
118
113
 
119
114
  // private
120
115
 
121
- static get #notInitialized() {
122
- return this.#business === undefined
116
+ static get notInitialized() {
117
+ return this.business.id === undefined
123
118
  }
124
119
 
125
120
  static async #mintAnonymousSession() {
126
- if (this.#notInitialized) { throw new NotInitializedError() }
127
-
128
- const trackingUrl = this.__apiURL + 'track/sessions'
129
-
130
- this.mintingPromise = await fetch(trackingUrl, {
131
- method: 'post',
132
- headers: { Authorization: `Bearer ${this.#business}` },
133
- })
121
+ if (this.notInitialized) {
122
+ throw new NotInitializedError()
123
+ }
134
124
 
135
- return this.mintingPromise.json()
125
+ return API.sessions(this.business.id).create()
136
126
  }
137
127
 
138
- static get #headers() {
139
- if (this.#notInitialized) { throw new NotInitializedError() }
128
+ static get headers() {
129
+ if (this.notInitialized) {
130
+ throw new NotInitializedError()
131
+ }
140
132
 
141
133
  return {
142
- Authorization: `Bearer ${this.#business}`,
134
+ Authorization: `Bearer ${this.business.id}`,
143
135
  Accept: 'application.json',
144
136
  'Content-Type': 'application/json',
145
137
  }
146
138
  }
147
-
148
- static #setSessionCookie() {
149
- if (this.#notInitialized) { throw new NotInitializedError() }
150
-
151
- if(this.#eventEmitter.listeners) {
152
- this.#eventEmitter.emit("session-set", this.#session)
153
- }
154
-
155
- document.cookie = `hello_session=${this.#session}`
156
- }
157
-
158
- static get #cookie() {
159
- return document.cookie.match('(^|;)\\s*' + 'hello_session' + '\\s*=\\s*([^;]+)')?.pop()
160
- }
161
139
  }
162
140
 
163
141
  export default Hellotext
package/src/index.js CHANGED
@@ -1,3 +1,13 @@
1
+ import { Application } from '@hotwired/stimulus'
1
2
  import Hellotext from './hellotext'
2
3
 
4
+ import FormController from './controllers/form_controller'
5
+ import OTPController from './controllers/otp_controller'
6
+
7
+ const application = Application.start()
8
+ application.register('hellotext--form', FormController)
9
+ application.register('hellotext--otp', OTPController)
10
+
11
+ import '../styles/index.css'
12
+
3
13
  export default Hellotext
@@ -0,0 +1,13 @@
1
+ export default {
2
+ otp: {
3
+ sent_to_email: 'A One-Time Password has been sent to your email address',
4
+ sent_to_phone: 'An One-Time Password has been sent to your phone number',
5
+ resend_successful: 'The One-Time Password has been resent successfully',
6
+ invalid: 'Invalid One-Time Password',
7
+ resend: 'Resend OTP',
8
+ throttled: 'You have reached the maximum number of attempts. Please try again in 1 minute.',
9
+ },
10
+ white_label: {
11
+ powered_by: "Powered by",
12
+ }
13
+ }
@@ -0,0 +1,13 @@
1
+ export default {
2
+ otp: {
3
+ sent_to_email: 'Un código de un solo uso ha sido enviado a tu correo electrónico',
4
+ sent_to_phone: 'Un código de un solo uso ha sido enviado a tu número de teléfono',
5
+ resend_successful: 'El código de un solo uso ha sido reenviado exitosamente',
6
+ invalid: 'Código de un solo uso inválido',
7
+ resend: 'Reenviar OTP',
8
+ throttled: 'Has alcanzado el número máximo de intentos. Por favor intenta de nuevo en 1 minuto.',
9
+ },
10
+ white_label: {
11
+ powered_by: "Desarrollado por",
12
+ }
13
+ }
@@ -0,0 +1,7 @@
1
+ import en from './en'
2
+ import es from './es'
3
+
4
+ export default {
5
+ en,
6
+ es,
7
+ }