browniejs
v1.0.0
Published
A lite getter/setter for cookies - because document.cookie is a nightmare!
Maintainers
Readme
Brownie.js 🍫
A lite getter/setter for cookies - because document.cookie is a nightmare to work with!
Repository: https://github.com/ez0000001000000/BrownieJS
The Problem
document.cookie is a single long string that you have to parse with complex Regex every time you want one value. It's ugly, error-prone, and makes working with cookies a pain.
The Solution
Brownie gives you brownies to work with instead of messy cookies! Internally it manages cookies, but in your app you work with delicious brownies.
Installation
npm install browniejsUsage
// Import brownies from browniejs package
const brownies = require('browniejs');
// Set a brownie (internally stores as cookie)
brownies.set('theme', 'dark', { days: 7 });
// Get a brownie (internally reads from cookie)
const theme = brownies.get('theme'); // 'dark'
// Set an authentication brownie (secure cookie internally)
brownies.set('token', 'abc123', {
days: 1,
secure: true,
sameSite: 'Strict'
});
// Check if a brownie exists
if (brownies.exists('token')) {
console.log('User is authenticated');
}
// Get all brownies
const allBrownies = brownies.getAll();
console.log(allBrownies); // { theme: 'dark', token: 'abc123', ... }
// Remove a brownie
brownies.remove('token');API Reference
brownies.get(name)
Get a brownie value by name.
- Parameters:
name(string): The brownie name
- Returns: The brownie value or
nullif not found
brownies.set(name, value, options)
Set a brownie with optional options.
- Parameters:
name(string): The brownie namevalue(string): The brownie valueoptions(object, optional):days(number): Days until expirationpath(string): Brownie path (default:'/')domain(string): Brownie domainsecure(boolean): Whether to use Secure flaghttpOnly(boolean): Whether to use HttpOnly flag (server-side only)sameSite(string): SameSite policy ('Strict','Lax','None')
brownies.remove(name, options)
Remove a brownie by setting its expiration to the past.
- Parameters:
name(string): The brownie nameoptions(object, optional):path(string): Brownie pathdomain(string): Brownie domain
brownies.getAll()
Get all brownies as an object.
- Returns: Object with all brownies as key-value pairs
brownies.exists(name)
Check if a brownie exists.
- Parameters:
name(string): The brownie name
- Returns:
trueif brownie exists,falseotherwise
Common Use Cases
Authentication Tokens
// Store auth brownie securely
brownies.set('auth_token', 'user_session_123', {
days: 1,
secure: true,
sameSite: 'Strict'
});
// Check authentication
const token = brownies.get('auth_token');
if (token) {
// User is logged in
}Dark Mode Preference
// Save theme brownie
brownies.set('theme', 'dark', { days: 30 });
// Apply theme on page load
const theme = brownies.get('theme') || 'light';
document.body.classList.toggle('dark-mode', theme === 'dark');User Preferences
// Save user preference brownies
brownies.set('language', 'en', { days: 365 });
brownies.set('timezone', 'America/New_York', { days: 365 });
// Load preferences
const prefs = brownies.getAll();
console.log(prefs.language, prefs.timezone);How It Works
Brownie.js abstracts away the complexity of document.cookie. You work with brownies in your code, and Brownie handles the cookie management behind the scenes. No more parsing strings, no more complex regex - just sweet, simple brownie management!
Browser Compatibility
Brownie works in all modern browsers that support cookies. It includes safety checks to prevent errors when used in non-browser environments (like Node.js during server-side rendering).
Why "Brownie"?
Because while the browser stores cookies, you should work with brownies! 🍫 Brownies are much sweeter and easier to work with than messy cookies. Your app gets brownies, Brownie handles the cookies.
License
MIT
