Storage
Storage type definitions related to the useAppStorage composable.
StorageType
A string literal type representing browser storage types.
Source
export type StorageType = 'localStorage' | 'sessionStorage'
Example
const storage: StorageType = 'localStorage'
StorageConfig<T>
The full configuration object type for the useAppStorage function.
Source
export interface StorageConfig<T = unknown> {
key: string
defaultValue: T
prefix: string
storage: StorageType
}
Properties
key: The unique key name for the storage entrydefaultValue: The default valueprefix: Key name prefix used to create a namespacestorage: Storage type, eitherlocalStorageorsessionStorage
StorageConfigInput<T>
The input configuration object type for the useAppStorage function, with prefix and storage as optional parameters.
Source
export type StorageConfigInput<T = unknown> = Partial<Omit<StorageConfig<T>, 'key' | 'defaultValue'>> & {
key: string
defaultValue: T
}
Example
import type { StorageConfigInput } from '@movk/core'
import { useAppStorage } from '@movk/core'
const config: StorageConfigInput<{ theme: string }> = {
key: 'user-preferences',
defaultValue: { theme: 'light' },
storage: 'localStorage', // optional
prefix: 'app' // optional
}
const storage = useAppStorage(config)
AppStorageReturn<T>
The return object interface for the useAppStorage function.
Source
export interface AppStorageReturn<T> {
state: Ref<T>
getItem: () => T
setItem: (value: T) => void
removeItem: () => void
}
Properties
state: Reactive ref synchronized with storage datagetItem: Reads data from storagesetItem: Sets a new value to storageremoveItem: Removes the entry from storage
Example
import type { AppStorageReturn } from '@movk/core'
import { useAppStorage } from '@movk/core'
const storage: AppStorageReturn<string> = useAppStorage({
key: 'my-key',
defaultValue: 'hello'
})
// Use reactive state
console.log(storage.state.value) // 'hello'
// Update value
storage.setItem('world')
// Read value
const value = storage.getItem() // 'world'
// Remove entry
storage.removeItem()
Changelog
No recent changes