@viibe/impulse-integration-js 2.3.2 → 2.3.4-alpha1
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 +124 -84
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(){
|
|
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=this,n=window.screen.width/2-240,o=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(o),"left=".concat(n)];return new Promise((function(n,o){var a=window.open(t,"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(t){if(!e.isViibeEvent(t))return null;t.data.token&&(c(),n(t.data.token)),t.data.error&&(c(),o(t.data.error))};window.addEventListener("message",l)}))}var a,r,s,c=function(){function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"https://auth.viibe.tech";e(this,t),this.host=n}return o(t,[{key:"loginZendesk",value:function(t){return i("https://".concat(this.host,"/auth/zendesk/login?subdomain=").concat(t))}}]),t}(),l=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,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:"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:"onDocument",value:function(t){window.open(t.signed_url,"_blank")}},{key:"authenticate",value:function(t){return i("".concat(this.origin,"/api/auth/login/").concat(t))}}]),t}();s=c,(r="Auth")in(a=l)?Object.defineProperty(a,r,{value:s,enumerable:!0,configurable:!0,writable:!0}):a.Auth=s,"undefined"!=typeof window&&(window.Viibe=l),t.exports=l}},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,5 +1,89 @@
|
|
|
1
|
+
function openAuthPopup(src) {
|
|
2
|
+
const width = 480
|
|
3
|
+
const height = 600
|
|
4
|
+
const TWO = 2
|
|
5
|
+
|
|
6
|
+
const left = (window.screen.width / TWO) - (width / TWO)
|
|
7
|
+
const top = (window.screen.height / TWO) - (height / TWO)
|
|
8
|
+
const windowParams = [
|
|
9
|
+
'toolbar=no',
|
|
10
|
+
'location=no',
|
|
11
|
+
'directories=no',
|
|
12
|
+
'status=no',
|
|
13
|
+
'menubar=no',
|
|
14
|
+
'scrollbars=no',
|
|
15
|
+
'resizable=no',
|
|
16
|
+
'copyhistory=no',
|
|
17
|
+
`width=${width}`,
|
|
18
|
+
`height=${height}`,
|
|
19
|
+
`top=${top}`,
|
|
20
|
+
`left=${left}`,
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const win = window.open(src, 'Authorization', windowParams.join(', '))
|
|
25
|
+
|
|
26
|
+
if (!win) {
|
|
27
|
+
reject('Failed to open window')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// pings the login window to check if it was closed
|
|
31
|
+
const closeCheck = setInterval(() => {
|
|
32
|
+
if (win.closed) {
|
|
33
|
+
destroy();
|
|
34
|
+
onClose();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
}, 100)
|
|
38
|
+
|
|
39
|
+
// triggered if the window was closed
|
|
40
|
+
const onClose = () => {
|
|
41
|
+
reject('User closed window')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// completes the process, either because the user closes or because the authentication completed
|
|
45
|
+
// cleans intervals and event listeners
|
|
46
|
+
const destroy = () => {
|
|
47
|
+
window.removeEventListener('message', onMessage)
|
|
48
|
+
clearInterval(closeCheck)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// triggered when a message is received on the current window
|
|
52
|
+
const onMessage = event => {
|
|
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
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
window.addEventListener('message', onMessage)
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
class ViibeAuth {
|
|
71
|
+
|
|
72
|
+
// FIXME
|
|
73
|
+
constructor(host = 'https://auth.viibe.tech') {
|
|
74
|
+
this.host = host;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
loginZendesk(subdomain) {
|
|
78
|
+
const url = `https://${this.host}/auth/zendesk/login?subdomain=${subdomain}`;
|
|
79
|
+
return openAuthPopup(url);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
1
83
|
class Viibe {
|
|
2
84
|
|
|
85
|
+
static Auth = ViibeAuth;
|
|
86
|
+
|
|
3
87
|
/**
|
|
4
88
|
* @typedef Handler
|
|
5
89
|
* @type {object}
|
|
@@ -20,6 +104,17 @@ class Viibe {
|
|
|
20
104
|
window.addEventListener('message', this.onMessage.bind(this))
|
|
21
105
|
}
|
|
22
106
|
|
|
107
|
+
createNotificationIframe() {
|
|
108
|
+
if (!this.host) throw new Error('fillHost')
|
|
109
|
+
if (!this.source) throw new Error('fillSource')
|
|
110
|
+
|
|
111
|
+
const iframe = document.createElement('iframe')
|
|
112
|
+
iframe.name = 'viibe-' + Math.floor(Math.random() * 10000)
|
|
113
|
+
iframe.style = 'border: 0;width: 0;'
|
|
114
|
+
iframe.src = `${this.origin}/integration/features/notifications`
|
|
115
|
+
return iframe
|
|
116
|
+
}
|
|
117
|
+
|
|
23
118
|
createCallIframe(options = {}) {
|
|
24
119
|
if (!this.host) throw new Error('fillHost')
|
|
25
120
|
if (!this.source) throw new Error('fillSource')
|
|
@@ -32,6 +127,8 @@ class Viibe {
|
|
|
32
127
|
iframe.src = src
|
|
33
128
|
iframe.allow = 'geolocation;autoplay;microphone;camera;midi;encrypted-media;clipboard-write;'
|
|
34
129
|
|
|
130
|
+
this.on(iframe, 'document', event => this.onDocument(event))
|
|
131
|
+
|
|
35
132
|
return iframe
|
|
36
133
|
}
|
|
37
134
|
|
|
@@ -43,7 +140,8 @@ class Viibe {
|
|
|
43
140
|
o2m = false,
|
|
44
141
|
ticket,
|
|
45
142
|
token,
|
|
46
|
-
locale
|
|
143
|
+
locale,
|
|
144
|
+
standalone,
|
|
47
145
|
} = options
|
|
48
146
|
|
|
49
147
|
let src = `${this.origin}/integration/features/call`
|
|
@@ -76,6 +174,9 @@ class Viibe {
|
|
|
76
174
|
params.set('components', componentsParams)
|
|
77
175
|
}
|
|
78
176
|
|
|
177
|
+
if (standalone) params.set('standalone', Boolean(standalone))
|
|
178
|
+
|
|
179
|
+
|
|
79
180
|
params.set('clipboard', Boolean(clipboard))
|
|
80
181
|
params.set('o2m', Boolean(o2m))
|
|
81
182
|
|
|
@@ -92,15 +193,15 @@ class Viibe {
|
|
|
92
193
|
* Dispatches an event ie calls handlers corresponding to that event.
|
|
93
194
|
* Handlers with not windowName will be triggered regardless of the origin windowName
|
|
94
195
|
* 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
|
|
196
|
+
*
|
|
197
|
+
* @param {string} eventName
|
|
198
|
+
* @param {any} payload
|
|
199
|
+
* @param {string} windowName
|
|
200
|
+
* @returns
|
|
100
201
|
*/
|
|
101
202
|
dispatch(eventName, payload, windowName) {
|
|
102
203
|
return this.handlers
|
|
103
|
-
.filter(handler =>
|
|
204
|
+
.filter(handler =>
|
|
104
205
|
handler.eventName === eventName
|
|
105
206
|
&& (!handler.windowName || (handler.windowName === windowName))
|
|
106
207
|
)
|
|
@@ -109,8 +210,8 @@ class Viibe {
|
|
|
109
210
|
|
|
110
211
|
/**
|
|
111
212
|
* Handles postMessages / events received on the current window
|
|
112
|
-
* @param {any} event
|
|
113
|
-
* @returns
|
|
213
|
+
* @param {any} event
|
|
214
|
+
* @returns
|
|
114
215
|
*/
|
|
115
216
|
onMessage(event) {
|
|
116
217
|
if (!this.isViibeEvent(event)) {
|
|
@@ -134,15 +235,15 @@ class Viibe {
|
|
|
134
235
|
|
|
135
236
|
/**
|
|
136
237
|
* Registers an event handler.
|
|
137
|
-
* Can be either called with 2 arguments (eventName and handler) to catch events from all iframes
|
|
238
|
+
* Can be either called with 2 arguments (eventName and handler) to catch events from all iframes
|
|
138
239
|
* 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
|
|
240
|
+
* @param {HTMLIFrameElement} [iframe]
|
|
241
|
+
* @param {string} eventName
|
|
242
|
+
* @param {Function} handler
|
|
142
243
|
*/
|
|
143
244
|
on(iframe, eventName, func) {
|
|
144
|
-
if (typeof iframe === 'string'
|
|
145
|
-
&& typeof eventName === 'function'
|
|
245
|
+
if (typeof iframe === 'string'
|
|
246
|
+
&& typeof eventName === 'function'
|
|
146
247
|
&& typeof func === 'undefined') {
|
|
147
248
|
func = eventName;
|
|
148
249
|
eventName = iframe;
|
|
@@ -157,80 +258,19 @@ class Viibe {
|
|
|
157
258
|
this.handlers.push(handler);
|
|
158
259
|
}
|
|
159
260
|
|
|
261
|
+
onDocument(document) {
|
|
262
|
+
window.open(document.signed_url, '_blank')
|
|
263
|
+
}
|
|
264
|
+
|
|
160
265
|
/**
|
|
161
266
|
* Initiates an oauth login in a popup using the passed provider
|
|
162
267
|
* @param {string} provider A strategy name from the Viibe API (see /strategies API)
|
|
163
|
-
* @returns
|
|
268
|
+
* @returns
|
|
164
269
|
*/
|
|
165
270
|
authenticate(provider) {
|
|
166
271
|
const src = `${this.origin}/api/auth/login/${provider}`
|
|
167
|
-
|
|
168
|
-
|
|
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
|
-
|
|
272
|
+
|
|
273
|
+
return openAuthPopup(src);
|
|
234
274
|
}
|
|
235
275
|
|
|
236
276
|
}
|
|
@@ -240,4 +280,4 @@ if (typeof window !== 'undefined') {
|
|
|
240
280
|
}
|
|
241
281
|
if (typeof module !== 'undefined') {
|
|
242
282
|
module.exports = Viibe
|
|
243
|
-
}
|
|
283
|
+
}
|