-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(types): provide simplistic storage interfaces
- Loading branch information
Showing
6 changed files
with
90 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import { PersistentStorage } from '../types'; | ||
|
||
export class IonicStorageWrapper implements PersistentStorage<string> { | ||
// Actual type definition: https://github.com/ionic-team/ionic-storage/blob/main/src/storage.ts#L102 | ||
export class IonicStorageWrapper implements PersistentStorage<string | null> { | ||
private storage; | ||
|
||
constructor(storage: any) { | ||
constructor(storage: IonicStorageInterface) { | ||
this.storage = storage; | ||
} | ||
|
||
getItem(key: string): string | Promise<string | null> | null { | ||
getItem(key: string): Promise<string | null> { | ||
return this.storage.get(key); | ||
} | ||
|
||
removeItem(key: string): void | Promise<void> { | ||
removeItem(key: string): Promise<void> { | ||
return this.storage.remove(key); | ||
} | ||
|
||
setItem(key: string, value: string): void | Promise<void> { | ||
setItem(key: string, value: string | null): Promise<void> { | ||
return this.storage.set(key, value); | ||
} | ||
} | ||
|
||
interface IonicStorageInterface { | ||
// Actual type definition: https://github.com/ionic-team/ionic-storage/blob/main/lib/src/index.ts | ||
get(key: string): Promise<string | null>; | ||
set(key: string, value: string | null): Promise<void>; | ||
remove(key: string): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,33 @@ | ||
import { PersistentStorage } from '../types'; | ||
|
||
export class LocalStorageWrapper implements PersistentStorage<string> { | ||
// Actual type definition: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L15286 | ||
export class LocalStorageWrapper implements PersistentStorage<string | null> { | ||
private storage; | ||
|
||
constructor(storage: any) { | ||
constructor(storage: LocalStorageInterface) { | ||
this.storage = storage; | ||
} | ||
|
||
getItem(key: string): string | Promise<string | null> | null { | ||
getItem(key: string): string | null { | ||
return this.storage.getItem(key); | ||
} | ||
|
||
removeItem(key: string): void | Promise<void> { | ||
removeItem(key: string): void { | ||
return this.storage.removeItem(key); | ||
} | ||
|
||
setItem(key: string, value: string): void | Promise<void> { | ||
return this.storage.setItem(key, value); | ||
setItem(key: string, value: string | null): void { | ||
if (value !== null) { | ||
// setting null to localstorage stores "null" as string | ||
return this.storage.setItem(key, value); | ||
} else { | ||
return this.removeItem(key); | ||
} | ||
} | ||
} | ||
|
||
interface LocalStorageInterface { | ||
// Actual type definition: https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts#L14276 | ||
getItem(key: string): string | null; | ||
setItem(key: string, value: string): void; | ||
removeItem(key: string): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,33 @@ | ||
import { PersistentStorage } from '../types'; | ||
|
||
export class SessionStorageWrapper implements PersistentStorage<string> { | ||
// Actual type definition: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L15286 | ||
export class SessionStorageWrapper implements PersistentStorage<string | null> { | ||
private storage; | ||
|
||
constructor(storage: any) { | ||
constructor(storage: SessionStorageInterface) { | ||
this.storage = storage; | ||
} | ||
|
||
getItem(key: string): string | Promise<string | null> | null { | ||
getItem(key: string): string | null { | ||
return this.storage.getItem(key); | ||
} | ||
|
||
removeItem(key: string): void | Promise<void> { | ||
removeItem(key: string): void { | ||
return this.storage.removeItem(key); | ||
} | ||
|
||
setItem(key: string, value: string): void | Promise<void> { | ||
return this.storage.setItem(key, value); | ||
setItem(key: string, value: string | null): void { | ||
if (value !== null) { | ||
// setting null to sessionstorage stores "null" as string | ||
return this.storage.setItem(key, value); | ||
} else { | ||
return this.removeItem(key); | ||
} | ||
} | ||
} | ||
|
||
interface SessionStorageInterface { | ||
// Actual type definition: https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts#L14276 | ||
getItem(key: string): string | null; | ||
setItem(key: string, value: string): void; | ||
removeItem(key: string): void; | ||
} |