@viibe/impulse-integration-js 2.3.2 → 2.3.4-alpha3

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.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- (()=>{var t={579:t=>{function e(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}var n=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.handlers=[],this.host=e.host,this.origin=/^localhost/.test(this.host)?"http://".concat(this.host):"https://".concat(this.host),this.source=e.source,window.addEventListener("message",this.onMessage.bind(this))}var n,o;return n=t,o=[{key:"createCallIframe",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(!this.host)throw new Error("fillHost");if(!this.source)throw new Error("fillSource");var e=this.createCallUrl(t),n=document.createElement("iframe");return n.name="viibe-"+Math.floor(1e4*Math.random()),n.style="border: 0;width: calc(100% - 1rem);margin: 0.5rem;",n.src=e,n.allow="geolocation;autoplay;microphone;camera;midi;encrypted-media;clipboard-write;",n}},{key:"createCallUrl",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.clipboard,n=void 0!==e&&e,o=t.components,a=t.links,i=t.o2m,r=void 0!==i&&i,s=t.ticket,c=t.token,l=t.locale,u="".concat(this.origin,"/integration/features/call"),d=new URLSearchParams({resize:"true",source:this.source});if(null!=s&&s.title&&d.set("title",s.title),null!=s&&s.integration&&d.set("thirdPartyId",s.integration.id),a){var h=a instanceof Array?a.map((function(t){return"".concat(t.category,":").concat(t.value)})).join(","):String(a);d.set("links",h)}if(c&&d.set("token",c),l&&d.set("locale",l),o){var f=o instanceof Array?o.join(","):String(o);d.set("components",f)}return d.set("clipboard",Boolean(n)),d.set("o2m",Boolean(r)),u+"?".concat(d)}},{key:"isViibeEvent",value:function(t){return Boolean(t.isTrusted&&0===t.origin.indexOf(this.origin))}},{key:"dispatch",value:function(t,e,n){return this.handlers.filter((function(e){return e.eventName===t&&(!e.windowName||e.windowName===n)})).map((function(t){return t.func(e)}))}},{key:"onMessage",value:function(t){return this.isViibeEvent(t)?t.data&&t.data.event&&t.data.payload?this.dispatch(t.data.event,t.data.payload,t.data.windowName):t.data&&t.data.token?this.dispatch("authentication:token",t.data.token):t.data&&t.data.error?this.dispatch("authentication:error",t.data.error):null:null}},{key:"on",value:function(t,e,n){"string"==typeof t&&"function"==typeof e&&void 0===n&&(n=e,e=t,t=null);var o={eventName:e,windowName:t?t.name:null,func:n};this.handlers.push(o)}},{key:"authenticate",value:function(t){var e=this,n="".concat(this.origin,"/api/auth/login/").concat(t),o=window.screen.width/2-240,a=window.screen.height/2-300,i=["toolbar=no","location=no","directories=no","status=no","menubar=no","scrollbars=no","resizable=no","copyhistory=no","width=".concat(480),"height=".concat(600),"top=".concat(a),"left=".concat(o)];return new Promise((function(t,o){var a=window.open(n,"Authorization",i.join(", "));a||o("Failed to open window");var r=setInterval((function(){a.closed&&(c(),s())}),100),s=function(){o("User closed window")},c=function(){window.removeEventListener("message",l),clearInterval(r)},l=function(n){if(!e.isViibeEvent(n))return null;n.data.token&&(c(),t(n.data.token)),n.data.error&&(c(),o(n.data.error))};window.addEventListener("message",l)}))}}],o&&e(n.prototype,o),t}();"undefined"!=typeof window&&(window.Viibe=n),t.exports=n}},e={},n=function n(o){var a=e[o];if(void 0!==a)return a.exports;var i=e[o]={exports:{}};return t[o](i,i.exports,n),i.exports}(579);module.exports=n})();
1
+ (()=>{var t={579:t=>{function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}function o(t,e,o){return e&&n(t.prototype,e),o&&n(t,o),t}function i(t){return function(e){return Boolean(e.isTrusted&&0===e.origin.indexOf(t))}}function a(t){var e=new URL(t),n="".concat(e.protocol,"://").concat(e.host);console.log("opeing popup",e,"origin is",n);var o=window.screen.width/2-240,a=window.screen.height/2-300,r=["toolbar=no","location=no","directories=no","status=no","menubar=no","scrollbars=no","resizable=no","copyhistory=no","width=".concat(480),"height=".concat(600),"top=".concat(a),"left=".concat(o)];return new Promise((function(e,o){var a=window.open(t,"Authorization",r.join(", "));a||o("Failed to open window");var s=setInterval((function(){a.closed&&(l(),c())}),100),c=function(){o("User closed window")},l=function(){window.removeEventListener("message",u),clearInterval(s)},u=function(t){if(console.log("got message",t),!i(n)(t))return null;t.data.token&&(l(),e(t.data.token)),t.data.error&&(l(),o(t.data.error))};window.addEventListener("message",u)}))}var r,s,c,l=function(){function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"auth.viibe.tech";e(this,t),this.host=n}return o(t,[{key:"loginZendesk",value:function(t){return a("https://".concat(this.host,"/auth/zendesk/login?subdomain=").concat(t))}}]),t}(),u=function(){function t(n){e(this,t),this.handlers=[],this.host=n.host,this.origin=/^localhost/.test(this.host)?"http://".concat(this.host):"https://".concat(this.host),this.source=n.source,this.isViibeEvent=i(this.origin),window.addEventListener("message",this.onMessage.bind(this))}return o(t,[{key:"createNotificationIframe",value:function(){if(!this.host)throw new Error("fillHost");if(!this.source)throw new Error("fillSource");var t=document.createElement("iframe");return t.name="viibe-"+Math.floor(1e4*Math.random()),t.style="border: 0;width: 0;",t.src="".concat(this.origin,"/integration/features/notifications"),t}},{key:"createCallIframe",value:function(){var t=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(!this.host)throw new Error("fillHost");if(!this.source)throw new Error("fillSource");var n=this.createCallUrl(e),o=document.createElement("iframe");return o.name="viibe-"+Math.floor(1e4*Math.random()),o.style="border: 0;width: calc(100% - 1rem);margin: 0.5rem;",o.src=n,o.allow="geolocation;autoplay;microphone;camera;midi;encrypted-media;clipboard-write;",this.on(o,"document",(function(e){return t.onDocument(e)})),o}},{key:"createCallUrl",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.clipboard,n=void 0!==e&&e,o=t.components,i=t.links,a=t.o2m,r=void 0!==a&&a,s=t.ticket,c=t.token,l=t.locale,u=t.standalone,d="".concat(this.origin,"/integration/features/call"),h=new URLSearchParams({resize:"true",source:this.source});if(s&&s.title&&h.set("title",s.title),s&&s.integration&&h.set("thirdPartyId",s.integration.id),i){var f=i instanceof Array?i.map((function(t){return"".concat(t.category,":").concat(t.value)})).join(","):String(i);h.set("links",f)}if(c&&h.set("token",c),l&&h.set("locale",l),o){var v=o instanceof Array?o.join(","):String(o);h.set("components",v)}return u&&h.set("standalone",Boolean(u)),h.set("clipboard",Boolean(n)),h.set("o2m",Boolean(r)),d+"?".concat(h)}},{key:"dispatch",value:function(t,e,n){return this.handlers.filter((function(e){return e.eventName===t&&(!e.windowName||e.windowName===n)})).map((function(t){return t.func(e)}))}},{key:"onMessage",value:function(t){return this.isViibeEvent(t)?t.data&&t.data.event&&t.data.payload?this.dispatch(t.data.event,t.data.payload,t.data.windowName):t.data&&t.data.token?this.dispatch("authentication:token",t.data.token):t.data&&t.data.error?this.dispatch("authentication:error",t.data.error):null:null}},{key:"on",value:function(t,e,n){"string"==typeof t&&"function"==typeof e&&void 0===n&&(n=e,e=t,t=null);var o={eventName:e,windowName:t?t.name:null,func:n};this.handlers.push(o)}},{key:"onDocument",value:function(t){window.open(t.signed_url,"_blank")}},{key:"authenticate",value:function(t){return a("".concat(this.origin,"/api/auth/login/").concat(t))}}]),t}();c=l,(s="Auth")in(r=u)?Object.defineProperty(r,s,{value:c,enumerable:!0,configurable:!0,writable:!0}):r.Auth=c,"undefined"!=typeof window&&(window.Viibe=u),t.exports=u}},e={},n=function n(o){var i=e[o];if(void 0!==i)return i.exports;var a=e[o]={exports:{}};return t[o](a,a.exports,n),a.exports}(579);module.exports=n})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viibe/impulse-integration-js",
3
- "version": "2.3.2",
3
+ "version": "2.3.4-alpha3",
4
4
  "description": "ViiBE integration in js native",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
package/src/index.js CHANGED
@@ -1,5 +1,101 @@
1
+ function isFromOrigin(origin) {
2
+ return function (event) {
3
+ return Boolean(event.isTrusted && event.origin.indexOf(origin) === 0)
4
+ }
5
+ }
6
+
7
+ function openAuthPopup(src) {
8
+ const url = new URL(src);
9
+ const origin = `${url.protocol}://${url.host}`;
10
+
11
+ console.log('opeing popup', url, 'origin is', origin);
12
+
13
+ const width = 480
14
+ const height = 600
15
+ const TWO = 2
16
+
17
+ const left = (window.screen.width / TWO) - (width / TWO)
18
+ const top = (window.screen.height / TWO) - (height / TWO)
19
+ const windowParams = [
20
+ 'toolbar=no',
21
+ 'location=no',
22
+ 'directories=no',
23
+ 'status=no',
24
+ 'menubar=no',
25
+ 'scrollbars=no',
26
+ 'resizable=no',
27
+ 'copyhistory=no',
28
+ `width=${width}`,
29
+ `height=${height}`,
30
+ `top=${top}`,
31
+ `left=${left}`,
32
+ ]
33
+
34
+ return new Promise((resolve, reject) => {
35
+ const win = window.open(src, 'Authorization', windowParams.join(', '))
36
+
37
+ if (!win) {
38
+ reject('Failed to open window')
39
+ }
40
+
41
+ // pings the login window to check if it was closed
42
+ const closeCheck = setInterval(() => {
43
+ if (win.closed) {
44
+ destroy();
45
+ onClose();
46
+ }
47
+
48
+ }, 100)
49
+
50
+ // triggered if the window was closed
51
+ const onClose = () => {
52
+ reject('User closed window')
53
+ }
54
+
55
+ // completes the process, either because the user closes or because the authentication completed
56
+ // cleans intervals and event listeners
57
+ const destroy = () => {
58
+ window.removeEventListener('message', onMessage)
59
+ clearInterval(closeCheck)
60
+ }
61
+
62
+ // triggered when a message is received on the current window
63
+ const onMessage = event => {
64
+ console.log('got message', event);
65
+ if (!isFromOrigin(origin)(event)) {
66
+ return null
67
+ }
68
+ if (event.data.token) {
69
+ destroy();
70
+ resolve(event.data.token)
71
+ }
72
+ if (event.data.error) {
73
+ destroy();
74
+ reject(event.data.error)
75
+ }
76
+ }
77
+
78
+ window.addEventListener('message', onMessage)
79
+ })
80
+ }
81
+
82
+ class ViibeAuth {
83
+
84
+ // FIXME
85
+ constructor(host = 'auth.viibe.tech') {
86
+ this.host = host;
87
+ }
88
+
89
+ loginZendesk(subdomain) {
90
+ const url = `https://${this.host}/auth/zendesk/login?subdomain=${subdomain}`;
91
+ return openAuthPopup(url);
92
+ }
93
+ }
94
+
1
95
  class Viibe {
2
96
 
97
+ static Auth = ViibeAuth;
98
+
3
99
  /**
4
100
  * @typedef Handler
5
101
  * @type {object}
@@ -17,9 +113,22 @@ class Viibe {
17
113
  this.origin = /^localhost/.test(this.host) ? `http://${this.host}` : `https://${this.host}`
18
114
  this.source = options.source
19
115
 
116
+ this.isViibeEvent = isFromOrigin(this.origin);
117
+
20
118
  window.addEventListener('message', this.onMessage.bind(this))
21
119
  }
22
120
 
121
+ createNotificationIframe() {
122
+ if (!this.host) throw new Error('fillHost')
123
+ if (!this.source) throw new Error('fillSource')
124
+
125
+ const iframe = document.createElement('iframe')
126
+ iframe.name = 'viibe-' + Math.floor(Math.random() * 10000)
127
+ iframe.style = 'border: 0;width: 0;'
128
+ iframe.src = `${this.origin}/integration/features/notifications`
129
+ return iframe
130
+ }
131
+
23
132
  createCallIframe(options = {}) {
24
133
  if (!this.host) throw new Error('fillHost')
25
134
  if (!this.source) throw new Error('fillSource')
@@ -32,6 +141,8 @@ class Viibe {
32
141
  iframe.src = src
33
142
  iframe.allow = 'geolocation;autoplay;microphone;camera;midi;encrypted-media;clipboard-write;'
34
143
 
144
+ this.on(iframe, 'document', event => this.onDocument(event))
145
+
35
146
  return iframe
36
147
  }
37
148
 
@@ -43,7 +154,8 @@ class Viibe {
43
154
  o2m = false,
44
155
  ticket,
45
156
  token,
46
- locale
157
+ locale,
158
+ standalone,
47
159
  } = options
48
160
 
49
161
  let src = `${this.origin}/integration/features/call`
@@ -76,6 +188,9 @@ class Viibe {
76
188
  params.set('components', componentsParams)
77
189
  }
78
190
 
191
+ if (standalone) params.set('standalone', Boolean(standalone))
192
+
193
+
79
194
  params.set('clipboard', Boolean(clipboard))
80
195
  params.set('o2m', Boolean(o2m))
81
196
 
@@ -84,23 +199,19 @@ class Viibe {
84
199
  return src
85
200
  }
86
201
 
87
- isViibeEvent(event) {
88
- return Boolean(event.isTrusted && event.origin.indexOf(this.origin) === 0)
89
- }
90
-
91
202
  /**
92
203
  * Dispatches an event ie calls handlers corresponding to that event.
93
204
  * Handlers with not windowName will be triggered regardless of the origin windowName
94
205
  * Otherwise, the event will be triggered only if the handler windowName and the event windowName correspond
95
- *
96
- * @param {string} eventName
97
- * @param {any} payload
98
- * @param {string} windowName
99
- * @returns
206
+ *
207
+ * @param {string} eventName
208
+ * @param {any} payload
209
+ * @param {string} windowName
210
+ * @returns
100
211
  */
101
212
  dispatch(eventName, payload, windowName) {
102
213
  return this.handlers
103
- .filter(handler =>
214
+ .filter(handler =>
104
215
  handler.eventName === eventName
105
216
  && (!handler.windowName || (handler.windowName === windowName))
106
217
  )
@@ -109,8 +220,8 @@ class Viibe {
109
220
 
110
221
  /**
111
222
  * Handles postMessages / events received on the current window
112
- * @param {any} event
113
- * @returns
223
+ * @param {any} event
224
+ * @returns
114
225
  */
115
226
  onMessage(event) {
116
227
  if (!this.isViibeEvent(event)) {
@@ -134,16 +245,16 @@ class Viibe {
134
245
 
135
246
  /**
136
247
  * Registers an event handler.
137
- * Can be either called with 2 arguments (eventName and handler) to catch events from all iframes
248
+ * Can be either called with 2 arguments (eventName and handler) to catch events from all iframes
138
249
  * or with 3 arguments (iframe, eventName, handler) to catch events from only that iframe
139
- * @param {HTMLIFrameElement} [iframe]
140
- * @param {string} eventName
141
- * @param {Function} handler
250
+ * @param {HTMLIFrameElement} [iframe]
251
+ * @param {string} eventName
252
+ * @param {Function} handler
142
253
  */
143
254
  on(iframe, eventName, func) {
144
- if (typeof iframe === 'string'
145
- && typeof eventName === 'function'
146
- && typeof func === 'undefined') {
255
+ if (typeof iframe === 'string'
256
+ && typeof eventName === 'function'
257
+ && typeof func === 'undefined') {
147
258
  func = eventName;
148
259
  eventName = iframe;
149
260
  iframe = null;
@@ -157,80 +268,19 @@ class Viibe {
157
268
  this.handlers.push(handler);
158
269
  }
159
270
 
271
+ onDocument(document) {
272
+ window.open(document.signed_url, '_blank')
273
+ }
274
+
160
275
  /**
161
276
  * Initiates an oauth login in a popup using the passed provider
162
277
  * @param {string} provider A strategy name from the Viibe API (see /strategies API)
163
- * @returns
278
+ * @returns
164
279
  */
165
280
  authenticate(provider) {
166
281
  const src = `${this.origin}/api/auth/login/${provider}`
167
- const width = 480
168
- const height = 600
169
- const TWO = 2
170
-
171
- const left = (window.screen.width / TWO) - (width / TWO)
172
- const top = (window.screen.height / TWO) - (height / TWO)
173
- const windowParams = [
174
- 'toolbar=no',
175
- 'location=no',
176
- 'directories=no',
177
- 'status=no',
178
- 'menubar=no',
179
- 'scrollbars=no',
180
- 'resizable=no',
181
- 'copyhistory=no',
182
- `width=${width}`,
183
- `height=${height}`,
184
- `top=${top}`,
185
- `left=${left}`,
186
- ]
187
-
188
- return new Promise((resolve, reject) => {
189
- const win = window.open(src, 'Authorization', windowParams.join(', '))
190
-
191
- if (!win) {
192
- reject('Failed to open window')
193
- }
194
-
195
- // pings the login window to check if it was closed
196
- const closeCheck = setInterval(() => {
197
- if (win.closed) {
198
- destroy();
199
- onClose();
200
- }
201
-
202
- }, 100)
203
-
204
- // triggered if the window was closed
205
- const onClose = () => {
206
- reject('User closed window')
207
- }
208
-
209
- // completes the process, either because the user closes or because the authentication completed
210
- // cleans intervals and event listeners
211
- const destroy = () => {
212
- window.removeEventListener('message', onMessage)
213
- clearInterval(closeCheck)
214
- }
215
-
216
- // triggered when a message is received on the current window
217
- const onMessage = event => {
218
- if (!this.isViibeEvent(event)) {
219
- return null
220
- }
221
- if (event.data.token) {
222
- destroy();
223
- resolve(event.data.token)
224
- }
225
- if (event.data.error) {
226
- destroy();
227
- reject(event.data.error)
228
- }
229
- }
230
-
231
- window.addEventListener('message', onMessage)
232
- })
233
282
 
283
+ return openAuthPopup(src);
234
284
  }
235
285
 
236
286
  }
@@ -240,4 +290,4 @@ if (typeof window !== 'undefined') {
240
290
  }
241
291
  if (typeof module !== 'undefined') {
242
292
  module.exports = Viibe
243
- }
293
+ }