Skip to content

Commit

Permalink
fix(types): provide simplistic storage interfaces
Browse files Browse the repository at this point in the history
fix #426, fix #431 by adding support for new synchronous removeItem
  • Loading branch information
wodCZ committed Aug 2, 2021
1 parent 15d8b40 commit 56b1c55
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 40 deletions.
18 changes: 12 additions & 6 deletions src/storageWrappers/AsyncStorageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,29 @@ import { PersistentStorage } from '../types';
* });
*
*/
export class AsyncStorageWrapper implements PersistentStorage<string> {
// Actual type definition: https://github.com/react-native-async-storage/async-storage/blob/master/types/index.d.ts
export class AsyncStorageWrapper implements PersistentStorage<string | null> {
private storage;

constructor(storage: any) {
constructor(storage: AsyncStorageInterface) {
this.storage = storage;
}

getItem(key: string): string | Promise<string | null> | null {
getItem(key: string): Promise<string | null> {
return this.storage.getItem(key);
}

removeItem(key: string): void | Promise<void> {
removeItem(key: string): Promise<void> {
return this.storage.removeItem(key);
}

setItem(key: string, value: string): void | Promise<void> {
setItem(key: string, value: string | null): Promise<void> {
return this.storage.setItem(key, value);
}
}

interface AsyncStorageInterface {
// Actual type definition: https://github.com/react-native-async-storage/async-storage/blob/master/types/index.d.ts
getItem(key: string): Promise<string | null>;
setItem(key: string, value: string | null): Promise<void>;
removeItem(key: string): Promise<void>;
}
18 changes: 12 additions & 6 deletions src/storageWrappers/IonicStorageWrapper.ts
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>;
}
19 changes: 13 additions & 6 deletions src/storageWrappers/LocalForageWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { PersistentStorage } from '../types';

export class LocalForageWrapper implements PersistentStorage<string | object> {
// Actual type definition: https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts#L17
export class LocalForageWrapper
implements PersistentStorage<string | object | null> {
private storage;

constructor(storage: any) {
constructor(storage: LocalForageInterface) {
this.storage = storage;
}

getItem(key: string): string | Promise<string | null> | null {
getItem(key: string): Promise<string | null> {
return this.storage.getItem(key);
}

removeItem(key: string): void | Promise<void> {
removeItem(key: string): Promise<void> {
return this.storage.removeItem(key);
}

setItem(key: string, value: string): void | Promise<void> {
setItem(key: string, value: string | object | null): Promise<void> {
return new Promise((resolve, reject) => {
this.storage
.setItem(key, value)
Expand All @@ -25,3 +25,10 @@ export class LocalForageWrapper implements PersistentStorage<string | object> {
});
}
}

interface LocalForageInterface {
// Actual type definition: https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts#L17
getItem(key: string): Promise<string | null>;
setItem(key: string, value: string | object | null): Promise<void>;
removeItem(key: string): Promise<void>;
}
25 changes: 18 additions & 7 deletions src/storageWrappers/LocalStorageWrapper.ts
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;
}
25 changes: 17 additions & 8 deletions src/storageWrappers/MMKVStorageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,30 @@ import { PersistentStorage } from '../types';
* });
*
*/
export class MMKVStorageWrapper implements PersistentStorage<string> {
// Actual type definition: https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/index.d.ts#L27
export class MMKVStorageWrapper
implements PersistentStorage<string | null | undefined> {
private storage;

constructor(storage: any) {
constructor(storage: MMKVStorageInterface) {
this.storage = storage;
}

getItem(key: string): string | Promise<string | null> | null {
getItem(key: string): Promise<string | null | undefined> {
return this.storage.getItem(key);
}

removeItem(key: string): void | Promise<void> {
removeItem(key: string): Promise<void> {
return new Promise((resolve, reject) => {
this.storage
.removeItem(key)
// Ensure the removeItem is thenable, even if it's not, by wrapping it to Promise.resolve
// The MMKV storage's removeItem is synchronous since 0.5.7, this Promise wrap allows backward compatibility
// https://stackoverflow.com/a/27746324/2078771
Promise.resolve(this.storage.removeItem(key))
.then(() => resolve())
.catch(() => reject());
});
}

setItem(key: string, value: string): void | Promise<void> {
setItem(key: string, value: string | null | undefined): Promise<void> {
return new Promise((resolve, reject) => {
this.storage
.setItem(key, value)
Expand All @@ -41,3 +43,10 @@ export class MMKVStorageWrapper implements PersistentStorage<string> {
});
}
}

interface MMKVStorageInterface {
// Actual type definition: https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/index.d.ts#L27
getItem(key: string): Promise<string | null | undefined>;
setItem(key: string, value: string): Promise<boolean | undefined>;
removeItem(key: string): boolean | undefined | Promise<boolean | undefined>;
}
25 changes: 18 additions & 7 deletions src/storageWrappers/SessionStorageWrapper.ts
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;
}

0 comments on commit 56b1c55

Please sign in to comment.