This package provides a flexible client-side storage manager for web applications. It allows you to easily manage data in different types of storage such as localStorage
, sessionStorage
, memoryStorage
, and cookieStorage
.
You can install this package via npm:
npm install client-storage-utility
or via yarn:
yarn add client-storage-utility
import createClientStorage from 'client-storage-utility';
const clientStorage = createClientStorage();
By default, the storage type is sessionStorage
. You can change it as needed:
clientStorage.setStorage('localStorage');
To store an item:
clientStorage.setItem('key1', 'value1');
To retrieve an item:
const value = clientStorage.getItem('key1');
console.log(value); // Output: value1
To remove an item:
clientStorage.removeItem('key1');
You can also specify allowed keys when creating the storage:
const allowedKeys = ['key1', 'key2', 'key3'];
const clientStorage = createClientStorage(allowedKeys);
This ensures that only specified keys can be used for storage operations.
You can specify allowed keys when creating the storage as const
:
const allowedKeys = ['key1', 'key2', 'key3'] as const;
const clientStorage = createClientStorage(allowedKeys);
This ensures that only specified keys can be used for storage operations.
Alternatively, you can use generics to specify allowed keys:
const clientStorage = createClientStorage<'key1' | 'key2' | 'key3'>();
This approach provides type safety for allowed keys.
localStorage
: Persistent storage that lasts across browser sessions.sessionStorage
: Storage that is cleared when the page session ends.memoryStorage
: In-memory storage useful for temporary data.cookieStorage
: Storage using browser cookies.
You can also create custom storage types by extending the StorageInterface
.
clientStorage.setStorage('localStorage');
clientStorage.setItem('username', 'JohnDoe');
const username = clientStorage.getItem('username');
console.log(username); // Output: JohnDoe
clientStorage.removeItem('username');
This package is licensed under the MIT License. See the LICENSE file for details.