A minimal API wrapper for localStorages, inspired by tsironis/lockr and marcuswestin/store.js. It is written fully with Typescript.
- 🕒 Familiar
store
API & patterns - 🔥 TypeScript
$ npm install @zcorky/storage
import storage from '@zcorky/storage';
// set sets the value of the given key
// suport maxAge, unit: ms
storage.set('user', { name:'Marcus' }, 30 * 1000)
// get returns the value of the given key
storage.get('user')
// remove removes the value of the given key
storage.remove('user')
// clear clears the storage
storage.clear()
// keys returns the keys of the storage
storage.keys()
// getAll returns the all items of the storage
storage.getAll()
// has checks whether the given key exists
storage.has('user')
// built-in drivers
// local - use localStorage
// session - use sessionStorage
// indexeddb - use IndexedDB
// service - use service with http request
storage.setDriver('session')
// custom prefix
storage.setPrefix('my-prefix-')
Default storage is a global storage with singleton pattern.
If you want to create a new storage, you can use storage.create()
method.
const storage = storage.create(options);
import { StorageDriver } from '@zcorky/storage';
// 1. Create a class implements StorageDriver
export class CustomStorage extends StorageDriver {
...
}
// 2. Register the driver
storage.setDriver('custom', CustomStorage)
- See the detailed API Reference.
MIT © Moeover