diff --git a/src/storageWrappers/AsyncStorageWrapper.ts b/src/storageWrappers/AsyncStorageWrapper.ts index 3eeb4da9..c8ef5809 100644 --- a/src/storageWrappers/AsyncStorageWrapper.ts +++ b/src/storageWrappers/AsyncStorageWrapper.ts @@ -12,23 +12,29 @@ import { PersistentStorage } from '../types'; * }); * */ -export class AsyncStorageWrapper implements PersistentStorage { - // Actual type definition: https://github.com/react-native-async-storage/async-storage/blob/master/types/index.d.ts +export class AsyncStorageWrapper implements PersistentStorage { private storage; - constructor(storage: any) { + constructor(storage: AsyncStorageInterface) { this.storage = storage; } - getItem(key: string): string | Promise | null { + getItem(key: string): Promise { return this.storage.getItem(key); } - removeItem(key: string): void | Promise { + removeItem(key: string): Promise { return this.storage.removeItem(key); } - setItem(key: string, value: string): void | Promise { + setItem(key: string, value: string | null): Promise { 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; + setItem(key: string, value: string | null): Promise; + removeItem(key: string): Promise; +} diff --git a/src/storageWrappers/IonicStorageWrapper.ts b/src/storageWrappers/IonicStorageWrapper.ts index 27ed0244..f9bb0c7b 100644 --- a/src/storageWrappers/IonicStorageWrapper.ts +++ b/src/storageWrappers/IonicStorageWrapper.ts @@ -1,22 +1,28 @@ import { PersistentStorage } from '../types'; -export class IonicStorageWrapper implements PersistentStorage { - // Actual type definition: https://github.com/ionic-team/ionic-storage/blob/main/src/storage.ts#L102 +export class IonicStorageWrapper implements PersistentStorage { private storage; - constructor(storage: any) { + constructor(storage: IonicStorageInterface) { this.storage = storage; } - getItem(key: string): string | Promise | null { + getItem(key: string): Promise { return this.storage.get(key); } - removeItem(key: string): void | Promise { + removeItem(key: string): Promise { return this.storage.remove(key); } - setItem(key: string, value: string): void | Promise { + setItem(key: string, value: string | null): Promise { 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; + set(key: string, value: string | null): Promise; + remove(key: string): Promise; +} diff --git a/src/storageWrappers/LocalForageWrapper.ts b/src/storageWrappers/LocalForageWrapper.ts index 19e988ab..1c3e24e9 100644 --- a/src/storageWrappers/LocalForageWrapper.ts +++ b/src/storageWrappers/LocalForageWrapper.ts @@ -1,22 +1,22 @@ import { PersistentStorage } from '../types'; -export class LocalForageWrapper implements PersistentStorage { - // Actual type definition: https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts#L17 +export class LocalForageWrapper + implements PersistentStorage { private storage; - constructor(storage: any) { + constructor(storage: LocalForageInterface) { this.storage = storage; } - getItem(key: string): string | Promise | null { + getItem(key: string): Promise { return this.storage.getItem(key); } - removeItem(key: string): void | Promise { + removeItem(key: string): Promise { return this.storage.removeItem(key); } - setItem(key: string, value: string): void | Promise { + setItem(key: string, value: string | object | null): Promise { return new Promise((resolve, reject) => { this.storage .setItem(key, value) @@ -25,3 +25,10 @@ export class LocalForageWrapper implements PersistentStorage { }); } } + +interface LocalForageInterface { + // Actual type definition: https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts#L17 + getItem(key: string): Promise; + setItem(key: string, value: string | object | null): Promise; + removeItem(key: string): Promise; +} diff --git a/src/storageWrappers/LocalStorageWrapper.ts b/src/storageWrappers/LocalStorageWrapper.ts index 5fb8ea8e..6238608c 100644 --- a/src/storageWrappers/LocalStorageWrapper.ts +++ b/src/storageWrappers/LocalStorageWrapper.ts @@ -1,22 +1,33 @@ import { PersistentStorage } from '../types'; -export class LocalStorageWrapper implements PersistentStorage { - // Actual type definition: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L15286 +export class LocalStorageWrapper implements PersistentStorage { private storage; - constructor(storage: any) { + constructor(storage: LocalStorageInterface) { this.storage = storage; } - getItem(key: string): string | Promise | null { + getItem(key: string): string | null { return this.storage.getItem(key); } - removeItem(key: string): void | Promise { + removeItem(key: string): void { return this.storage.removeItem(key); } - setItem(key: string, value: string): void | Promise { - 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; +} diff --git a/src/storageWrappers/MMKVStorageWrapper.ts b/src/storageWrappers/MMKVStorageWrapper.ts index ef983b04..5f6409fd 100644 --- a/src/storageWrappers/MMKVStorageWrapper.ts +++ b/src/storageWrappers/MMKVStorageWrapper.ts @@ -11,28 +11,30 @@ import { PersistentStorage } from '../types'; * }); * */ -export class MMKVStorageWrapper implements PersistentStorage { - // Actual type definition: https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/index.d.ts#L27 +export class MMKVStorageWrapper + implements PersistentStorage { private storage; - constructor(storage: any) { + constructor(storage: MMKVStorageInterface) { this.storage = storage; } - getItem(key: string): string | Promise | null { + getItem(key: string): Promise { return this.storage.getItem(key); } - removeItem(key: string): void | Promise { + removeItem(key: string): Promise { 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 { + setItem(key: string, value: string | null | undefined): Promise { return new Promise((resolve, reject) => { this.storage .setItem(key, value) @@ -41,3 +43,10 @@ export class MMKVStorageWrapper implements PersistentStorage { }); } } + +interface MMKVStorageInterface { + // Actual type definition: https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/index.d.ts#L27 + getItem(key: string): Promise; + setItem(key: string, value: string): Promise; + removeItem(key: string): boolean | undefined | Promise; +} diff --git a/src/storageWrappers/SessionStorageWrapper.ts b/src/storageWrappers/SessionStorageWrapper.ts index 2b5ebf0d..fd1bb303 100644 --- a/src/storageWrappers/SessionStorageWrapper.ts +++ b/src/storageWrappers/SessionStorageWrapper.ts @@ -1,22 +1,33 @@ import { PersistentStorage } from '../types'; -export class SessionStorageWrapper implements PersistentStorage { - // Actual type definition: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L15286 +export class SessionStorageWrapper implements PersistentStorage { private storage; - constructor(storage: any) { + constructor(storage: SessionStorageInterface) { this.storage = storage; } - getItem(key: string): string | Promise | null { + getItem(key: string): string | null { return this.storage.getItem(key); } - removeItem(key: string): void | Promise { + removeItem(key: string): void { return this.storage.removeItem(key); } - setItem(key: string, value: string): void | Promise { - 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; +}