@langlois/cookies
v1.0.0
Published
A read/write library for browser cookies.
Downloads
4
Readme
Cookie Library
A javascript library that helps set, get and expire browser cookies.
Usage
import {cookies} from '@langlois/cookies';
// setting a cookie
cookies.set('testCookie', 'helloWorld');
// getting a cookie
cookies.get('testCookie'); // returns 'helloWorld'
// removing/expiring a cookie
cookies.expire('testCookie');
cookies.get('testCookie'); // returns undefined as cookie is expired
/*
One can chain certain methods together.
This chain will return:
{
testCookie: 'helloWorld',
testCookie2: 'goodbyeWorld'
}
*/
cookies.set('testCookie', 'helloWorld').set('testCookie2', 'goodbyeWorld', {
domain: '.langlois.dev',
expires: Infinity,
path: '/',
secure: true
}).getAll();
cookies.expire('testCookie2').get('testCookie') // returns 'helloWorld'
cookies.expireAll().get('testCookie'); // returns undefined
API
.enabled()
Determines we can set, get and remove a cookie.
Returns a Boolean
.
Usage
cookies.enabled();
.expire(cookieName)
Expires the cookie with the passed cookieName
.
Returns this
to allow chaining.
Params
| Name | Type | Description | |------------|--------|-----------------------------------------------| | cookieName | String | The name/key of the cookie one wants expired. |
Usage
cookies.expire('testCookie');
.expireAll()
Expires all the cookies.
Returns this
to allow chaining.
Usage
cookies.expireAll();
.get(cookieName)
Gets a cookie with the passed cookieName
.
Returns a String
representing the value of the cookie or undefined
if the cookie doesn't exist.
Params
| Name | Type | Description | |------------|--------|---------------------------------------------------| | cookieName | String | The name/key of the cookie one wants to retrieve. |
Usage
cookies.get('testCookie');
.getAll()
Gets all the currently set cookies.
Returns an Object
containing all the cookies and their values or undefined
if there are no cookies.
Usage
cookies.getAll();
.set(cookieName, cookieValue, options)
Sets a cookie with the cookieName
and cookieValue
passed.
Returns this
to allow chaining.
Params
| Name | Type | Default | Description | |-----------------|--------------------------------|-------------------------------------------|-----------------------------------------------------| | cookieName | String | | The name/key of the cookie to be set. | | cookieValue | String | | The value of the cookie to be set. | | options | Object | | Different cookie options to set | | options.domain | String | "" | Domain name that the cookie should be accessible to | | options.expires | String|Number|Infinity|Date | new Date('Fri, 31 Dec 9999 23:59:59 UTC') | Date that the cookie expires | | options.path | String | '/' | Path that has access to cookie | | options.secure | Boolean | false | Cookie to always use HTTPS or not |
Usage
cookies.set('testCookie', 'helloWorld', {
domain: '',
expires: new Date('Fri, 31 Dec 9999 23:59:59 UTC'),
path: '/',
secure: false
});
Chaining
One can chain the following methods:
- .expire
- .expireAll
- .set
Example
cookies.expire('testCookie').get('testCookie'); // returns undefined
cookies.set('testCookie', 'helloWorld').set('testCookie2', 'goodbyeWorld').get('testCookie'); // returns 'helloWorld'