Feature-rich proxy wrapper for browser storage with serialization, reactivity, expiration, and one-time values. Works with localStorage, sessionStorage, and asy
Feature-rich proxy wrapper for browser storage with serialization, reactivity, expiration, and one-time values. Works with localStorage, sessionStorage, and asy
…shell npm install stokado
### Proxy
```js
import { createProxyStorage } from 'stokado'
const storage = createProxyStorage(localStorage)
storage.getItem('test')
…
js
const storage = createProxyStorage(localStorage)
storage.test = 'hello stokado'
storage.test // 'hello stokado'
delete storage.testThe storage also have the same methods and properties: key(), getItem(), setItem(), removeItem(), clear() and length.
Keep the type of storage value unchanged
// number
storage.test = 0
storage.test === 0
// boolean
storage.test = false
storage.test === false
// undefined
storage.test = undefined
storage.test === undefined
// null
storage.test = null
storage.test === null
// object
storage.test = { hello: 'world' }
storage.test.hello === 'stokado'
// array
storage.test = ['hello']
storage.test.push('stokado')
storage.test.length // 2
// Date
storage.test = new Date('2000-01-01T00:00:00.000Z')
storage.test.getTime() === 946684800000
// RegExp
storage.test = /d(b+)d/g
storage.test.test('cdbbdbsbz')
// function
storage.test = function () {
return 'hello stokado!'
}
storage.test() === 'hello stokado!'Subscribe to value changes
storage.on(key, callback)
storage.once(key, callback)
storage.off([[key], callback])key: the name of the item to subscribe to. Support obj.a for Object and list[0] for Array, and also Array length.callback: the function to call when the item is changed. Includes newValue and oldValue.Tips: For off, if a callback exists, it removes the trigger of the specified callback; otherwise, it removes all callbacks bound to the key; if the key is empty, it removes all listening callbacks.
Set expires for items
storage.setExpires(key, expires)
storage.getExpires(key)
storage.removeExpires(key)key: the name of the item to set expires.expires: accept string、number and Date.Get the value once, which can be used for communication through storage.
storage.setDisposable(key)key:the name of the item to set disposable.Get expires and disposable configuration information for the specified item
storage.getOptions(key)Set expires and disposable using setItem
storage.setItem(key, value, { expires, disposable })Monitor storage usage and get notified when approaching limits
import { createProxyStorage, MB } from 'stokado'
const storage = createProxyStorage(localStorage, {
quota: 5 * MB,
onQuotaExceeded({ current, limit, key }) {
console.warn(`Storage quota exceeded: ${current}/${limit} bytes, key: "${key}"`)
return false
}
})
storage.getUsage()
await storage.ready
…
js
import { createProxyStorage } from 'stokado'
import { cookieStorage } from 'stokado/presets/cookie'
const storage = createProxyStorage(cookieStorage)import { createProxyStorage } from 'stokado'
import { wechatStorage } from 'stokado/presets/wechat'
const storage = createProxyStorage(wechatStorage)Async version:
import { wechatStorageAsync } from 'stokado/presets/wechat'
const storage = createProxyStorage(wechatStorageAsync)import { douyinStorage, douyinStorageAsync } from 'stokado/presets/douyin'import { alipayStorage, alipayStorageAsync } from 'stokado/presets/alipay'import { uniStorage, uniStorageAsync } from 'stokado/presets/uni-app'React Native requires injecting the AsyncStorage instance:
import AsyncStorage from '@react-native-async-storage/async-storage'
import { createProxyStorage } from 'stokado'
import { createReactNativeStorage } from 'stokado/presets/react-native'
const storage = createProxyStorage(createReactNativeStorage(AsyncStorage))import { createProxyStorage } from 'stokado'
import { memoryStorage } from 'stokado/presets/node'
const storage = createProxyStorage(memoryStorage)localForage provides the same API as localStorage, it can be used in conjunction with stokado.
import localForage from 'localforage'
import { createProxyStorage } from 'stokado'
const local = createProxyStorage(localForage, { channel: 'localForage' })However, localForage uses an async API, it needs to be called using Promise.
await (local.test = 'hello localForage')
// or
await local.setItem('test', 'hello localForage')You can create multiple instances of localForage that point to different stores using createInstance.
const store = localforage.createInstance({
name: 'nameHere'
})
const proxyStore = createProxyStorage(store, { channel: 'store' })
const otherStore = localforage.createInstance({
name: 'otherName'
})
const proxyOtherStore = createProxyStorage(otherStore, { channel: 'otherStore' })stokado is a localStorage wrapper, browser storage proxy, web storage library, and storage utility for JavaScript. It provides localStorage serialization, storage reactivity, storage subscription, storage expiration, storage quota alert, cross-tab storage sync, and async storage support. Alternatives to store2, store.js, lscache, localForage, and idb-keyval. Works with localStorage, sessionStorage, localForage, and any storage-like object.
No open issues yet, or sync has not completed.