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
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
name | string | The name of the cookie to retrieve | ||
parseJSON | boolean | <optional> | false | Whether to parse the value as JSON |
The cookie value, parsed as JSON if specified, or null if not found
- Type:
- string |
object | null
// 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
An object containing all cookies
- Type:
- object
// 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
True if cookies are enabled, false otherwise
- Type:
- boolean
// 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
Name | Type | Description |
---|---|---|
setCookies | Array.<string> | Array of Set-Cookie header strings |
callback | function | Function to call for each parsed cookie |
// 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
Name | Type | Description |
---|---|---|
name | string | The name of the cookie to remove |
// Remove a cookie
cookie.remove('userId');
(inner) removeAll()
Remove all cookies
// Remove all cookies
cookie.removeAll();
(inner) set(name, value, optionsopt)
Set a cookie
Name | Type | Attributes | Default | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name | string | The name of the cookie to set | ||||||||||||||||||||||
value | string | | The value to set | ||||||||||||||||||||||
options | object | <optional> | {} | Cookie options Properties
|
// 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');