A lightweight, easy-to-use cookie management module for client-side JavaScript. This module provides functions for getting, setting, and removing cookies, as well as utilities for working with JSON data and checking browser cookie support.

Version
  • 2.0.0

Example

import cookie from 'kitto/cookie';

// Set a cookie
cookie.set('user', { id: 123, name: 'John' }, { expires: 7 });

// Get a cookie
const user = cookie.get('user', true); // true to parse JSON

// Remove a cookie
cookie.remove('user');

// Check if cookies are enabled
if (cookie.isEnabled()) {
  console.log('Cookies are supported and enabled');
}

Methods

(inner) get(name, parseJSONopt) → {string|object|null}

Get a cookie value by name

Parameters:
NameTypeAttributesDefaultDescription
namestring

The name of the cookie to retrieve

parseJSONboolean<optional>
false

Whether to parse the value as JSON

Returns:

The cookie value, parsed as JSON if specified, or null if not found

Type: 
string | object | null
Example
// Get a simple cookie
const userId = cookie.get('userId');

// Get and parse a JSON cookie
const userObject = cookie.get('user', true);

(inner) getAll() → {object}

Get all cookies as an object

Returns:

An object containing all cookies

Type: 
object
Example
// Get all cookies
const allCookies = cookie.getAll();
console.log(allCookies); // { userId: '123', sessionId: 'abc123', ... }

(inner) isEnabled() → {boolean}

Check if cookies are enabled in the browser

Returns:

True if cookies are enabled, false otherwise

Type: 
boolean
Example
// Check if cookies are enabled
if (cookie.isEnabled()) {
  console.log('Cookies are enabled');
} else {
  console.log('Cookies are disabled');
}

(inner) parse(setCookies, callback)

Parse Set-Cookie headers and call a callback for each cookie

Parameters:
NameTypeDescription
setCookiesArray.<string>

Array of Set-Cookie header strings

callbackfunction

Function to call for each parsed cookie

Example
// Parse Set-Cookie headers
const setCookies = [
  'session=abc123; Path=/; HttpOnly; Secure',
  'user=john; Expires=Wed, 21 Oct 2023 07:28:00 GMT'
];
cookie.parse(setCookies, (name, value, options) => {
  console.log(name, value, options);
});

(inner) remove(name)

Remove a cookie

Parameters:
NameTypeDescription
namestring

The name of the cookie to remove

Example
// Remove a cookie
cookie.remove('userId');

(inner) removeAll()

Remove all cookies

Example
// Remove all cookies
cookie.removeAll();

(inner) set(name, value, optionsopt)

Set a cookie

Parameters:
NameTypeAttributesDefaultDescription
namestring

The name of the cookie to set

valuestring | object

The value to set

optionsobject<optional>
{}

Cookie options

Properties
NameTypeAttributesDefaultDescription
expiresnumber | Date<optional>

Expiration date or number of days until expiry

pathstring<optional>
'/'

The path for the cookie

sameSitestring<optional>
'Lax'

SameSite attribute for the cookie

Example
// Set a simple cookie that expires in 7 days
cookie.set('userId', '123', { expires: 7 });

// Set a JSON cookie with a specific expiration date
cookie.set('user', { id: 123, name: 'John' }, { expires: new Date('2023-12-31') });

// Set a session cookie (expires when browser is closed)
cookie.set('sessionId', 'abc123');