A lite getter/setter for cookies - because document.cookie is a nightmare to work with!
Repository: https://github.com/ez0000001000000/BrownieJS
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.
Brownie gives you brownies to work with instead of messy cookies! Internally it manages cookies, but in your app you work with delicious brownies.
npm install browniejs// 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');Get a brownie value by name.
- Parameters:
name(string): The brownie name
- Returns: The brownie value or
nullif not found
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')
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
Get all brownies as an object.
- Returns: Object with all brownies as key-value pairs
Check if a brownie exists.
- Parameters:
name(string): The brownie name
- Returns:
trueif brownie exists,falseotherwise
// 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
}// 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');// 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);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!
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).
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.
MIT