@wildix/xbees-connect 1.2.0-alpha.2 → 1.2.0-alpha.4

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildix/xbees-connect",
3
- "version": "1.2.0-alpha.2",
3
+ "version": "1.2.0-alpha.4",
4
4
  "description": "This library provides easy communication between x-bees and integrated web applications",
5
5
  "author": "dimitri.chernykh <dimitri.chernykh@wildix.com>",
6
6
  "homepage": "",
@@ -2,7 +2,9 @@ import packageJson from '../package.json';
2
2
  import { ClientEventType, EventType } from './enums';
3
3
  import ClientParams from './helpers/ClientParams';
4
4
  import LocalStorageManager from './helpers/LocalStorageManager';
5
+ import { MessageListener } from './helpers/MessageListener';
5
6
  import PostMessageController from './helpers/PostMessageController';
7
+ import TechnicalSupport from './helpers/TechnicalSupport';
6
8
  /**
7
9
  * Client provides functionality of communication between xBees and integrated web applications via iFrame or ReactNative WebView
8
10
  * integration creates na instance with new Client()
@@ -28,52 +30,14 @@ export class Client {
28
30
  void this.getInstance().ready();
29
31
  }
30
32
  }
31
- listeners = [];
32
- useSubscription = false;
33
33
  localStorageManager = LocalStorageManager.getInstance();
34
+ messageListener = MessageListener.getInstance();
34
35
  constructor() {
35
36
  this.addEventListener(EventType.PBX_TOKEN, (token) => (ClientParams.getInstance().userToken = token));
36
37
  }
37
38
  sendAsync(data) {
38
39
  return PostMessageController.getInstance().sendAsync(data);
39
40
  }
40
- parseMessage(message) {
41
- try {
42
- const data = typeof message.data === 'string' ? JSON.parse(message.data) : message.data;
43
- if (!data?.type) {
44
- return null;
45
- }
46
- return data;
47
- }
48
- catch (error) {
49
- console.error('parse message error', error);
50
- return null;
51
- }
52
- }
53
- onMessage(message) {
54
- if (window.location.host === message.origin || window === message.source) {
55
- // skip events started from integration itself if any
56
- return;
57
- }
58
- const data = this.parseMessage(message);
59
- if (!data) {
60
- return;
61
- }
62
- const { type, payload } = data;
63
- this.listeners.forEach(({ eventName, callback }) => {
64
- if (eventName === type) {
65
- if (type === EventType.ADD_CALL) {
66
- const callbackFn = callback;
67
- callbackFn(payload);
68
- }
69
- else {
70
- // @ts-expect-error TODO: check the type for Callback<?>
71
- const callbackFn = callback;
72
- callbackFn(payload);
73
- }
74
- }
75
- });
76
- }
77
41
  ready(platform = 'all') {
78
42
  return this.sendAsync({
79
43
  type: ClientEventType.READY,
@@ -165,24 +129,10 @@ export class Client {
165
129
  return this.sendAsync({ type: ClientEventType.AUTHORIZED });
166
130
  }
167
131
  addEventListener(eventName, callback) {
168
- if (!this.useSubscription) {
169
- this.useSubscription = true;
170
- window.addEventListener('message', this.onMessage.bind(this));
171
- }
172
- const foundThisEvent = this.listeners.find(({ eventName: _eventName, callback: _callback }) => eventName === _eventName && Object.is(callback, _callback));
173
- if (!foundThisEvent) {
174
- this.listeners.push({ eventName, callback });
175
- }
176
- return () => {
177
- this.removeEventListener(eventName, callback);
178
- };
132
+ return this.messageListener.addEventListener(eventName, callback);
179
133
  }
180
134
  removeEventListener(eventName, callback) {
181
- this.listeners = this.listeners.filter(({ eventName: _eventName, callback: _callback }) => !(Object.is(callback, _callback) && (!eventName ? true : eventName === _eventName)));
182
- if (this.useSubscription && !this.listeners.length) {
183
- this.useSubscription = false;
184
- window.removeEventListener('message', this.onMessage.bind(this));
185
- }
135
+ this.messageListener.removeEventListener(eventName, callback);
186
136
  }
187
137
  off(callback) {
188
138
  this.localStorageManager.removeOnStorage(callback);
@@ -289,4 +239,7 @@ export class Client {
289
239
  setIntegrationStorageKey(integrationKey) {
290
240
  this.localStorageManager.setIntegrationKey(integrationKey);
291
241
  }
242
+ getTechnicalSupport() {
243
+ return TechnicalSupport.getInstance();
244
+ }
292
245
  }
@@ -23,4 +23,7 @@ export default class ClientParams {
23
23
  this.referrer = (params.get(UrlParams.REFERRER) ?? params.get(DeprecatedUrlParams.REFERRER));
24
24
  this.needAuthorize = params.has(UrlParams.AUTHORIZE) ?? params.has(DeprecatedUrlParams.AUTHORIZE);
25
25
  }
26
+ toString() {
27
+ return JSON.stringify(this);
28
+ }
26
29
  }
@@ -0,0 +1,78 @@
1
+ import { EventType } from '../enums';
2
+ export class MessageListener {
3
+ static instance = null;
4
+ static getInstance() {
5
+ if (!this.instance) {
6
+ this.instance = new MessageListener();
7
+ }
8
+ return this.instance;
9
+ }
10
+ listeners = [];
11
+ useSubscription = false;
12
+ // eslint-disable-next-line
13
+ constructor() { }
14
+ parseMessage(message) {
15
+ try {
16
+ const data = typeof message.data === 'string' ? this.parseJSON(message.data) : message.data;
17
+ if (!data?.type) {
18
+ return null;
19
+ }
20
+ return data;
21
+ }
22
+ catch (error) {
23
+ console.error('parse message error', error);
24
+ return null;
25
+ }
26
+ }
27
+ parseJSON(messageData) {
28
+ const trimmedData = messageData.trim();
29
+ if (!trimmedData || !trimmedData.startsWith('{') || !trimmedData.endsWith('}')) {
30
+ return null;
31
+ }
32
+ return JSON.parse(trimmedData);
33
+ }
34
+ onMessage = (message) => {
35
+ if (window.location.host === message.origin || window === message.source) {
36
+ // skip events started from integration itself if any
37
+ return;
38
+ }
39
+ const data = this.parseMessage(message);
40
+ if (!data) {
41
+ return;
42
+ }
43
+ const { type, payload } = data;
44
+ this.listeners.forEach(({ eventName, callback }) => {
45
+ if (eventName === type) {
46
+ if (type === EventType.ADD_CALL) {
47
+ const callbackFn = callback;
48
+ callbackFn(payload);
49
+ }
50
+ else {
51
+ // @ts-expect-error TODO: check the type for Callback<?>
52
+ const callbackFn = callback;
53
+ callbackFn(payload);
54
+ }
55
+ }
56
+ });
57
+ };
58
+ addEventListener(eventName, callback) {
59
+ if (!this.useSubscription) {
60
+ this.useSubscription = true;
61
+ window.addEventListener('message', this.onMessage);
62
+ }
63
+ const foundThisEvent = this.listeners.find(({ eventName: _eventName, callback: _callback }) => eventName === _eventName && Object.is(callback, _callback));
64
+ if (!foundThisEvent) {
65
+ this.listeners.push({ eventName, callback });
66
+ }
67
+ return () => {
68
+ this.removeEventListener(eventName, callback);
69
+ };
70
+ }
71
+ removeEventListener(eventName, callback) {
72
+ this.listeners = this.listeners.filter(({ eventName: _eventName, callback: _callback }) => !(Object.is(callback, _callback) && (!eventName ? true : eventName === _eventName)));
73
+ if (this.useSubscription && !this.listeners.length) {
74
+ this.useSubscription = false;
75
+ window.removeEventListener('message', this.onMessage);
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,25 @@
1
+ import { ClientEventType } from '../enums';
2
+ import ClientParams from './ClientParams';
3
+ import PostMessageController from './PostMessageController';
4
+ class TechnicalSupport {
5
+ static instance = null;
6
+ static getInstance() {
7
+ if (!this.instance) {
8
+ this.instance = new TechnicalSupport();
9
+ }
10
+ return this.instance;
11
+ }
12
+ // eslint-disable-next-line
13
+ constructor() { }
14
+ sendTechnicalInformation(message, data) {
15
+ return PostMessageController.getInstance().sendAsyncErrorSafe({
16
+ type: ClientEventType.SEND_TECHNICAL_SUPPORT_INFORMATION,
17
+ payload: {
18
+ 'x-iframe-params': ClientParams.getInstance(),
19
+ message,
20
+ data,
21
+ },
22
+ });
23
+ }
24
+ }
25
+ export default TechnicalSupport;
@@ -0,0 +1 @@
1
+ export {};
@@ -8,6 +8,7 @@ import { LookupAndMatchContactsResolver, Reject, SuggestContactsResolver } from
8
8
  import { StorageEventCallback } from '../types/Storage';
9
9
  import { ToastSeverity } from '../types/Toast';
10
10
  import { ClientEventType, EventType } from './enums';
11
+ import TechnicalSupport from './helpers/TechnicalSupport';
11
12
  /**
12
13
  * Client provides functionality of communication between xBees and integrated web applications via iFrame or ReactNative WebView
13
14
  * integration creates na instance with new Client()
@@ -16,13 +17,10 @@ export declare class Client implements ConnectClient {
16
17
  private static instance;
17
18
  static getInstance(): ConnectClient;
18
19
  static initialize(renderer: () => Promise<void>): void;
19
- private listeners;
20
- private useSubscription;
21
20
  private readonly localStorageManager;
22
- constructor();
21
+ private readonly messageListener;
22
+ private constructor();
23
23
  private sendAsync;
24
- private parseMessage;
25
- private onMessage;
26
24
  ready(platform?: SupportedPlatformVariant | undefined): Promise<ResponseMessage>;
27
25
  version(): string;
28
26
  isPlatformNative(): boolean;
@@ -68,4 +66,5 @@ export declare class Client implements ConnectClient {
68
66
  onLogout(callback: Callback<EventType.LOGOUT>): RemoveEventListener;
69
67
  sendAnalytics(eventName: string, params?: Record<string, string>): void;
70
68
  setIntegrationStorageKey(integrationKey: string): void;
69
+ getTechnicalSupport(): TechnicalSupport;
71
70
  }
@@ -9,4 +9,5 @@ export default class ClientParams {
9
9
  readonly iframeId: string;
10
10
  readonly variant: WorkVariants | null;
11
11
  constructor();
12
+ toString(): string;
12
13
  }
@@ -0,0 +1,15 @@
1
+ import { Callback } from '../../types/Callback';
2
+ import { RemoveEventListener } from '../../types/Listener';
3
+ import { EventType } from '../enums';
4
+ export declare class MessageListener {
5
+ private static instance;
6
+ static getInstance(): MessageListener;
7
+ private listeners;
8
+ private useSubscription;
9
+ private constructor();
10
+ private parseMessage;
11
+ private parseJSON;
12
+ private onMessage;
13
+ addEventListener<T extends EventType = EventType>(eventName: T, callback: Callback<T>): RemoveEventListener;
14
+ removeEventListener<T extends EventType = EventType>(eventName: T | null, callback: Callback<T>): void;
15
+ }
@@ -0,0 +1,9 @@
1
+ import { JSONArray, JSONObject } from '../../types';
2
+ import { ClientEventType } from '../enums';
3
+ declare class TechnicalSupport {
4
+ private static instance;
5
+ static getInstance(): TechnicalSupport;
6
+ private constructor();
7
+ sendTechnicalInformation(message: string, data?: JSONObject | JSONArray): Promise<import("../../types/Message").ResponseMessage<ClientEventType.SEND_TECHNICAL_SUPPORT_INFORMATION> | undefined>;
8
+ }
9
+ export default TechnicalSupport;
@@ -1,4 +1,5 @@
1
1
  import { ClientEventType, EventType } from '../src/enums';
2
+ import TechnicalSupport from '../src/helpers/TechnicalSupport';
2
3
  import { Callback } from './Callback';
3
4
  import { Contact, ContactQuery } from './Contact';
4
5
  import { RemoveEventListener } from './Listener';
@@ -147,4 +148,7 @@ export interface ConnectClient {
147
148
  /**
148
149
  * send analytics data to x-bees for track into analytics data */
149
150
  sendAnalytics: (eventName: string, params?: Record<string, string>) => void;
151
+ /**
152
+ * special object to provide x-bees technical support */
153
+ getTechnicalSupport: () => TechnicalSupport;
150
154
  }
@@ -0,0 +1,7 @@
1
+ type JSONValue = string | number | boolean | null | JSONObject | JSONArray;
2
+ export interface JSONObject {
3
+ [key: string]: JSONValue;
4
+ }
5
+ export interface JSONArray extends Array<JSONValue> {
6
+ }
7
+ export {};
@@ -1,5 +1,7 @@
1
+ import ClientParams from '../src/helpers/ClientParams';
1
2
  import { Contact, ContactQuery } from './Contact';
2
3
  import { Conversation } from './conversation';
4
+ import { JSONArray, JSONObject } from './Json';
3
5
  import { SupportedPlatformVariant } from './Platform';
4
6
  import { ToastSeverity } from './Toast';
5
7
  export interface IPayloadViewPort {
@@ -52,5 +54,7 @@ export interface IPayloadSendAnalytics {
52
54
  params?: Record<string, string>;
53
55
  }
54
56
  export interface IPayloadTechSupport {
57
+ 'x-iframe-params': ClientParams;
55
58
  message: string;
59
+ data?: JSONObject | JSONArray;
56
60
  }
@@ -1,3 +1,4 @@
1
1
  import { Contact, ContactQuery } from './Contact';
2
+ import { JSONArray, JSONObject } from './Json';
2
3
  import { IPayloadAutoSuggestResult, IPayloadCallStart, IPayloadCallStartedInfo, IPayloadContactMatchResult, IPayloadContactMatchResultNotFound, IPayloadContactResult, IPayloadContextResult, IPayloadConversationResult, IPayloadSendAnalytics, IPayloadTechSupport, IPayloadThemeChange, IPayloadToast, IPayloadVersion, IPayloadViewPort } from './Payload';
3
- export type { Contact, ContactQuery, IPayloadAutoSuggestResult, IPayloadCallStart, IPayloadCallStartedInfo, IPayloadContactMatchResult, IPayloadContactMatchResultNotFound, IPayloadContactResult, IPayloadContextResult, IPayloadConversationResult, IPayloadSendAnalytics, IPayloadTechSupport, IPayloadThemeChange, IPayloadToast, IPayloadVersion, IPayloadViewPort, };
4
+ export type { Contact, ContactQuery, IPayloadAutoSuggestResult, IPayloadCallStart, IPayloadCallStartedInfo, IPayloadContactMatchResult, IPayloadContactMatchResultNotFound, IPayloadContactResult, IPayloadContextResult, IPayloadConversationResult, IPayloadSendAnalytics, IPayloadTechSupport, IPayloadThemeChange, IPayloadToast, IPayloadVersion, IPayloadViewPort, JSONArray, JSONObject, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildix/xbees-connect",
3
- "version": "1.2.0-alpha.2",
3
+ "version": "1.2.0-alpha.4",
4
4
  "description": "This library provides easy communication between x-bees and integrated web applications",
5
5
  "author": "dimitri.chernykh <dimitri.chernykh@wildix.com>",
6
6
  "homepage": "",
@@ -42,5 +42,5 @@
42
42
  "engines": {
43
43
  "node": ">=16"
44
44
  },
45
- "gitHead": "e18b94393f098d28bdcdf727173e765802584692"
45
+ "gitHead": "9b2373d5205d394e554be89f5001d14105322995"
46
46
  }