@viibe/impulse-integration-js 2.3.4-alpha1 → 2.3.4-alpha4
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 +1 -1
- package/package.json +1 -1
- package/src/index.js +75 -65
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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){var e=
|
|
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
package/src/index.js
CHANGED
|
@@ -1,76 +1,88 @@
|
|
|
1
|
+
function isFromOrigin(origin) {
|
|
2
|
+
return function (event) {
|
|
3
|
+
return Boolean(event.isTrusted && event.origin.indexOf(origin) === 0)
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
1
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
|
+
|
|
2
13
|
const width = 480
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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();
|
|
28
46
|
}
|
|
29
47
|
|
|
30
|
-
|
|
31
|
-
const closeCheck = setInterval(() => {
|
|
32
|
-
if (win.closed) {
|
|
33
|
-
destroy();
|
|
34
|
-
onClose();
|
|
35
|
-
}
|
|
48
|
+
}, 100)
|
|
36
49
|
|
|
37
|
-
|
|
50
|
+
// triggered if the window was closed
|
|
51
|
+
const onClose = () => {
|
|
52
|
+
reject('User closed window')
|
|
53
|
+
}
|
|
38
54
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
+
}
|
|
43
61
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
|
49
67
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (!this.isViibeEvent(event)) {
|
|
54
|
-
return null
|
|
55
|
-
}
|
|
56
|
-
if (event.data.token) {
|
|
57
|
-
destroy();
|
|
58
|
-
resolve(event.data.token)
|
|
59
|
-
}
|
|
60
|
-
if (event.data.error) {
|
|
61
|
-
destroy();
|
|
62
|
-
reject(event.data.error)
|
|
63
|
-
}
|
|
68
|
+
if (event.data.token) {
|
|
69
|
+
destroy();
|
|
70
|
+
resolve(event.data.token)
|
|
64
71
|
}
|
|
72
|
+
if (event.data.error) {
|
|
73
|
+
destroy();
|
|
74
|
+
reject(event.data.error)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
65
77
|
|
|
66
|
-
|
|
67
|
-
|
|
78
|
+
window.addEventListener('message', onMessage)
|
|
79
|
+
})
|
|
68
80
|
}
|
|
69
81
|
|
|
70
82
|
class ViibeAuth {
|
|
71
83
|
|
|
72
84
|
// FIXME
|
|
73
|
-
constructor(host = '
|
|
85
|
+
constructor(host = 'auth.viibe.tech') {
|
|
74
86
|
this.host = host;
|
|
75
87
|
}
|
|
76
88
|
|
|
@@ -101,6 +113,8 @@ class Viibe {
|
|
|
101
113
|
this.origin = /^localhost/.test(this.host) ? `http://${this.host}` : `https://${this.host}`
|
|
102
114
|
this.source = options.source
|
|
103
115
|
|
|
116
|
+
this.isViibeEvent = isFromOrigin(this.origin);
|
|
117
|
+
|
|
104
118
|
window.addEventListener('message', this.onMessage.bind(this))
|
|
105
119
|
}
|
|
106
120
|
|
|
@@ -185,10 +199,6 @@ class Viibe {
|
|
|
185
199
|
return src
|
|
186
200
|
}
|
|
187
201
|
|
|
188
|
-
isViibeEvent(event) {
|
|
189
|
-
return Boolean(event.isTrusted && event.origin.indexOf(this.origin) === 0)
|
|
190
|
-
}
|
|
191
|
-
|
|
192
202
|
/**
|
|
193
203
|
* Dispatches an event ie calls handlers corresponding to that event.
|
|
194
204
|
* Handlers with not windowName will be triggered regardless of the origin windowName
|
|
@@ -243,8 +253,8 @@ class Viibe {
|
|
|
243
253
|
*/
|
|
244
254
|
on(iframe, eventName, func) {
|
|
245
255
|
if (typeof iframe === 'string'
|
|
246
|
-
|
|
247
|
-
|
|
256
|
+
&& typeof eventName === 'function'
|
|
257
|
+
&& typeof func === 'undefined') {
|
|
248
258
|
func = eventName;
|
|
249
259
|
eventName = iframe;
|
|
250
260
|
iframe = null;
|
|
@@ -269,7 +279,7 @@ class Viibe {
|
|
|
269
279
|
*/
|
|
270
280
|
authenticate(provider) {
|
|
271
281
|
const src = `${this.origin}/api/auth/login/${provider}`
|
|
272
|
-
|
|
282
|
+
|
|
273
283
|
return openAuthPopup(src);
|
|
274
284
|
}
|
|
275
285
|
|