This document serves as an internal reference for understanding the Stash library, how it works, and the decisions behind its implementation. Unlike README.md, this file is for my personal use to track what I have done and why.
The Stash library provides a unified API to manage three types of storage:
localStoragesessionStoragecookies
Instead of manually writing localStorage.setItem() or handling cookies separately, this library provides a cleaner abstraction.
- Unified API for setting, getting, removing, and clearing storage.
- Handles JSON parsing internally to prevent errors.
- Supports expiration for cookies (optional
expiryDaysparameter). - Works only in the browser (prevents SSR-related crashes by checking
window). - Singleton pattern ensures only one instance is used throughout the app.
- Stores the given
valuein the selected storage (local,session, orcookie). - Converts the
valueto JSON before saving. - Handles cookie expiration if
expiryDaysis provided. - Uses a switch-case for scalability and readability.
- Retrieves data from storage and parses it back to its original type.
- Uses a private helper method
parseValue()to safely handle JSON parsing. - For cookies, it loops through
document.cookieto find the matching key.
- Removes the key from the specified storage.
- For cookies, sets an expired timestamp to delete it.
- Clears all entries in the selected storage.
- Uses a private
clearCookies()method to remove all stored cookies.
isBrowser(): Ensures the code runs only in the browser.parseValue(): Safely parses JSON values.getCookie(): Extracts a single cookie.clearCookies(): Deletes all cookies.
- Scalability: Easier to extend when adding more storage options.
- Performance:
switchis optimized for multi-branch conditions. - Readability: More structured and easier to navigate.
- Encapsulation: Prevents external access to internal logic.
- Code Reusability: Avoids repeating
JSON.parse()in multiple places. - Improved Readability: Keeps the main functions clean and concise.
- Prevents unnecessary multiple instances.
- Ensures consistent state throughout the application.
- Simplifies usage by always using
useStash().
- Add TypeScript Generics for stricter type safety.
- Support IndexedDB for larger storage needs.
- Option to encrypt stored data for added security.
- Improve error handling with custom exceptions.
- Everything works as expected, but cookie storage needs more testing across different browsers.
- Need to test how this handles storage limits (e.g.,
QuotaExceededErrorin localStorage). - Potential optimization: use a cache mechanism to reduce frequent
JSON.parse()calls.
stash.tsβ Core library implementationREADME.mdβ Public-facing documentation for usagetests/β Unit tests for different storage types
const stash = useStash();
stash.set("local", "username", "Ganesh Sharma");
const user = stash.get<string>("local", "username");
console.log(user); // Outputs: Ganesh Sharma- Changed module from CommonJS to ESNext for better compatibility with React.
- Target set to ES2016 instead of ES6 for modern JavaScript features.
- Optimized code by replacing
if-elseblocks withswitch-caseto improve readability and performance. - Handles QuotaExceededError in
localStorageto prevent crashes. - Red Hat Dependency Analytics was the cause of recursive
node_modulesinstallations, now removed.
- Initialize the project:
npm init -y
- Install TypeScript:
npm i typescript --save-dev
- Initialize TypeScript configuration:
npm --init
- Compile TypeScript:
npx tsc
This file is for internal tracking only. If I revisit this project after months, this document will remind me exactly how everything works! π