Skip to content

Commit

Permalink
fix(types): loosen wrapper type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
wodCZ committed Aug 1, 2021
1 parent 15d8b40 commit 1b6a249
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/storageWrappers/AsyncStorageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ import { PersistentStorage } from '../types';
* });
*
*/
export class AsyncStorageWrapper implements PersistentStorage<string> {
export class AsyncStorageWrapper implements PersistentStorage<any> {
// Actual type definition: https://github.com/react-native-async-storage/async-storage/blob/master/types/index.d.ts
private storage;

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

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

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

setItem(key: string, value: string): void | Promise<void> {
return this.storage.setItem(key, value);
setItem(key: string, value: any): void | Promise<void> {
return this.storage.setItem(
key,
typeof value === 'string' ? value : value.toString(),
);
}
}
11 changes: 7 additions & 4 deletions src/storageWrappers/IonicStorageWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { PersistentStorage } from '../types';

export class IonicStorageWrapper implements PersistentStorage<string> {
export class IonicStorageWrapper implements PersistentStorage<any> {
// Actual type definition: https://github.com/ionic-team/ionic-storage/blob/main/src/storage.ts#L102
private storage;

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

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

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

setItem(key: string, value: string): void | Promise<void> {
return this.storage.set(key, value);
setItem(key: string, value: any): void | Promise<void> {
return this.storage.set(
key,
typeof value === 'string' ? value : value.toString(),
);
}
}
2 changes: 1 addition & 1 deletion src/storageWrappers/LocalForageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class LocalForageWrapper implements PersistentStorage<string | object> {
return this.storage.removeItem(key);
}

setItem(key: string, value: string): void | Promise<void> {
setItem(key: string, value: string | object): void | Promise<void> {
return new Promise((resolve, reject) => {
this.storage
.setItem(key, value)
Expand Down
11 changes: 7 additions & 4 deletions src/storageWrappers/LocalStorageWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { PersistentStorage } from '../types';

export class LocalStorageWrapper implements PersistentStorage<string> {
export class LocalStorageWrapper implements PersistentStorage<any> {
// Actual type definition: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L15286
private storage;

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

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

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

setItem(key: string, value: string): void | Promise<void> {
return this.storage.setItem(key, value);
setItem(key: string, value: any): void | Promise<void> {
return this.storage.setItem(
key,
typeof value === 'string' ? value : value.toString(),
);
}
}
8 changes: 4 additions & 4 deletions src/storageWrappers/MMKVStorageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { PersistentStorage } from '../types';
* });
*
*/
export class MMKVStorageWrapper implements PersistentStorage<string> {
export class MMKVStorageWrapper implements PersistentStorage<any> {
// Actual type definition: https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/index.d.ts#L27
private storage;

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

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

Expand All @@ -32,10 +32,10 @@ export class MMKVStorageWrapper implements PersistentStorage<string> {
});
}

setItem(key: string, value: string): void | Promise<void> {
setItem(key: string, value: any): void | Promise<void> {
return new Promise((resolve, reject) => {
this.storage
.setItem(key, value)
.setItem(key, typeof value === 'string' ? value : value.toString())
.then(() => resolve())
.catch(() => reject());
});
Expand Down
11 changes: 7 additions & 4 deletions src/storageWrappers/SessionStorageWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { PersistentStorage } from '../types';

export class SessionStorageWrapper implements PersistentStorage<string> {
export class SessionStorageWrapper implements PersistentStorage<any> {
// Actual type definition: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L15286
private storage;

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

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

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

setItem(key: string, value: string): void | Promise<void> {
return this.storage.setItem(key, value);
setItem(key: string, value: any): void | Promise<void> {
return this.storage.setItem(
key,
typeof value === 'string' ? value : value.toString(),
);
}
}

0 comments on commit 1b6a249

Please sign in to comment.