bolsa v1.0.3
š Bolsa: A Lightweight State Management Library with Persistence & Middleware
Bolsa is a lightweight state management library written in TypeScript, offering:
ā
Global State Management
ā
Automatic Persistence (IndexedDB for Web, AsyncStorage for React Native)
ā
AES-256 Encryption using crypto-js
ā
Middleware Support for logging, validation, and more
ā
Minimal Dependencies & Blazing Fast Performance
š Installation
npm install bolsa
or
yarn add bolsa
š ļø Usage
1ļøā£ Create a Simple Store
import { createStore } from "bolsa";
const counter = createStore(0, "counter-key");
counter.subscribe(value => console.log("Counter Updated:", value));
counter.set(5); // Updates state & persists it
counter.update(n => n + 1); // Increments state
2ļøā£ Persistent User Store (Encrypted)
const userStore = createStore({ name: "Alice", age: 25 }, "user-store");
userStore.subscribe(user => console.log("User State:", user));
userStore.set({ name: "Bob", age: 30 }); // Stores encrypted user data
š Middleware Support
Middleware functions allow you to modify, log, or validate state changes before they are applied.
Adding Middleware
import { createStore, Middleware } from "bolsa";
const logger: Middleware<number> = (state, next) => {
console.log("Previous State:", state);
next(state);
console.log("New State:", state);
};
const preventNegative: Middleware<number> = (state, next) => {
if (state < 0) {
console.warn("Negative values not allowed!");
return;
}
next(state);
};
const counter = createStore(0, "counter-key", [logger, preventNegative]);
š Encryption & Persistence
This library encrypts state before saving it using AES-256-CBC with crypto-js
.
- Web: Uses IndexedDB for persistence.
- React Native: Uses AsyncStorage.
How it Works
- State updates trigger
encryptData()
before saving. - Data is stored in IndexedDB (Web) or AsyncStorage (React Native).
- When restoring,
decryptData()
decrypts the stored value.
š API Reference
createStore<T>(initialState: T, storageKey?: string, middlewares?: Middleware<T>[])
Creates a new store with optional persistence and middleware support.
Parameters:
initialState: T
- The initial state of the store.storageKey?: string
- Optional key for persistence.middlewares?: Middleware<T>[]
- Optional array of middleware functions.
Methods:
get(): T
- Returns the current state.set(newState: T): Promise<void>
- Updates the state.update(updater: (state: T) => T): Promise<void>
- Updates the state using a function.subscribe(callback: (state: T) => void): () => void
- Subscribes to state changes.use(middleware: Middleware<T>): void
- Adds a middleware function.
š Why Use EncryptedStore?
ā Fast & Lightweight (Minimal dependencies) ā Cross-Platform (Works on Web & React Native) ā Automatic Persistence (IndexedDB / AsyncStorage) ā AES-256 Encryption for secure data storage ā Middleware Support for enhanced control ā Simple API for ease of use
š License
This project is licensed under the MIT License.
š Contributing
We welcome contributions! Feel free to open an issue or submit a pull request.
ā Support
If you like this project, give it a ā on GitHub!