1.2.0-beta โข Published 8 months ago
@lonewolfdays/storage-manager v1.2.0-beta
Storage Manager for browser based javascript frameworks
A universal browser storage manager for Next.js projects with support for localStorage, sessionStorage, and cookies, with optional AES encryption.
๐ฆ Installation
npm install @lonewolfdays/storage-manager
# or
yarn add @lonewolfdays/storage-managerโจ Features
- ๐ Optional AES encryption support
- ๐งฑ Unified API for
localStorage,sessionStorage, andcookies - โ TypeScript support (Syntactic Sugar)
- โ Safe for use in client components
- ๐ฆ Cookie operations based on
js-cookie - ๐ Modular import and usage flexibility
Usage
๐ Set Secret Key for Encryption (Optional)
import { StorageManager } from '@lonewolfdays/storage-manager';
// Only needed if you want encryption
StorageManager.local.setSecretKey('your-secret-key');
StorageManager.session.setSecretKey('your-secret-key');
StorageManager.cookie.setSecretKey('your-secret-key');โ ๏ธ If you skip setting a secret key, values will be stored in plain text.
๐ฆ LocalStorage Example
StorageManager.local.setItem('key', 'value');
const value = StorageManager.local.getItem('key');
StorageManager.local.removeItem('key');๐ SessionStorage Example
StorageManager.session.setItem('sessionKey', 'sessionValue');
const value = StorageManager.session.getItem('sessionKey');
StorageManager.session.removeItem('sessionKey');๐ช Cookie Example (Client-side Only)
StorageManager.cookie.setCookie('cookieKey', 'cookieValue', {
path: '/',
maxAge: 60 * 60 * 24, // 1 day | in seconds
});
const cookie = StorageManager.cookie.getCookie('cookieKey');
StorageManager.cookie.deleteCookie('cookieKey');๐งฉ Modular Import (Optional)
If you want only one type of storage:
import { ClientLocalStorageManager } from '@lonewolfdays/storage-manager';
ClientLocalStorageManager.setItem('foo', 'bar');โ๏ธ Use in Next.js Client Components
'use client';
import { StorageManager } from '@lonewolfdays/storage-manager';
StorageManager.local.setItem('theme', 'dark');
const theme = StorageManager.local.getItem('theme');โ The package is modular, so only the relevant storage runs in the browser.
๐ Environment Variable Example for Encryption Key
if (typeof window !== 'undefined') {
const secret = process.env.NEXT_PUBLIC_STORAGE_SECRET;
StorageManager.local.setSecretKey(secret!);
}๐งพ API Reference
StorageManager.local / StorageManager.session
| Method | Description |
|---|---|
setSecretKey(key: string) | Sets the AES encryption key |
setItem(key, value) | Stores the value |
getItem(key) | Retrieves the value |
removeItem(key) | Deletes the key |
StorageManager.cookie
| Method | Description |
|---|---|
setSecretKey(key: string) | Sets the AES encryption key |
setCookie(key, value, options?) | Sets a cookie |
getCookie(key) | Gets a cookie value |
deleteCookie(key, options?) | Deletes the cookie |
๐ License
MIT ยฉ Manas Saha Roy