[api] Add PersistentStorage
Wrapper for LocalStorage on browsers and Storage plugin on capacitor
This commit is contained in:
parent
88dd96c937
commit
6769e18ffa
|
@ -4,5 +4,6 @@ export * from './src/stores/';
|
|||
|
||||
export * from './src/utils/datetime';
|
||||
export * from './src/utils/permission';
|
||||
export * from './src/utils/persistent';
|
||||
export * from './src/utils/validators';
|
||||
export * from './src/utils/misc';
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
import { LocalStorage, Platform } from 'quasar';
|
||||
import { Storage } from '@capacitor/storage';
|
||||
|
||||
type GetReturn = Date | number | boolean | string | object;
|
||||
|
||||
export class PersistentStorage {
|
||||
static clear() {
|
||||
if (Platform.is.capacitor) return Storage.clear();
|
||||
else return Promise.resolve(LocalStorage.clear());
|
||||
}
|
||||
|
||||
static remove(key: string) {
|
||||
if (Platform.is.capacitor) return Storage.remove({ key: key });
|
||||
else return Promise.resolve(LocalStorage.remove(key));
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
static set(key: string, value: any) {
|
||||
if (Platform.is.capacitor) return Storage.set({ key, value: JSON.stringify(value) });
|
||||
else return Promise.resolve(LocalStorage.set(key, value));
|
||||
}
|
||||
|
||||
static get<T extends GetReturn>(key: string) {
|
||||
if (Platform.is.capacitor)
|
||||
return Storage.get({ key }).then((v) =>
|
||||
v.value === null ? null : (JSON.parse(v.value) as T)
|
||||
);
|
||||
else return Promise.resolve(LocalStorage.getItem<T>(key));
|
||||
}
|
||||
|
||||
static keys() {
|
||||
if (Platform.is.capacitor) return Storage.keys().then((v) => v.keys);
|
||||
else return Promise.resolve(LocalStorage.getAllKeys());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue