@wildix/xbees-connect 1.1.0-alpha.0 → 1.1.2-alpha.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-es/package.json +3 -3
- package/dist-es/src/Client.js +8 -0
- package/dist-es/src/helpers/LocalStorageManager.js +65 -0
- package/dist-types/src/Client.d.ts +3 -0
- package/dist-types/src/helpers/LocalStorageManager.d.ts +16 -0
- package/dist-types/types/index.d.ts +6 -0
- package/package.json +4 -4
package/dist-es/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wildix/xbees-connect",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
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": "",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"eslint": "^8.
|
|
33
|
+
"eslint": "^8.55.0",
|
|
34
34
|
"rimraf": "^5.0.5",
|
|
35
|
-
"typescript": "^5.
|
|
35
|
+
"typescript": "^5.3.3"
|
|
36
36
|
},
|
|
37
37
|
"parserOptions": {
|
|
38
38
|
"project": [
|
package/dist-es/src/Client.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import packageJson from '../package.json';
|
|
2
2
|
import { ClientEventType, DeprecatedUrlParams, EventType, UrlParams } from './enums';
|
|
3
|
+
import LocalStorageManager from './helpers/LocalStorageManager';
|
|
3
4
|
import PostMessageControllerNative from './helpers/PostMessageControllerNative';
|
|
4
5
|
import PostMessageControllerWeb from './helpers/PostMessageControllerWeb';
|
|
5
6
|
/**
|
|
@@ -36,6 +37,7 @@ export class Client {
|
|
|
36
37
|
needAuthorize;
|
|
37
38
|
iframeId;
|
|
38
39
|
variant = null;
|
|
40
|
+
localStorageManager = LocalStorageManager.getInstance();
|
|
39
41
|
constructor() {
|
|
40
42
|
const params = new URLSearchParams(window.location.search);
|
|
41
43
|
this.iframeId = (params.get(UrlParams.ID) ?? params.get(DeprecatedUrlParams.ID));
|
|
@@ -239,4 +241,10 @@ export class Client {
|
|
|
239
241
|
onThemeChange(callback) {
|
|
240
242
|
this.addEventListener(EventType.USE_THEME, callback);
|
|
241
243
|
}
|
|
244
|
+
getFromStorage(key) {
|
|
245
|
+
this.localStorageManager.retrieve(key);
|
|
246
|
+
}
|
|
247
|
+
saveToStorage(key, value) {
|
|
248
|
+
this.localStorageManager.save(key, value);
|
|
249
|
+
}
|
|
242
250
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
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 = [];
|
|
10
|
+
integrationKey = '';
|
|
11
|
+
getStorageKey(key) {
|
|
12
|
+
return !this.integrationKey ? key : `${this.integrationKey}_${key}`;
|
|
13
|
+
}
|
|
14
|
+
getKey(key) {
|
|
15
|
+
return !this.integrationKey
|
|
16
|
+
? key
|
|
17
|
+
: key.slice(`${this.integrationKey}_`.length);
|
|
18
|
+
}
|
|
19
|
+
constructor() {
|
|
20
|
+
if (this.shouldUseIntegrationKey()) {
|
|
21
|
+
this.integrationKey = this.calculateIntegrationKey();
|
|
22
|
+
}
|
|
23
|
+
window.addEventListener('storage', (event) => {
|
|
24
|
+
if (!event.key)
|
|
25
|
+
return;
|
|
26
|
+
const key = this.getKey(event.key);
|
|
27
|
+
this.listeners.forEach((listener) => listener({ ...event, key }));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
calculateIntegrationKey() {
|
|
31
|
+
const pattern = /^\/([^/]+)\/index\.html$/;
|
|
32
|
+
const match = window.location.pathname.match(pattern);
|
|
33
|
+
if (match && match[1]) {
|
|
34
|
+
const [, key] = match;
|
|
35
|
+
return key;
|
|
36
|
+
}
|
|
37
|
+
return '';
|
|
38
|
+
}
|
|
39
|
+
shouldUseIntegrationKey() {
|
|
40
|
+
return (window.location.host.endsWith('integrations.x-bees.com') ||
|
|
41
|
+
window.location.pathname.endsWith('/index.html'));
|
|
42
|
+
}
|
|
43
|
+
save(key, value) {
|
|
44
|
+
// Save data to localStorage
|
|
45
|
+
localStorage.setItem(this.getStorageKey(key), JSON.stringify(value));
|
|
46
|
+
}
|
|
47
|
+
retrieve(key) {
|
|
48
|
+
// Retrieve data from localStorage
|
|
49
|
+
const data = localStorage.getItem(this.getStorageKey(key));
|
|
50
|
+
try {
|
|
51
|
+
return !data ? data : JSON.parse(data);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('parse data error', error);
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
get(key) {
|
|
59
|
+
return localStorage.getItem(this.getStorageKey(key));
|
|
60
|
+
}
|
|
61
|
+
onStorage(listener) {
|
|
62
|
+
this.listeners.push(listener);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export default LocalStorageManager;
|
|
@@ -17,6 +17,7 @@ export declare class Client implements ConnectClient {
|
|
|
17
17
|
private readonly needAuthorize;
|
|
18
18
|
private readonly iframeId;
|
|
19
19
|
private readonly variant;
|
|
20
|
+
private readonly localStorageManager;
|
|
20
21
|
constructor();
|
|
21
22
|
private sendAsync;
|
|
22
23
|
private parseMessage;
|
|
@@ -50,4 +51,6 @@ export declare class Client implements ConnectClient {
|
|
|
50
51
|
onSuggestContacts(callback: (query: string, resolve: SuggestContactsResolver, reject: Reject) => void): RemoveEventListener;
|
|
51
52
|
onLookupAndMatchContact(callback: (query: ContactQuery, resolve: LookupAndMatchContactsResolver, reject: Reject) => void): RemoveEventListener;
|
|
52
53
|
onThemeChange(callback: Callback<EventType.USE_THEME>): void;
|
|
54
|
+
getFromStorage(key: string): void;
|
|
55
|
+
saveToStorage(key: string, value: any): void;
|
|
53
56
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare class LocalStorageManager {
|
|
2
|
+
private static instance;
|
|
3
|
+
static getInstance(): LocalStorageManager;
|
|
4
|
+
private listeners;
|
|
5
|
+
private integrationKey;
|
|
6
|
+
getStorageKey(key: string): string;
|
|
7
|
+
getKey(key: string): string;
|
|
8
|
+
constructor();
|
|
9
|
+
private calculateIntegrationKey;
|
|
10
|
+
private shouldUseIntegrationKey;
|
|
11
|
+
save(key: string, value: any): void;
|
|
12
|
+
retrieve(key: string): any;
|
|
13
|
+
get(key: string): any;
|
|
14
|
+
onStorage(listener: (event: StorageEvent) => void): void;
|
|
15
|
+
}
|
|
16
|
+
export default LocalStorageManager;
|
|
@@ -190,5 +190,11 @@ export interface ConnectClient {
|
|
|
190
190
|
/**
|
|
191
191
|
* Removes particular callback from handling events */
|
|
192
192
|
off: (callback: Callback) => void;
|
|
193
|
+
/**
|
|
194
|
+
* saves data to localStorage */
|
|
195
|
+
saveToStorage: (key: string, value: any) => void;
|
|
196
|
+
/**
|
|
197
|
+
* Retrieves data from localStorage */
|
|
198
|
+
getFromStorage: (key: string) => void;
|
|
193
199
|
}
|
|
194
200
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wildix/xbees-connect",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2-alpha.0",
|
|
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": "",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"eslint": "^8.
|
|
33
|
+
"eslint": "^8.55.0",
|
|
34
34
|
"rimraf": "^5.0.5",
|
|
35
|
-
"typescript": "^5.
|
|
35
|
+
"typescript": "^5.3.3"
|
|
36
36
|
},
|
|
37
37
|
"parserOptions": {
|
|
38
38
|
"project": [
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"engines": {
|
|
43
43
|
"node": ">=16"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "72065c36cfbb65edbadec8a73718af7324f62e14"
|
|
46
46
|
}
|