@viibe/impulse-integration-js 2.1.0 → 2.2.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.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- (()=>{"use strict";var e={d:(t,o)=>{for(var r in o)e.o(o,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:o[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};function o(e,t){for(var o=0;o<t.length;o++){var r=t[o];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}e.r(t),e.d(t,{default:()=>r});const r=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.host=t.host,this.origin=/^localhost/.test(this.host)?"http://".concat(this.host):"https://".concat(this.host),this.source=t.source}var t,r;return t=e,r=[{key:"createCallIframe",value:function(){var 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 t=this.createCallUrl(e),o=document.createElement("iframe");return o.style="border: 0;width: calc(100% - 1rem);margin: 0.5rem;",o.src=t,o.allow="geolocation;autoplay;microphone;camera;midi;encrypted-media;clipboard-write;",o}},{key:"createCallUrl",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.links,o=e.ticket,r=e.token,n=e.locale,a="".concat(this.origin,"/integration/features/call"),i=["resize=true"];if(i.push("source=".concat(this.source)),null!=o&&o.integration&&i.push("thirdPartyId=".concat(o.integration.id)),t){var c=t.map((function(e){return"".concat(e.category,":").concat(e.value)})).join(",");i.push("links=".concat(c))}return r&&i.push("token=".concat(r)),n&&i.push("locale=".concat(n)),a+"?"+i.join("&")}},{key:"on",value:function(e,t){var o=this;window.addEventListener("message",(function(r){var n=Boolean(r.isTrusted&&0===r.origin.indexOf(o.origin)),a=Boolean(e===r.data.event);return n&&a&&t(r.data.payload||null),null}))}}],r&&o(t.prototype,r),e}();module.exports=t})();
1
+ (()=>{"use strict";var t={d:(e,n)=>{for(var o in n)t.o(n,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:n[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};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)}}t.r(e),t.d(e,{default:()=>o});const o=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.host=e.host,this.origin=/^localhost/.test(this.host)?"http://".concat(this.host):"https://".concat(this.host),this.source=e.source,this.handlers={},window.addEventListener("message",this.onMessage.bind(this))}var e,o;return e=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.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.links,n=t.ticket,o=t.token,a=t.locale,r="".concat(this.origin,"/integration/features/call"),i=["resize=true"];if(i.push("source=".concat(this.source)),null!=n&&n.integration&&i.push("thirdPartyId=".concat(n.integration.id)),e){var c=e.map((function(t){return"".concat(t.category,":").concat(t.value)})).join(",");i.push("links=".concat(c))}return o&&i.push("token=".concat(o)),a&&i.push("locale=".concat(a)),r+"?"+i.join("&")}},{key:"isViibeEvent",value:function(t){return Boolean(t.isTrusted&&0===t.origin.indexOf(this.origin))}},{key:"dispatch",value:function(t,e){var n=this.handlers[t];return n?n.map((function(t){return t(e)})):null}},{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&&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){(this.handlers[t]=this.handlers[t]||[]).push(e)}},{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,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(t,o){var a=window.open(n,"Authorization",r.join(", ")),i=setInterval((function(){a.closed&&(s(),c())}),100),c=function(){o("User closed window")},s=function(){window.removeEventListener("message",l),clearInterval(i)},l=function(n){if(!e.isViibeEvent(n))return null;n.data.token&&(s(),t(n.data.token)),n.data.error&&(s(),o(n.data.error))};window.addEventListener("message",l)}))}}],o&&n(e.prototype,o),t}();module.exports=e})();
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@viibe/impulse-integration-js",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "ViiBE integration in js native",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "build": "webpack --mode production",
8
8
  "prepublish": "npm run build",
9
- "test": "echo \"Error: no test specified\" && exit 1"
9
+ "test": "echo \"Error: no test specified\" && exit 1",
10
+ "publish:public": "npm publish --access public"
10
11
  },
11
12
  "author": "Viibe",
12
13
  "license": "ISC",
package/src/index.js CHANGED
@@ -4,6 +4,9 @@ class Viibe {
4
4
  this.host = options.host
5
5
  this.origin = /^localhost/.test(this.host) ? `http://${this.host}` : `https://${this.host}`
6
6
  this.source = options.source
7
+ this.handlers = {}
8
+
9
+ window.addEventListener('message', this.onMessage.bind(this))
7
10
  }
8
11
 
9
12
  createCallIframe(options = {}) {
@@ -45,17 +48,102 @@ class Viibe {
45
48
  return src
46
49
  }
47
50
 
48
- on(eventName, handler) {
51
+ isViibeEvent(event) {
52
+ return Boolean(event.isTrusted && event.origin.indexOf(this.origin) === 0)
53
+ }
49
54
 
50
- const handlerWrapper = event => {
51
- const isFromViiBE = Boolean(event.isTrusted && event.origin.indexOf(this.origin) === 0)
52
- const isFromThisEvent = Boolean(eventName === event.data.event)
55
+ dispatch(eventName, payload) {
56
+ const eventHandlers = this.handlers[eventName];
57
+ if(!eventHandlers) {
58
+ return null
59
+ }
60
+ return eventHandlers.map(handler => handler(payload))
61
+ }
53
62
 
54
- if (isFromViiBE && isFromThisEvent) handler(event.data.payload || null)
63
+ onMessage(event) {
64
+ if(!this.isViibeEvent(event)) {
55
65
  return null
56
66
  }
57
67
 
58
- window.addEventListener('message', handlerWrapper)
68
+ if(event.data && event.data.event && event.data.payload) {
69
+ return this.dispatch(event.data.event, event.data.payload)
70
+ }
71
+
72
+ // FIXME: legacy authentication messages
73
+ if(event.data && event.data.token) {
74
+ return this.dispatch('authentication:token', event.data.token)
75
+ }
76
+ if(event.data && event.data.error) {
77
+ return this.dispatch('authentication:error', event.data.error)
78
+ }
79
+
80
+ return null
81
+ }
82
+
83
+ on(eventName, handler) {
84
+ const eventHandlers = this.handlers[eventName] = this.handlers[eventName] || []
85
+ eventHandlers.push(handler);
86
+ }
87
+
88
+ authenticate(provider) {
89
+ const src = `${this.origin}/api/auth/login/${provider}`
90
+ const width = 480
91
+ const height = 600
92
+ const TWO = 2
93
+
94
+ const left = (window.screen.width / TWO) - (width / TWO)
95
+ const top = (window.screen.height / TWO) - (height / TWO)
96
+ const windowParams = [
97
+ 'toolbar=no',
98
+ 'location=no',
99
+ 'directories=no',
100
+ 'status=no',
101
+ 'menubar=no',
102
+ 'scrollbars=no',
103
+ 'resizable=no',
104
+ 'copyhistory=no',
105
+ `width=${width}`,
106
+ `height=${height}`,
107
+ `top=${top}`,
108
+ `left=${left}`,
109
+ ]
110
+
111
+ return new Promise((resolve, reject) => {
112
+ const win = window.open(src, 'Authorization', windowParams.join(', '))
113
+
114
+ const closeCheck = setInterval(() => {
115
+ if(win.closed) {
116
+ destroy();
117
+ onClose();
118
+ }
119
+ }, 100)
120
+
121
+ const onClose = () => {
122
+ reject('User closed window')
123
+ }
124
+
125
+ const destroy = () => {
126
+ window.removeEventListener('message', onMessage)
127
+ clearInterval(closeCheck)
128
+ }
129
+
130
+ const onMessage = event => {
131
+ if(!this.isViibeEvent(event)) {
132
+ return null
133
+ }
134
+ if(event.data.token) {
135
+ destroy();
136
+ resolve(event.data.token)
137
+ }
138
+ if(event.data.error) {
139
+ destroy();
140
+ reject(event.data.error)
141
+ }
142
+ }
143
+
144
+ window.addEventListener('message', onMessage)
145
+ })
146
+
59
147
  }
60
148
 
61
149
  }