11import { inject , Injectable } from '@angular/core' ;
22import { IndexedDBService } from './indexeddb.service' ;
33
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 ;
99} ;
1010
1111@Injectable ( {
@@ -14,50 +14,84 @@ export type StorageOptions = {
1414export class StorageService {
1515 private readonly indexedDB = inject ( IndexedDBService ) ;
1616
17+ private storage : Storage | null = null ;
18+
19+ setStorage ( storage : Storage ) : void {
20+ this . storage = storage ;
21+ }
22+
1723 // 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 ;
2029
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 ) ;
2635 }
2736
28- const { dbName, storeName } = configOrKey ;
37+ if ( this . storage === null ) {
38+ throw new Error ( 'Storage not set' ) ;
39+ }
2940
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 ) ;
3146 }
3247
3348 // 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 ;
3654
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' ) ;
4468 }
4569
46- const { dbName, storeName } = configOrKey ;
47- await this . indexedDB . write ( dbName , storeName , value ) ;
70+ return this . storage . setItem ( config . key , value ) ;
4871 }
72+ //
73+ // // remove item from storage(localStorage, sessionStorage, indexedDB)
74+ async removeItem ( config : IndexedDBConfig ) : Promise < void > ;
4975
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+ }
5390
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' ) ;
5893 }
5994
60- const { dbName, storeName } = configOrKey ;
61- return await this . indexedDB . clear ( dbName , storeName ) ;
95+ return this . storage . removeItem ( config . key ) ;
6296 }
6397}
0 commit comments