1
1
import { isObjectLike } from 'lodash'
2
+ import { Maybe } from 'uiSrc/utils'
2
3
import BrowserStorageItem from '../constants/storage'
3
4
4
5
class StorageService {
5
6
private storage : Storage
6
7
7
- constructor ( storage : Storage ) {
8
+ private envKey : Maybe < string >
9
+
10
+ constructor ( storage : Storage , envKey ?: string ) {
8
11
this . storage = storage
12
+ this . envKey = envKey
13
+ }
14
+
15
+ private getKey ( itemName : string ) : string {
16
+ return this . envKey ? `${ this . envKey } _${ itemName } ` : itemName
9
17
}
10
18
11
19
get ( itemName : string = '' ) {
20
+ const key = this . getKey ( itemName )
12
21
let item
13
22
try {
14
- item = this . storage . getItem ( itemName )
23
+ item = this . storage . getItem ( key )
15
24
} catch ( error ) {
16
25
console . error ( `getItem from storage error: ${ error } ` )
17
26
}
@@ -26,28 +35,28 @@ class StorageService {
26
35
return null
27
36
}
28
37
29
- getAll ( ) {
30
- return this . storage
31
- }
32
-
33
38
set ( itemName : string = '' , item : any ) {
34
39
try {
40
+ const key = this . getKey ( itemName )
35
41
if ( isObjectLike ( item ) ) {
36
- this . storage . setItem ( itemName , JSON . stringify ( item ) )
42
+ this . storage . setItem ( key , JSON . stringify ( item ) )
37
43
} else {
38
- this . storage . setItem ( itemName , item )
44
+ this . storage . setItem ( key , item )
39
45
}
40
46
} catch ( error ) {
41
47
console . error ( `setItem to storage error: ${ error } ` )
42
48
}
43
49
}
44
50
45
51
remove ( itemName : string = '' ) {
46
- this . storage . removeItem ( itemName )
52
+ const key = this . getKey ( itemName )
53
+ this . storage . removeItem ( key )
47
54
}
48
55
}
49
- export const localStorageService = new StorageService ( localStorage )
50
- export const sessionStorageService = new StorageService ( sessionStorage )
56
+ const envKey = window . __RI_PROXY_PATH__
57
+
58
+ export const localStorageService = new StorageService ( localStorage , envKey )
59
+ export const sessionStorageService = new StorageService ( sessionStorage , envKey )
51
60
52
61
export const getObjectStorageField = ( itemName = '' , field = '' ) => {
53
62
try {
0 commit comments