1
1
import { inject , Injectable } from '@angular/core' ;
2
2
import { IndexedDBService } from './indexeddb.service' ;
3
3
4
- export type StorageType = 'localStorage' | 'sessionStorage' ;
5
-
6
- export type StorageOptions = {
7
- dbName : string ;
8
- storeName : string ;
4
+ export type IndexedDBConfig = {
5
+ storage : 'localStorage' | 'sessionStorage' | 'indexedDB' ;
6
+ key ?: string ;
7
+ dbName ? : string ;
8
+ storeName ? : string ;
9
9
} ;
10
10
11
11
@Injectable ( {
@@ -14,50 +14,84 @@ export type StorageOptions = {
14
14
export class StorageService {
15
15
private readonly indexedDB = inject ( IndexedDBService ) ;
16
16
17
+ private storage : Storage | null = null ;
18
+
19
+ setStorage ( storage : Storage ) : void {
20
+ this . storage = storage ;
21
+ }
22
+
17
23
// get item from storage(localStorage, sessionStorage, indexedDB)
18
- async getItem ( type : StorageType ) : Promise < string | null > ;
19
- async getItem ( options : StorageOptions ) : Promise < string | null > ;
24
+ async getItem ( config : IndexedDBConfig ) : Promise < string | null > ;
25
+
26
+ async getItem ( config : IndexedDBConfig ) : Promise < string | null > {
27
+ if ( config . storage === 'indexedDB' ) {
28
+ const { dbName, storeName } = config ;
20
29
21
- async getItem (
22
- configOrKey : StorageOptions | StorageType
23
- ) : Promise < string | null > {
24
- if ( typeof configOrKey === 'string' ) {
25
- return window [ configOrKey ] . getItem ( configOrKey ) ;
30
+ if ( dbName === undefined || storeName === undefined ) {
31
+ throw new Error ( 'dbName and storeName must be set' ) ;
32
+ }
33
+
34
+ return await this . indexedDB . read ( dbName , storeName ) ;
26
35
}
27
36
28
- const { dbName, storeName } = configOrKey ;
37
+ if ( this . storage === null ) {
38
+ throw new Error ( 'Storage not set' ) ;
39
+ }
29
40
30
- return await this . indexedDB . read ( dbName , storeName ) ;
41
+ if ( config . key === undefined ) {
42
+ throw new Error ( 'key is undefined' ) ;
43
+ }
44
+
45
+ return this . storage . getItem ( config . key ) ;
31
46
}
32
47
33
48
// set item in storage(localStorage, sessionStorage, indexedDB)
34
- async setItem ( type : StorageType , value : string ) : Promise < void > ;
35
- async setItem ( options : StorageOptions , value : string ) : Promise < void > ;
49
+ async setItem ( config : IndexedDBConfig , value : string ) : Promise < void > ;
50
+
51
+ async setItem ( config : IndexedDBConfig , value : string ) : Promise < void > {
52
+ if ( config . storage === 'indexedDB' ) {
53
+ const { dbName, storeName } = config ;
36
54
37
- async setItem (
38
- configOrKey : StorageOptions | StorageType ,
39
- value : string
40
- ) : Promise < void > {
41
- if ( typeof configOrKey === 'string' ) {
42
- window [ configOrKey ] . setItem ( configOrKey , value ) ;
43
- return ;
55
+ if ( dbName === undefined || storeName === undefined ) {
56
+ throw new Error ( 'dbName and storeName must be set' ) ;
57
+ }
58
+
59
+ return await this . indexedDB . write ( dbName , storeName , value ) ;
60
+ }
61
+
62
+ if ( this . storage === null ) {
63
+ throw new Error ( 'Storage not set' ) ;
64
+ }
65
+
66
+ if ( config . key === undefined ) {
67
+ throw new Error ( 'key is undefined' ) ;
44
68
}
45
69
46
- const { dbName, storeName } = configOrKey ;
47
- await this . indexedDB . write ( dbName , storeName , value ) ;
70
+ return this . storage . setItem ( config . key , value ) ;
48
71
}
72
+ //
73
+ // // remove item from storage(localStorage, sessionStorage, indexedDB)
74
+ async removeItem ( config : IndexedDBConfig ) : Promise < void > ;
49
75
50
- // remove item from storage(localStorage, sessionStorage, indexedDB)
51
- async removeItem ( type : StorageType ) : Promise < void > ;
52
- async removeItem ( options : StorageOptions ) : Promise < void > ;
76
+ async removeItem ( config : IndexedDBConfig ) : Promise < void > {
77
+ if ( config . storage === 'indexedDB' ) {
78
+ const { dbName, storeName } = config ;
79
+
80
+ if ( dbName === undefined || storeName === undefined ) {
81
+ throw new Error ( 'dbName and storeName must be set' ) ;
82
+ }
83
+
84
+ return await this . indexedDB . clear ( dbName , storeName ) ;
85
+ }
86
+
87
+ if ( this . storage === null ) {
88
+ throw new Error ( 'Storage not set' ) ;
89
+ }
53
90
54
- async removeItem ( configOrKey : StorageOptions | StorageType ) : Promise < void > {
55
- if ( typeof configOrKey === 'string' ) {
56
- window [ configOrKey ] . removeItem ( configOrKey ) ;
57
- return ;
91
+ if ( config . key === undefined ) {
92
+ throw new Error ( 'key is undefined' ) ;
58
93
}
59
94
60
- const { dbName, storeName } = configOrKey ;
61
- return await this . indexedDB . clear ( dbName , storeName ) ;
95
+ return this . storage . removeItem ( config . key ) ;
62
96
}
63
97
}
0 commit comments