1.2.0-beta โ€ข Published 8 months ago

@lonewolfdays/storage-manager v1.2.0-beta

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

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, and cookies
  • โœ… 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

MethodDescription
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

MethodDescription
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