1.0.3 • Published 6 months ago

bolsa v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

šŸ” 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

  1. State updates trigger encryptData() before saving.
  2. Data is stored in IndexedDB (Web) or AsyncStorage (React Native).
  3. 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!

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago