Enhanced Web Storage API
you can install the package through npm runnig:
npm install secchiojs
import { getBucket } from 'secchiojs';
type UserPrefs = {
name: string;
email: string;
mode: number;
};
const [getPrefs, updatePrefs, deletePrefs] = getBucket<UserPrefs>(
'user-prefs',
{
version: 1,
defaultValue: {},
storage: localStorage,
expire: 3600,
}
);
const userPrefs = getPrefs();
updatePrefs({
...userPrefs,
mode: 2,
});
deletePrefs();
import { getBucket } from 'secchiojs';
const [getPrefs, updatePrefs, deletePrefs] = getBucket('user-prefs', {
version: 1,
defaultValue: {},
storage: localStorage,
expire: 3600,
});
const userPrefs = getPrefs();
updatePrefs({
...userPrefs,
mode: 2,
});
deletePrefs();
name
string The name of the bucket. It will be used as key when saving into the storage.options
BucketOptions The bucket options.
The function returns an array containing get, set and remove methods.
export type Bucket<T> = [
// get
() => T,
// set
(value: T) => void,
// remove
() => void
];
storage?
Storage An instance of a storage implementing the Web Storage interface (eg. localStorage, sessionStorage).version?
number The version of the bucket.expire?
number A number that indicates the expiration of the data. Default is 0, which means no expiration.defaultValue?
any The default value returned if the data doesn't exist, or is expired or is from a different verision.