@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/src/vault.ts CHANGED
@@ -1,25 +1,30 @@
1
1
  import { Logger } from '@mparticle/web-sdk';
2
- import { Dictionary, isEmpty } from './utils';
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 default class Vault<StorableItem> {
9
+ export abstract class BaseVault<StorableItem> {
10
10
  public contents: StorableItem;
11
- private readonly _storageKey: string;
12
- private logger?: Logger;
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 {string} itemKey an element within your StorableItem to use as a key
18
+ * @param {Storage} Web API Storage object that is being used
18
19
  * @param {IVaultOptions} options A Dictionary of IVaultOptions
19
20
  */
20
- constructor(storageKey: string, options?: IVaultOptions) {
21
+ constructor(
22
+ storageKey: string,
23
+ storageObject: Storage,
24
+ options?: IVaultOptions
25
+ ) {
21
26
  this._storageKey = storageKey;
22
- this.contents = this.getFromLocalStorage();
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 Local Storage
34
- * @method storeItem
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
- this.logger.verbose(`Saved to local storage: ${item}`);
47
+ const stringifiedItem = !isEmpty(item) ? JSON.stringify(item) : '';
48
+
49
+ try {
50
+ this.storageObject.setItem(this._storageKey, stringifiedItem);
41
51
 
42
- this.saveToLocalStorage(this.contents);
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
- * Retrieves all StorableItems from local storage as an array
47
- * @method retrieveItems
48
- * @returns {StorableItem[]} an array of Items
62
+ * Retrieve StorableItem from Storage
63
+ * @method retrieve
64
+ * @returns {StorableItem}
49
65
  */
50
- public retrieve(): StorableItem {
51
- this.contents = this.getFromLocalStorage();
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 local storage based on this vault's `key`
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.removeFromLocalStorage();
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
- private getFromLocalStorage(): StorableItem | null {
78
- // TODO: Handle cases where Local Storage is unavailable
79
- // https://go.mparticle.com/work/SQDSDKS-5022
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
- private removeFromLocalStorage(): void {
86
- window.localStorage.removeItem(this._storageKey);
96
+ export class SessionStorageVault<StorableItem> extends BaseVault<StorableItem> {
97
+ constructor(storageKey: string, options?: IVaultOptions) {
98
+ super(storageKey, window.sessionStorage, options);
87
99
  }
88
100
  }