@uekichinos/stash

Lightweight localStorage/sessionStorage wrapper with TTL expiry, TypeScript generics, namespace isolation, and version-based auto-wipe. Zero dependencies.
stash.set('token', 'abc123', { ttl: '1h' })
stash.get('token')
Installation
npm install @uekichinos/stash
Quick start
import { stash } from '@uekichinos/stash'
stash.set('user', { name: 'John' })
stash.get('user')
stash.set('session', data, { ttl: '30m' })
const auth = stash.namespace('auth')
auth.set('token', 'xyz', { ttl: '1h' })
API
stash.set(key, value, options?)
Stores a value in localStorage (default) or sessionStorage.
stash.set('key', value)
stash.set('key', value, { ttl: '1h' })
stash.set('key', value, { ttl: 5000 })
stash.set('key', value, { storage: 'session' })
stash.get<T>(key, options?)
Returns the stored value, or null if missing or expired. Expired keys are removed automatically on read.
stash.get('key')
stash.get<User>('user')
stash.get('key', { storage: 'session' })
stash.has(key, options?)
Returns true if the key exists and has not expired.
stash.has('token')
stash.remove(key, options?)
Deletes a key immediately.
stash.remove('token')
stash.ttl(key, options?)
Returns remaining TTL in milliseconds, null if the key has no TTL or doesn't exist.
stash.ttl('token')
stash.keys(options?)
Returns all non-expired keys managed by stash. Expired keys are removed automatically.
stash.set('a', 1)
stash.set('b', 2, { ttl: '1h' })
stash.keys()
const auth = stash.namespace('auth')
auth.set('token', 'xyz')
auth.keys()
stash.clear(options?)
Removes all keys managed by stash. Does not touch keys set outside of stash.
stash.clear()
stash.clear({ storage: 'session' })
stash.purge(options?)
Removes only expired keys. Useful for manual cleanup.
stash.purge()
stash.namespace(prefix, options?)
Returns a namespaced stash instance. Keys are stored as stash:{prefix}:{key}.
const auth = stash.namespace('auth')
auth.set('token', 'abc')
auth.get('token')
auth.clear()
With version — when version changes, all keys from older versions are wiped automatically:
const v1 = stash.namespace('app', { version: 1 })
v1.set('config', oldData)
const v2 = stash.namespace('app', { version: 2 })
v2.get('config')
TTL formats
'30s' | 30 seconds |
'5m' | 5 minutes |
'2h' | 2 hours |
'7d' | 7 days |
5000 | 5000 milliseconds |
TypeScript
All methods are fully typed. Use generics with get for typed retrieval:
interface User {
id: number
name: string
}
stash.set<User>('user', { id: 1, name: 'John' })
const user = stash.get<User>('user')
Via <script> tag (no bundler)
<script src="https://unpkg.com/@uekichinos/stash/dist/index.global.js"></script>
<script>
Stash.stash.set('key', 'value', { ttl: '1h' })
Stash.stash.get('key')
</script>
Storage backends
'local' (default) | localStorage — persists across sessions |
'session' | sessionStorage — cleared when tab closes |
stash.set('draft', form, { storage: 'session' })
stash.get('draft', { storage: 'session' })
License
MIT © uekichinos