1.0.0 • Published 5 years ago

@langlois/cookies v1.0.0

Weekly downloads
1
License
Unlicense
Repository
gitlab
Last release
5 years ago

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

NameTypeDescription
cookieNameStringThe 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

NameTypeDescription
cookieNameStringThe 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

NameTypeDefaultDescription
cookieNameStringThe name/key of the cookie to be set.
cookieValueStringThe value of the cookie to be set.
optionsObjectDifferent cookie options to set
options.domainString""Domain name that the cookie should be accessible to
options.expiresString|Number|Infinity|Datenew Date('Fri, 31 Dec 9999 23:59:59 UTC')Date that the cookie expires
options.pathString'/'Path that has access to cookie
options.secureBooleanfalseCookie 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'
1.0.0

5 years ago