@mparticle/web-sdk 2.20.2 → 2.21.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/mparticle.common.js +74 -40
- package/dist/mparticle.esm.js +74 -40
- package/dist/mparticle.js +815 -2380
- package/package.json +5 -5
- package/src/apiClient.ts +2 -4
- package/src/batchUploader.ts +113 -11
- package/src/configAPIClient.ts +19 -2
- package/src/constants.ts +1 -0
- package/src/forwarders.interfaces.ts +61 -0
- package/src/forwarders.js +1 -1
- package/src/helpers.js +4 -75
- package/src/mockBatchCreator.ts +13 -9
- package/src/sdkRuntimeModels.ts +0 -1
- package/src/store.ts +7 -0
- package/src/utils.ts +79 -4
- package/src/vault.ts +51 -39
package/src/vault.ts
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
import { Logger } from '@mparticle/web-sdk';
|
|
2
|
-
import {
|
|
2
|
+
import { isEmpty } from './utils';
|
|
3
3
|
|
|
4
|
-
interface IVaultOptions {
|
|
4
|
+
export interface IVaultOptions {
|
|
5
5
|
logger?: Logger;
|
|
6
6
|
offlineStorageEnabled?: boolean;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export
|
|
9
|
+
export abstract class BaseVault<StorableItem> {
|
|
10
10
|
public contents: StorableItem;
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
protected readonly _storageKey: string;
|
|
12
|
+
protected logger?: Logger;
|
|
13
|
+
protected storageObject: Storage;
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
*
|
|
16
17
|
* @param {string} storageKey the local storage key string
|
|
17
|
-
* @param {
|
|
18
|
+
* @param {Storage} Web API Storage object that is being used
|
|
18
19
|
* @param {IVaultOptions} options A Dictionary of IVaultOptions
|
|
19
20
|
*/
|
|
20
|
-
constructor(
|
|
21
|
+
constructor(
|
|
22
|
+
storageKey: string,
|
|
23
|
+
storageObject: Storage,
|
|
24
|
+
options?: IVaultOptions
|
|
25
|
+
) {
|
|
21
26
|
this._storageKey = storageKey;
|
|
22
|
-
this.
|
|
27
|
+
this.storageObject = storageObject;
|
|
23
28
|
|
|
24
29
|
// Add a fake logger in case one is not provided or needed
|
|
25
30
|
this.logger = options?.logger || {
|
|
@@ -27,62 +32,69 @@ export default class Vault<StorableItem> {
|
|
|
27
32
|
warning: () => {},
|
|
28
33
|
error: () => {},
|
|
29
34
|
};
|
|
35
|
+
|
|
36
|
+
this.contents = this.retrieve();
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
/**
|
|
33
|
-
* Stores a StorableItem to
|
|
34
|
-
* @method
|
|
40
|
+
* Stores a StorableItem to Storage
|
|
41
|
+
* @method store
|
|
35
42
|
* @param item {StorableItem}
|
|
36
43
|
*/
|
|
37
44
|
public store(item: StorableItem): void {
|
|
38
45
|
this.contents = item;
|
|
39
46
|
|
|
40
|
-
|
|
47
|
+
const stringifiedItem = !isEmpty(item) ? JSON.stringify(item) : '';
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
this.storageObject.setItem(this._storageKey, stringifiedItem);
|
|
41
51
|
|
|
42
|
-
|
|
52
|
+
this.logger.verbose(`Saving item to Storage: ${stringifiedItem}`);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
this.logger.error(
|
|
55
|
+
`Cannot Save items to Storage: ${stringifiedItem}`
|
|
56
|
+
);
|
|
57
|
+
this.logger.error(error as string);
|
|
58
|
+
}
|
|
43
59
|
}
|
|
44
60
|
|
|
45
61
|
/**
|
|
46
|
-
*
|
|
47
|
-
* @method
|
|
48
|
-
* @returns {StorableItem
|
|
62
|
+
* Retrieve StorableItem from Storage
|
|
63
|
+
* @method retrieve
|
|
64
|
+
* @returns {StorableItem}
|
|
49
65
|
*/
|
|
50
|
-
public retrieve(): StorableItem {
|
|
51
|
-
|
|
66
|
+
public retrieve(): StorableItem | null {
|
|
67
|
+
// TODO: Handle cases where Local Storage is unavailable
|
|
68
|
+
// https://go.mparticle.com/work/SQDSDKS-5022
|
|
69
|
+
const item: string = this.storageObject.getItem(this._storageKey);
|
|
70
|
+
|
|
71
|
+
this.contents = item ? JSON.parse(item) : null;
|
|
72
|
+
|
|
73
|
+
this.logger.verbose(`Retrieving item from Storage: ${item}`);
|
|
52
74
|
|
|
53
75
|
return this.contents;
|
|
54
76
|
}
|
|
55
77
|
|
|
56
78
|
/**
|
|
57
|
-
* Removes all persisted data from
|
|
79
|
+
* Removes all persisted data from Storage based on this vault's `key`
|
|
80
|
+
* Will remove storage key from Storage as well
|
|
58
81
|
* @method purge
|
|
59
82
|
*/
|
|
60
83
|
public purge(): void {
|
|
84
|
+
this.logger.verbose('Purging Storage');
|
|
61
85
|
this.contents = null;
|
|
62
|
-
this.
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private saveToLocalStorage(items: StorableItem): void {
|
|
66
|
-
try {
|
|
67
|
-
window.localStorage.setItem(
|
|
68
|
-
this._storageKey,
|
|
69
|
-
!isEmpty(items) ? JSON.stringify(items) : ''
|
|
70
|
-
);
|
|
71
|
-
} catch (error) {
|
|
72
|
-
this.logger.error(`Cannot Save items to Local Storage: ${items}`);
|
|
73
|
-
this.logger.error(error as string);
|
|
74
|
-
}
|
|
86
|
+
this.storageObject.removeItem(this._storageKey);
|
|
75
87
|
}
|
|
88
|
+
}
|
|
76
89
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const item: string = window.localStorage.getItem(this._storageKey);
|
|
81
|
-
|
|
82
|
-
return item ? JSON.parse(item) : null;
|
|
90
|
+
export class LocalStorageVault<StorableItem> extends BaseVault<StorableItem> {
|
|
91
|
+
constructor(storageKey: string, options?: IVaultOptions) {
|
|
92
|
+
super(storageKey, window.localStorage, options);
|
|
83
93
|
}
|
|
94
|
+
}
|
|
84
95
|
|
|
85
|
-
|
|
86
|
-
|
|
96
|
+
export class SessionStorageVault<StorableItem> extends BaseVault<StorableItem> {
|
|
97
|
+
constructor(storageKey: string, options?: IVaultOptions) {
|
|
98
|
+
super(storageKey, window.sessionStorage, options);
|
|
87
99
|
}
|
|
88
100
|
}
|