diff --git a/docs/storage-providers.md b/docs/storage-providers.md index d29e2ad1..927e8cc5 100644 --- a/docs/storage-providers.md +++ b/docs/storage-providers.md @@ -9,15 +9,16 @@ `apollo3-cache-persist` provides wrappers for the following storage providers, with no additional dependencies: -| Storage provider | Platform | Wrapper class | -| ------------------------------------------------------------------------------- | -------------- | ----------------------- | -| [`AsyncStorage`](https://github.com/react-native-async-storage/async-storage)\* | React Native | `AsyncStorageWrapper` | -| `window.localStorage` | web | `LocalStorageWrapper` | -| `window.sessionStorage` | web | `SessionStorageWrapper` | -| [`localForage`](https://github.com/localForage/localForage) | web | `LocalForageWrapper` | -| [`Ionic storage`](https://ionicframework.com/docs/building/storage) | web and mobile | `IonicStorageWrapper` | -| [`MMKV Storage`](https://github.com/ammarahm-ed/react-native-mmkv-storage) | React Native | `MMKVStorageWrapper` | -| [`MMKV`](https://github.com/mrousavy/react-native-mmkv) | React Native | `MMKVWrapper` | +| Storage provider | Platform | Wrapper class | +|---------------------------------------------------------------------------------|----------------|-------------------------------| +| [`AsyncStorage`](https://github.com/react-native-async-storage/async-storage)\* | React Native | `AsyncStorageWrapper` | +| `window.localStorage` | web | `LocalStorageWrapper` | +| `window.sessionStorage` | web | `SessionStorageWrapper` | +| [`localForage`](https://github.com/localForage/localForage) | web | `LocalForageWrapper` | +| [`Ionic storage`](https://ionicframework.com/docs/building/storage) | web and mobile | `IonicStorageWrapper` | +| [`MMKV Storage`](https://github.com/ammarahm-ed/react-native-mmkv-storage) | React Native | `MMKVStorageWrapper` | +| [`MMKV`](https://github.com/mrousavy/react-native-mmkv) | React Native | `MMKVWrapper` | +| [`Capacitor Preferences`](https://capacitorjs.com/docs/apis/preferences) | Capacitor | `CapacitorPreferencesWrapper` | ## Redux Persist Providers diff --git a/src/storageWrappers/CapacitorPreferencesWrapper.ts b/src/storageWrappers/CapacitorPreferencesWrapper.ts new file mode 100644 index 00000000..dc084321 --- /dev/null +++ b/src/storageWrappers/CapacitorPreferencesWrapper.ts @@ -0,0 +1,35 @@ +import {PersistentStorage} from "../types"; + +export class CapacitorPreferencesWrapper implements PersistentStorage { + private storage; + + constructor(storage: CapacitorPreferencesInterface) { + this.storage = storage; + } + + getItem(key: string): Promise { + return this.storage.get({key: key}).then(r => r.value); + } + + removeItem(key: string): Promise { + return this.storage.remove({key: key}); + } + + setItem(key: string, value: string | null): Promise { + if (value) { + // Capacitor Preferences does not support nullable values + return this.storage.set({key: key, value: value}); + } else { + return this.removeItem(key); + } + } +} + +interface CapacitorPreferencesInterface { + // Actual type definition: https://capacitorjs.com/docs/apis/preferences#api + get(options: { key: string }): Promise<{ value: string | null }>; + + set(options: { key: string, value: string }): Promise; + + remove(options: { key: string }): Promise; +} diff --git a/src/storageWrappers/index.ts b/src/storageWrappers/index.ts index 38383427..974c0108 100644 --- a/src/storageWrappers/index.ts +++ b/src/storageWrappers/index.ts @@ -5,3 +5,4 @@ export * from './LocalStorageWrapper'; export * from './MMKVStorageWrapper'; export * from './MMKVWrapper'; export * from './SessionStorageWrapper'; +export * from './CapacitorPreferencesWrapper';