Skip to content

A lite getter/setter for cookies - because `document.cookie` is a nightmare to work with!

Notifications You must be signed in to change notification settings

Snowbase-Studio/BrownieJS

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 browniejs

Usage

// 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 null if not found

brownies.set(name, value, options)

Set a brownie with optional options.

  • Parameters:
    • name (string): The brownie name
    • value (string): The brownie value
    • options (object, optional):
      • days (number): Days until expiration
      • path (string): Brownie path (default: '/')
      • domain (string): Brownie domain
      • secure (boolean): Whether to use Secure flag
      • httpOnly (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 name
    • options (object, optional):
      • path (string): Brownie path
      • domain (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: true if brownie exists, false otherwise

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

About

A lite getter/setter for cookies - because `document.cookie` is a nightmare to work with!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%