@wildix/xbees-connect 1.3.4 → 1.3.5

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 (90) hide show
  1. package/dist-cjs/index.js +27 -0
  2. package/dist-cjs/package.json +48 -0
  3. package/dist-cjs/src/Client.js +492 -0
  4. package/dist-cjs/src/enums/index.js +74 -0
  5. package/dist-cjs/src/helpers/ClientParams.js +37 -0
  6. package/dist-cjs/src/helpers/LocalStorageManager.js +74 -0
  7. package/dist-cjs/src/helpers/MessageListener.js +85 -0
  8. package/dist-cjs/src/helpers/PostMessageController.js +48 -0
  9. package/dist-cjs/src/helpers/PostMessageControllerNative.js +47 -0
  10. package/dist-cjs/src/helpers/PostMessageControllerWeb.js +36 -0
  11. package/dist-cjs/src/helpers/TechnicalSupport.js +30 -0
  12. package/dist-cjs/src/utils/url/getUrlSearchParamsMap.js +6 -0
  13. package/dist-cjs/types/AvailableContactData.js +2 -0
  14. package/dist-cjs/types/Callback.js +2 -0
  15. package/dist-cjs/types/Client.js +2 -0
  16. package/dist-cjs/types/Contact.js +2 -0
  17. package/dist-cjs/types/Conversation.js +2 -0
  18. package/dist-cjs/types/Event.js +3 -0
  19. package/dist-cjs/types/Json.js +2 -0
  20. package/dist-cjs/types/Listener.js +2 -0
  21. package/dist-cjs/types/Message.js +2 -0
  22. package/dist-cjs/types/MessageSender.js +2 -0
  23. package/dist-cjs/types/Payload.js +2 -0
  24. package/dist-cjs/types/Platform.js +9 -0
  25. package/dist-cjs/types/Resolver.js +2 -0
  26. package/dist-cjs/types/Storage.js +2 -0
  27. package/dist-cjs/types/Toast.js +2 -0
  28. package/dist-cjs/types/WorkVariant.js +2 -0
  29. package/dist-cjs/types/XBeesUser.js +2 -0
  30. package/dist-cjs/types/index.js +5 -0
  31. package/dist-es/index.js +6 -0
  32. package/dist-es/package.json +48 -0
  33. package/dist-es/src/Client.js +485 -0
  34. package/dist-es/src/enums/index.js +71 -0
  35. package/dist-es/src/helpers/ClientParams.js +34 -0
  36. package/dist-es/src/helpers/LocalStorageManager.js +72 -0
  37. package/dist-es/src/helpers/MessageListener.js +81 -0
  38. package/dist-es/src/helpers/PostMessageController.js +42 -0
  39. package/dist-es/src/helpers/PostMessageControllerNative.js +44 -0
  40. package/dist-es/src/helpers/PostMessageControllerWeb.js +33 -0
  41. package/dist-es/src/helpers/TechnicalSupport.js +25 -0
  42. package/dist-es/src/utils/url/getUrlSearchParamsMap.js +3 -0
  43. package/dist-es/types/AvailableContactData.js +1 -0
  44. package/dist-es/types/Callback.js +1 -0
  45. package/dist-es/types/Client.js +1 -0
  46. package/dist-es/types/Contact.js +1 -0
  47. package/dist-es/types/Conversation.js +1 -0
  48. package/dist-es/types/Event.js +1 -0
  49. package/dist-es/types/Json.js +1 -0
  50. package/dist-es/types/Listener.js +1 -0
  51. package/dist-es/types/Message.js +1 -0
  52. package/dist-es/types/MessageSender.js +1 -0
  53. package/dist-es/types/Payload.js +1 -0
  54. package/dist-es/types/Platform.js +6 -0
  55. package/dist-es/types/Resolver.js +1 -0
  56. package/dist-es/types/Storage.js +1 -0
  57. package/dist-es/types/Toast.js +1 -0
  58. package/dist-es/types/WorkVariant.js +1 -0
  59. package/dist-es/types/XBeesUser.js +1 -0
  60. package/dist-es/types/index.js +1 -0
  61. package/dist-types/index.d.ts +6 -0
  62. package/dist-types/src/Client.d.ts +117 -0
  63. package/dist-types/src/enums/index.d.ts +67 -0
  64. package/dist-types/src/helpers/ClientParams.d.ts +16 -0
  65. package/dist-types/src/helpers/LocalStorageManager.d.ts +20 -0
  66. package/dist-types/src/helpers/MessageListener.d.ts +14 -0
  67. package/dist-types/src/helpers/PostMessageController.d.ts +13 -0
  68. package/dist-types/src/helpers/PostMessageControllerNative.d.ts +8 -0
  69. package/dist-types/src/helpers/PostMessageControllerWeb.d.ts +8 -0
  70. package/dist-types/src/helpers/TechnicalSupport.d.ts +9 -0
  71. package/dist-types/src/utils/url/getUrlSearchParamsMap.d.ts +1 -0
  72. package/dist-types/types/AvailableContactData.d.ts +4 -0
  73. package/dist-types/types/Callback.d.ts +4 -0
  74. package/dist-types/types/Client.d.ts +226 -0
  75. package/dist-types/types/Contact.d.ts +24 -0
  76. package/dist-types/types/Conversation.d.ts +4 -0
  77. package/dist-types/types/Event.d.ts +42 -0
  78. package/dist-types/types/Json.d.ts +6 -0
  79. package/dist-types/types/Listener.d.ts +7 -0
  80. package/dist-types/types/Message.d.ts +14 -0
  81. package/dist-types/types/MessageSender.d.ts +4 -0
  82. package/dist-types/types/Payload.d.ts +98 -0
  83. package/dist-types/types/Platform.d.ts +5 -0
  84. package/dist-types/types/Resolver.d.ts +5 -0
  85. package/dist-types/types/Storage.d.ts +1 -0
  86. package/dist-types/types/Toast.d.ts +1 -0
  87. package/dist-types/types/WorkVariant.d.ts +3 -0
  88. package/dist-types/types/XBeesUser.d.ts +7 -0
  89. package/dist-types/types/index.d.ts +16 -0
  90. package/package.json +2 -3
@@ -0,0 +1,485 @@
1
+ import packageJson from '../package.json';
2
+ import { SupportedPlatformVariant, } from '../types';
3
+ import { ClientEventType, EventType, UrlParams } from './enums';
4
+ import LocalStorageManager from './helpers/LocalStorageManager';
5
+ import { MessageListener } from './helpers/MessageListener';
6
+ import PostMessageControllerNative from './helpers/PostMessageControllerNative';
7
+ import PostMessageControllerWeb from './helpers/PostMessageControllerWeb';
8
+ import TechnicalSupport from './helpers/TechnicalSupport';
9
+ import { getUrlSearchParamsMap } from './utils/url/getUrlSearchParamsMap';
10
+ /**
11
+ * Client provides functionality of communication between xBees and integrated web applications via iFrame or ReactNative WebView
12
+ * integration creates na instance with new Client()
13
+ * */
14
+ export class Client {
15
+ static instance = null;
16
+ static getInstance() {
17
+ if (!this.instance) {
18
+ this.instance = new Client();
19
+ }
20
+ return this.instance;
21
+ }
22
+ static initialize(renderer) {
23
+ if (this.getInstance().showsUi()) {
24
+ try {
25
+ void renderer();
26
+ }
27
+ catch (error) {
28
+ console.error('Error on init rendering widget:', error);
29
+ }
30
+ }
31
+ }
32
+ worker;
33
+ listeners = [];
34
+ useSubscription = false;
35
+ userToken = '';
36
+ pbxDomain = '';
37
+ visible = false;
38
+ user = null;
39
+ userExtension = null;
40
+ userEmail;
41
+ referrer;
42
+ needAuthorize;
43
+ isParentReactNativeWebView;
44
+ iframeId;
45
+ variant = null;
46
+ startPage = null;
47
+ localStorageManager = LocalStorageManager.getInstance();
48
+ constructor() {
49
+ const params = getUrlSearchParamsMap();
50
+ this.iframeId = params.get(UrlParams.ID);
51
+ this.variant = params.get(UrlParams.VARIANT);
52
+ this.userEmail = params.get(UrlParams.USER);
53
+ this.referrer = params.get(UrlParams.REFERRER);
54
+ this.needAuthorize = params.has(UrlParams.AUTHORIZE);
55
+ this.startPage = params.get(UrlParams.START_PAGE);
56
+ // @ts-expect-error window.ReactNativeWebView will be provided by ReactNative WebView
57
+ this.isParentReactNativeWebView = !!window.ReactNativeWebView;
58
+ this.worker = this.isParentReactNativeWebView ? new PostMessageControllerNative() : new PostMessageControllerWeb();
59
+ this.addEventListener(EventType.PBX_TOKEN, (token) => {
60
+ this.userToken = token;
61
+ });
62
+ this.addEventListener(EventType.VISIBILITY, (isActive) => (this.visible = isActive));
63
+ }
64
+ sendAsync(data) {
65
+ if (!this.isOpenedFromXBees()) {
66
+ console.warn('[xbees-connect] Application is not running inside xbees. URL:', window.location.href);
67
+ return new Promise(() => { });
68
+ }
69
+ return this.worker.sendAsync({
70
+ ...data,
71
+ iframeId: this.iframeId,
72
+ });
73
+ }
74
+ async sendAsyncErrorSafe(data) {
75
+ try {
76
+ return await this.sendAsync(data);
77
+ }
78
+ catch (error) {
79
+ console.debug('send error - type:', error);
80
+ }
81
+ }
82
+ parseMessage(message) {
83
+ try {
84
+ const data = typeof message.data === 'string' ? MessageListener.parseJSON(message.data) : message.data;
85
+ if (!data?.type) {
86
+ return null;
87
+ }
88
+ return data;
89
+ }
90
+ catch (error) {
91
+ console.error('parse message error', error);
92
+ return null;
93
+ }
94
+ }
95
+ onMessage(message) {
96
+ const data = this.parseMessage(message);
97
+ if (!data) {
98
+ return;
99
+ }
100
+ const { type, payload } = data;
101
+ this.listeners.forEach(({ eventName, callback }) => {
102
+ if (eventName === type) {
103
+ if (type === EventType.ADD_CALL) {
104
+ const callbackFn = callback;
105
+ callbackFn(payload);
106
+ }
107
+ else {
108
+ // @ts-expect-error TODO: check the type for Callback<?>
109
+ const callbackFn = callback;
110
+ callbackFn(payload);
111
+ }
112
+ }
113
+ });
114
+ }
115
+ sendDailyIntegrationUsageAnalytics() {
116
+ const sentUsageAnalyticsDate = this.getFromStorage('sentUsageAnalyticsDate');
117
+ const today = new Date().toDateString();
118
+ if (sentUsageAnalyticsDate !== today) {
119
+ this.sendAnalytics('xBeesIntegrationUsage');
120
+ this.saveToStorage('sentUsageAnalyticsDate', today);
121
+ }
122
+ }
123
+ ready(props = SupportedPlatformVariant.ALL) {
124
+ const payload = typeof props === 'string'
125
+ ? { version: this.version(), platform: props }
126
+ : { version: this.version(), ...props, platform: props.platform ?? SupportedPlatformVariant.ALL };
127
+ this.getXBeesUser();
128
+ return this.sendAsync({
129
+ type: ClientEventType.READY,
130
+ payload,
131
+ });
132
+ }
133
+ async requestXbeesUser() {
134
+ const responseMessageUser = await this.sendAsync({ type: ClientEventType.USER });
135
+ if (responseMessageUser.payload) {
136
+ const { extension, domain } = responseMessageUser.payload;
137
+ this.user = { ...responseMessageUser.payload, email: this.getUserEmail() };
138
+ if (extension) {
139
+ this.userExtension = extension;
140
+ }
141
+ if (domain) {
142
+ this.pbxDomain = domain;
143
+ }
144
+ }
145
+ }
146
+ /**
147
+ * @deprecated Use getXBeesUserAsync instead. This method doesn't wait for the user data to be fetched.
148
+ */
149
+ getXBeesUser() {
150
+ if (!this.user) {
151
+ this.requestXbeesUser();
152
+ }
153
+ return this.user;
154
+ }
155
+ /**
156
+ * Asynchronously retrieves the xBees user data.
157
+ * This method waits for the user data to be fetched before returning.
158
+ * @returns Promise that resolves to the XBeesUser or null if not available
159
+ */
160
+ async getXBeesUserAsync() {
161
+ if (!this.user) {
162
+ await this.requestXbeesUser();
163
+ }
164
+ return this.user;
165
+ }
166
+ version() {
167
+ return packageJson.version;
168
+ }
169
+ isPlatformNative() {
170
+ return this.isParentReactNativeWebView;
171
+ }
172
+ isPlatformWeb() {
173
+ return !this.isParentReactNativeWebView;
174
+ }
175
+ isOpenedFromXBees() {
176
+ return this.isParentReactNativeWebView || (!!parent && parent !== window);
177
+ }
178
+ getUserPbxToken() {
179
+ return this.userToken;
180
+ }
181
+ getUserEmail() {
182
+ return this.userEmail;
183
+ }
184
+ getPbxDomain() {
185
+ return this.pbxDomain;
186
+ }
187
+ getUserExtension() {
188
+ return this.userExtension;
189
+ }
190
+ getReferrer() {
191
+ return this.referrer;
192
+ }
193
+ getStartPage() {
194
+ return this.startPage;
195
+ }
196
+ isVisible() {
197
+ return this.visible;
198
+ }
199
+ getBackToAppUrl() {
200
+ if (Client.getInstance().isPlatformNative()) {
201
+ return `${this.referrer}://integrations/${this.iframeId}`;
202
+ }
203
+ return `${this.referrer}/integrations/${this.iframeId}`;
204
+ }
205
+ isDataOnly() {
206
+ return this.variant === 'no-ui' || this.variant === 'daemon';
207
+ }
208
+ isSetupDialog() {
209
+ return this.variant === 'd' || this.variant === 'dialog';
210
+ }
211
+ showsUi() {
212
+ return !this.isDataOnly();
213
+ }
214
+ isFullsize() {
215
+ return this.variant === 'f';
216
+ }
217
+ isActivationOnly() {
218
+ return this.needAuthorize;
219
+ }
220
+ getContext() {
221
+ return this.sendAsync({ type: ClientEventType.CONTEXT });
222
+ }
223
+ getCurrentContact() {
224
+ return this.sendAsync({ type: ClientEventType.CURRENT_CONTACT });
225
+ }
226
+ getCurrentConversation() {
227
+ return this.sendAsync({ type: ClientEventType.CURRENT_CONVERSATION });
228
+ }
229
+ getAvailableContactData() {
230
+ return this.sendAsync({ type: ClientEventType.AVAILABLE_CONTACT_DATA });
231
+ }
232
+ contactUpdated(query, contact) {
233
+ return this.sendAsync({ type: ClientEventType.CONTACT_CREATE_OR_UPDATE, payload: { query, contact } });
234
+ }
235
+ contactMatchUpdated(query, contact) {
236
+ return this.sendAsync({ type: ClientEventType.CONTACT_MATCH_UPDATE, payload: { query, contact } });
237
+ }
238
+ getThemeMode() {
239
+ return this.sendAsync({ type: ClientEventType.THEME_MODE });
240
+ }
241
+ getTheme() {
242
+ return this.sendAsync({ type: ClientEventType.THEME });
243
+ }
244
+ startCall(phoneNumber) {
245
+ return this.sendAsync({
246
+ type: ClientEventType.START_CALL,
247
+ payload: { phoneNumber },
248
+ });
249
+ }
250
+ reboot() {
251
+ return this.sendAsync({ type: ClientEventType.REBOOT });
252
+ }
253
+ setViewport(payload) {
254
+ return this.sendAsync({ type: ClientEventType.VIEW_PORT, payload });
255
+ }
256
+ toClipboard(payload) {
257
+ return this.sendAsync({ type: ClientEventType.TO_CLIPBOARD, payload });
258
+ }
259
+ showToast(message, severity = 'INFO') {
260
+ return this.sendAsync({ type: ClientEventType.TOAST, payload: { message, severity } });
261
+ }
262
+ isNotAuthorized() {
263
+ return this.sendAsync({ type: ClientEventType.NOT_AUTHORIZED });
264
+ }
265
+ isAuthorized() {
266
+ this.sendDailyIntegrationUsageAnalytics();
267
+ return this.sendAsync({
268
+ type: ClientEventType.AUTHORIZED,
269
+ });
270
+ }
271
+ addEventListener(eventName, callback) {
272
+ if (!this.useSubscription) {
273
+ this.useSubscription = true;
274
+ window.addEventListener('message', this.onMessage.bind(this));
275
+ }
276
+ const foundThisEvent = this.listeners.find(({ eventName: _eventName, callback: _callback }) => eventName === _eventName && Object.is(callback, _callback));
277
+ if (!foundThisEvent) {
278
+ this.listeners.push({ eventName, callback });
279
+ }
280
+ return () => {
281
+ this.removeEventListener(eventName, callback);
282
+ };
283
+ }
284
+ removeEventListener(eventName, callback) {
285
+ this.listeners = this.listeners.filter(({ eventName: _eventName, callback: _callback }) => !(Object.is(callback, _callback) && (!eventName ? true : eventName === _eventName)));
286
+ if (this.useSubscription && !this.listeners.length) {
287
+ this.useSubscription = false;
288
+ window.removeEventListener('message', this.onMessage.bind(this));
289
+ }
290
+ }
291
+ off(callback) {
292
+ this.localStorageManager.removeOnStorage(callback);
293
+ this.removeEventListener(null, callback);
294
+ }
295
+ onRedirectQuery(callback) {
296
+ return this.addEventListener(EventType.REDIRECT_QUERY, callback);
297
+ }
298
+ onCallEnded(callback) {
299
+ return this.addEventListener(EventType.TERMINATE_CALL, callback);
300
+ }
301
+ onCallStarted(callback) {
302
+ return this.addEventListener(EventType.ADD_CALL, callback);
303
+ }
304
+ onPbxTokenChange(callback) {
305
+ return this.addEventListener(EventType.PBX_TOKEN, callback);
306
+ }
307
+ getXBeesToken() {
308
+ return this.sendAsync({ type: ClientEventType.TOKEN });
309
+ }
310
+ onSuggestContacts(callback) {
311
+ // send event to x-bees
312
+ void this.sendAsyncErrorSafe({
313
+ type: ClientEventType.CONTACTS_AUTO_SUGGEST_IS_SUPPORTED,
314
+ });
315
+ return this.addEventListener(EventType.GET_CONTACTS_AUTO_SUGGEST, (query) => {
316
+ const resolve = (contacts) => this.sendAsync({
317
+ type: ClientEventType.CONTACTS_AUTO_SUGGEST,
318
+ payload: {
319
+ contacts,
320
+ query,
321
+ },
322
+ });
323
+ const reject = (reason) => {
324
+ console.debug(reason);
325
+ };
326
+ try {
327
+ callback(query, resolve, reject);
328
+ }
329
+ catch (error) {
330
+ reject(`${error}`);
331
+ }
332
+ });
333
+ }
334
+ onLookupAndMatchContact(callback) {
335
+ // send event to x-bees
336
+ void this.sendAsyncErrorSafe({
337
+ type: ClientEventType.CONTACT_LOOK_UP_AND_MATCH_IS_SUPPORTED,
338
+ });
339
+ return this.addEventListener(EventType.GET_LOOK_UP_AND_MATCH, (query) => {
340
+ const resolve = (contact) => this.sendAsync({
341
+ type: ClientEventType.CONTACT_LOOKUP_AND_MATCH,
342
+ payload: {
343
+ contact,
344
+ query,
345
+ },
346
+ });
347
+ const reject = (reason) => {
348
+ console.debug(reason);
349
+ void this.sendAsync({
350
+ type: ClientEventType.CONTACT_LOOKUP_AND_MATCH_NOT_FOUND,
351
+ payload: { query },
352
+ });
353
+ };
354
+ try {
355
+ callback(query, resolve, reject);
356
+ }
357
+ catch (error) {
358
+ reject(`${error}`);
359
+ }
360
+ });
361
+ }
362
+ onLookupAndMatchBatchContacts(callback) {
363
+ // send event to x-bees
364
+ void this.sendAsyncErrorSafe({
365
+ type: ClientEventType.LOOK_UP_AND_MATCH_BATCH_CONTACTS_IS_SUPPORTED,
366
+ });
367
+ return this.addEventListener(EventType.GET_LOOK_UP_AND_MATCH_BATCH, (queries) => {
368
+ const returnResults = (contactResultsMap) => this.sendAsync({
369
+ type: ClientEventType.LOOK_UP_AND_MATCH_BATCH_CONTACTS,
370
+ payload: {
371
+ contactResultsMap: Array.from(contactResultsMap),
372
+ },
373
+ });
374
+ const reject = (reason) => {
375
+ console.debug(reason);
376
+ void this.sendAsync({
377
+ type: ClientEventType.LOOKUP_AND_MATCH_BATCH_ERROR,
378
+ payload: { reason },
379
+ });
380
+ };
381
+ try {
382
+ callback(queries, returnResults);
383
+ }
384
+ catch (error) {
385
+ reject(`${error}`);
386
+ }
387
+ });
388
+ }
389
+ createContactIsSupported() {
390
+ return this.sendAsync({
391
+ type: ClientEventType.CREATE_CONTACT_IS_SUPPORTED,
392
+ });
393
+ }
394
+ createContactHasNoPermission() {
395
+ return this.sendAsync({
396
+ type: ClientEventType.CREATE_CONTACT_HAS_NO_PERMISSION,
397
+ });
398
+ }
399
+ onThemeChange(callback) {
400
+ return this.addEventListener(EventType.USE_THEME, callback);
401
+ }
402
+ getFromStorage(key) {
403
+ return this.localStorageManager.retrieve(key);
404
+ }
405
+ saveToStorage(key, value) {
406
+ this.localStorageManager.save(key, value);
407
+ }
408
+ deleteFromStorage(key) {
409
+ this.localStorageManager.delete(key);
410
+ }
411
+ onStorage(listener) {
412
+ return this.localStorageManager.onStorage(listener);
413
+ }
414
+ onLogout(callback) {
415
+ // send event to x-bees
416
+ void this.sendAsync({
417
+ type: ClientEventType.LOGOUT_IS_SUPPORTED,
418
+ });
419
+ return this.addEventListener(EventType.LOGOUT, callback);
420
+ }
421
+ sendAnalytics(eventName, params) {
422
+ void this.sendAsync({
423
+ type: ClientEventType.SEND_ANALYTICS,
424
+ payload: {
425
+ eventName,
426
+ params,
427
+ },
428
+ });
429
+ }
430
+ setIntegrationStorageKey(integrationKey) {
431
+ this.localStorageManager.setIntegrationKey(integrationKey);
432
+ }
433
+ getTechnicalSupport() {
434
+ return TechnicalSupport.getInstance();
435
+ }
436
+ onVisibilityChange(callback) {
437
+ return this.addEventListener(EventType.VISIBILITY, callback);
438
+ }
439
+ saveInXbeesStorage(key, value) {
440
+ void this.sendAsync({
441
+ type: ClientEventType.SAVE_TO_STORAGE,
442
+ payload: {
443
+ key,
444
+ value: typeof value === 'string' ? value : JSON.stringify(value),
445
+ },
446
+ });
447
+ }
448
+ getFromXbeesStorage(key) {
449
+ return this.sendAsync({
450
+ type: ClientEventType.GET_FROM_STORAGE,
451
+ payload: { key },
452
+ });
453
+ }
454
+ removeFromXbeesStorage(key) {
455
+ void this.sendAsync({
456
+ type: ClientEventType.REMOVE_FROM_STORAGE,
457
+ payload: { key },
458
+ });
459
+ }
460
+ /** should be removed, sendCustomEvent is enough **/
461
+ sendDropdownVisibilityEvent(dropdownVisibilityStatus) {
462
+ void this.sendAsync({
463
+ type: ClientEventType.DROPDOWN_VISIBILITY,
464
+ payload: { dropdownVisibilityStatus },
465
+ });
466
+ }
467
+ sendCustomEvent({ type, payload }) {
468
+ void this.sendAsync({
469
+ type: ClientEventType.CUSTOM_EVENT,
470
+ payload: { type, payload },
471
+ });
472
+ }
473
+ onContactWeightUpdate(callback) {
474
+ return this.addEventListener(EventType.CONTACT_WEIGHT_UPDATE, callback);
475
+ }
476
+ onContactRefresh(callback) {
477
+ return this.addEventListener(EventType.CONTACT_REFRESH, callback);
478
+ }
479
+ onStartRedirectToEntityPage(callback) {
480
+ return this.addEventListener(EventType.START_REDIRECT_TO_ENTITY_PAGE, callback);
481
+ }
482
+ onCancelRedirectToEntityPage(callback) {
483
+ return this.addEventListener(EventType.CANCEL_REDIRECT_TO_ENTITY_PAGE, callback);
484
+ }
485
+ }
@@ -0,0 +1,71 @@
1
+ export var EventType;
2
+ (function (EventType) {
3
+ EventType["GET_CONTACTS_AUTO_SUGGEST"] = "xBeesGetContactsAutoSuggest";
4
+ EventType["GET_LOOK_UP_AND_MATCH"] = "xBeesGetLookUpAndMatch";
5
+ EventType["GET_LOOK_UP_AND_MATCH_BATCH"] = "xBeesGetLookUpAndMatchBatch";
6
+ EventType["ADD_CALL"] = "xBeesAddCall";
7
+ EventType["TERMINATE_CALL"] = "xBeesTerminateCall";
8
+ EventType["USE_THEME"] = "xBeesUseTheme";
9
+ EventType["PBX_TOKEN"] = "xBeesPbxToken";
10
+ EventType["REDIRECT_QUERY"] = "xBeesRedirectQuery";
11
+ EventType["LOGOUT"] = "xBeesLogout";
12
+ EventType["VISIBILITY"] = "xBeesVisibility";
13
+ EventType["CONTACT_WEIGHT_UPDATE"] = "xBeesContactWeightUpdate";
14
+ EventType["CONTACT_REFRESH"] = "xBeesContactRefresh";
15
+ EventType["START_REDIRECT_TO_ENTITY_PAGE"] = "xBeesStartRedirectToEntityPage";
16
+ EventType["CANCEL_REDIRECT_TO_ENTITY_PAGE"] = "xBeesCancelRedirectToEntityPage";
17
+ })(EventType || (EventType = {}));
18
+ export var ClientEventType;
19
+ (function (ClientEventType) {
20
+ ClientEventType["READY"] = "xBeesReady";
21
+ ClientEventType["CONTEXT"] = "xBeesContext";
22
+ ClientEventType["CURRENT_CONTACT"] = "xBeesCurrentContact";
23
+ ClientEventType["CURRENT_CONVERSATION"] = "xBeesCurrentConversation";
24
+ ClientEventType["THEME_MODE"] = "xBeesThemeMode";
25
+ ClientEventType["THEME"] = "xBeesTheme";
26
+ ClientEventType["START_CALL"] = "xBeesStartCall";
27
+ ClientEventType["VIEW_PORT"] = "xBeesViewPort";
28
+ ClientEventType["REBOOT"] = "xBeesReboot";
29
+ ClientEventType["TO_CLIPBOARD"] = "xBeesToClipboard";
30
+ ClientEventType["TOAST"] = "xBeesShowToast";
31
+ ClientEventType["NOT_AUTHORIZED"] = "xBeesNotAuthorized";
32
+ ClientEventType["AUTHORIZED"] = "xBeesAuthorized";
33
+ ClientEventType["TOKEN"] = "xBeesToken";
34
+ ClientEventType["CONTACTS_AUTO_SUGGEST"] = "xBeesContactsAutoSuggest";
35
+ ClientEventType["CONTACT_LOOKUP_AND_MATCH"] = "xBeesContactLookupAndMatch";
36
+ ClientEventType["LOOK_UP_AND_MATCH_BATCH_CONTACTS"] = "xBeesLookupAndMatchBatchContacts";
37
+ ClientEventType["LOOKUP_AND_MATCH_BATCH_ERROR"] = "xBeesLookupAndMatchBatchError";
38
+ ClientEventType["CONTACT_LOOKUP_AND_MATCH_NOT_FOUND"] = "xBeesContactLookupAndMatchNotFound";
39
+ ClientEventType["CONTACT_CREATE_OR_UPDATE"] = "xBeesContactCreateOrUpdate";
40
+ ClientEventType["CONTACT_MATCH_UPDATE"] = "xBeesContactMatchUpdate";
41
+ ClientEventType["CONTACTS_AUTO_SUGGEST_IS_SUPPORTED"] = "xBeesContactsAutoSuggestIsSupported";
42
+ ClientEventType["CONTACT_LOOK_UP_AND_MATCH_IS_SUPPORTED"] = "xBeesContactLookupAndMatchIsSupported";
43
+ ClientEventType["LOOK_UP_AND_MATCH_BATCH_CONTACTS_IS_SUPPORTED"] = "xBeesLookupAndMatchBatchContactsIsSupported";
44
+ ClientEventType["LOGOUT_IS_SUPPORTED"] = "xBeesLogoutIsSupported";
45
+ ClientEventType["SEND_ANALYTICS"] = "xBeesSendAnalytics";
46
+ ClientEventType["SEND_TECHNICAL_SUPPORT_INFORMATION"] = "xBeesSendTechInfo";
47
+ ClientEventType["USER"] = "xBeesUser";
48
+ ClientEventType["SAVE_TO_STORAGE"] = "xBeesSaveToStorage";
49
+ ClientEventType["GET_FROM_STORAGE"] = "xBeesRequestFromStorage";
50
+ ClientEventType["REMOVE_FROM_STORAGE"] = "xBeesRemoveFromStorage";
51
+ ClientEventType["AVAILABLE_CONTACT_DATA"] = "xBeesAvailableContactData";
52
+ ClientEventType["DROPDOWN_VISIBILITY"] = "xBeesDropdownVisibility";
53
+ ClientEventType["CUSTOM_EVENT"] = "xBeesCustomEvent";
54
+ ClientEventType["CREATE_CONTACT_IS_SUPPORTED"] = "xBeesCreateContactIsSupported";
55
+ ClientEventType["CREATE_CONTACT_HAS_NO_PERMISSION"] = "xBeesCreateContactHasNoPermission";
56
+ })(ClientEventType || (ClientEventType = {}));
57
+ export var UrlParams;
58
+ (function (UrlParams) {
59
+ UrlParams["TOKEN"] = "t";
60
+ UrlParams["ID"] = "i";
61
+ UrlParams["VARIANT"] = "v";
62
+ UrlParams["USER"] = "u";
63
+ UrlParams["EXTENSION"] = "e";
64
+ UrlParams["REFERRER"] = "r";
65
+ UrlParams["AUTHORIZE"] = "a";
66
+ UrlParams["START_PAGE"] = "sp";
67
+ })(UrlParams || (UrlParams = {}));
68
+ export var StartPage;
69
+ (function (StartPage) {
70
+ StartPage["CREATE_CONTACT"] = "createContact";
71
+ })(StartPage || (StartPage = {}));
@@ -0,0 +1,34 @@
1
+ import { UrlParams } from '../enums';
2
+ import { getUrlSearchParamsMap } from '../utils/url/getUrlSearchParamsMap';
3
+ export default class ClientParams {
4
+ static instance = null;
5
+ static getInstance() {
6
+ if (!this.instance) {
7
+ this.instance = new ClientParams();
8
+ }
9
+ return this.instance;
10
+ }
11
+ userToken;
12
+ userEmail;
13
+ referrer;
14
+ needAuthorize;
15
+ iframeId;
16
+ variant = null;
17
+ userExtension = null;
18
+ startPage = null;
19
+ constructor() {
20
+ const params = getUrlSearchParamsMap();
21
+ this.iframeId = params.get(UrlParams.ID);
22
+ this.variant = params.get(UrlParams.VARIANT);
23
+ this.userEmail = params.get(UrlParams.USER);
24
+ this.userExtension = params.get(UrlParams.EXTENSION);
25
+ this.userToken = params.get(UrlParams.TOKEN);
26
+ this.referrer = params.get(UrlParams.REFERRER);
27
+ this.needAuthorize = params.has(UrlParams.AUTHORIZE);
28
+ this.startPage = params.get(UrlParams.START_PAGE);
29
+ }
30
+ toString() {
31
+ const { userToken, ...props } = this;
32
+ return JSON.stringify({ ...props, userToken: userToken && `...${userToken.slice(-5)}` });
33
+ }
34
+ }
@@ -0,0 +1,72 @@
1
+ class LocalStorageManager {
2
+ static instance = null;
3
+ static getInstance() {
4
+ if (!this.instance) {
5
+ this.instance = new LocalStorageManager();
6
+ }
7
+ return this.instance;
8
+ }
9
+ listeners = new Set();
10
+ integrationKey = '';
11
+ setIntegrationKey(integrationKey) {
12
+ this.integrationKey = integrationKey;
13
+ }
14
+ getStorageKey(key) {
15
+ return !this.integrationKey ? key : `${this.integrationKey}_${key}`;
16
+ }
17
+ getKey(key) {
18
+ return !this.integrationKey ? key : key.slice(`${this.integrationKey}_`.length);
19
+ }
20
+ constructor() {
21
+ if (this.shouldUseIntegrationKey()) {
22
+ this.integrationKey = this.calculateIntegrationKey();
23
+ }
24
+ window.addEventListener('storage', (event) => {
25
+ if (!event.key)
26
+ return;
27
+ const key = this.getKey(event.key);
28
+ this.listeners.forEach((listener) => listener({ ...event, key }));
29
+ });
30
+ }
31
+ calculateIntegrationKey() {
32
+ const pattern = /^\/(\S+)?\/index\.html$/;
33
+ const match = window.location.pathname.match(pattern);
34
+ if (match && match[1]) {
35
+ const [, key] = match;
36
+ return key;
37
+ }
38
+ return '';
39
+ }
40
+ shouldUseIntegrationKey() {
41
+ return window.location.host.endsWith('integrations.x-bees.com');
42
+ }
43
+ save(key, value) {
44
+ // Save data to localStorage
45
+ localStorage.setItem(this.getStorageKey(key), typeof value === 'string' ? value : JSON.stringify(value));
46
+ }
47
+ delete(key) {
48
+ // Save data to localStorage
49
+ localStorage.removeItem(this.getStorageKey(key));
50
+ }
51
+ retrieve(key) {
52
+ // Retrieve data from localStorage
53
+ const data = localStorage.getItem(this.getStorageKey(key));
54
+ try {
55
+ return !data ? data : JSON.parse(data);
56
+ }
57
+ catch (error) {
58
+ return data;
59
+ }
60
+ }
61
+ get(key) {
62
+ return localStorage.getItem(this.getStorageKey(key));
63
+ }
64
+ onStorage(listener) {
65
+ this.listeners.add(listener);
66
+ return () => this.removeOnStorage(listener);
67
+ }
68
+ removeOnStorage(listener) {
69
+ this.listeners.delete(listener);
70
+ }
71
+ }
72
+ export default LocalStorageManager;