Skip to content

Storage Utilities

Standalone functions for inspecting and bulk-managing a storage backend, independent of any single useStorage() instance.

ts
import { getStorageQuota, exportStorage, importStorage, clearStorage } from 'vue-storage-kit'

getStorageQuota

() => Promise<StorageQuota>

Reads the browser's storage quota via navigator.storage.estimate(). Returns { quota: 0, usage: 0, usagePercent: 0 } in environments without the Storage API (older browsers, SSR).

ts
interface StorageQuota {
  quota: number // total bytes available, browser-estimated
  usage: number // bytes currently used
  usagePercent: number // usage / quota, rounded to 2 decimals
}
ts
const { usage, quota, usagePercent } = await getStorageQuota()
console.log(`${usagePercent}% of storage used (${usage} / ${quota} bytes)`)

exportStorage

(target?: StorageTarget, prefix?: string) => Promise<StorageSnapshot>

Reads every key (optionally filtered by prefix) from target (default 'local') into a plain object of raw stored strings — a snapshot you can serialize, download, or hand to importStorage() later.

ts
type StorageSnapshot = Record<string, string>
ts
const snapshot = await exportStorage('local', 'myapp:')
localStorage.setItem('backup', JSON.stringify(snapshot)) // or download it, send it somewhere, etc.

importStorage

(snapshot: StorageSnapshot, target?: StorageTarget, options?: { overwrite?: boolean }) => Promise<void>

Writes every key in snapshot back into target (default 'local'). With overwrite: false (default true), a key that already exists is left untouched instead of being replaced.

ts
const snapshot = JSON.parse(localStorage.getItem('backup')!)
await importStorage(snapshot, 'local', { overwrite: false })

clearStorage

(target?: StorageTarget, prefix?: string) => Promise<void>

Removes every key (optionally filtered by prefix) from target (default 'local'). Unlike a single useStorage()'s remove(), this clears everything matching at once, independent of whether a composable for those keys is currently mounted.

ts
// Wipe every key this app owns, e.g. on logout
await clearStorage('local', 'myapp:')