Skip to content

CerebralFart/Cookies

Repository files navigation

@CerebralFart/Cookies

Simple interaction with cookies in the browser

This library, as the name suggests, allows you to easily interact with cookies in the browser. No longer do you need to parse some complicated URI encoded string, instead, we've collected them already for you. Below we'll quickly go over how you can use this library.

Getting a cookie

const id = Cookie.trackingID;

Setting a cookie

Cookie.trackingID = id;

Removing a cookie

delete Cookie.trackingID;

Checking if a cookie exists

"trackingID" in Cookie;

Enumerating all cookies

for (const name in Cookie) {
  console.log(name);
}

There you have it, that's most there is to this library. If you need some more customization options, do keep reading.

Advanced configuration

It is also possible to customize the options of cookies, but we have provided sane defaults so you will rarely need this.

Cookie.trackingID = new Cookie(id, {
  path: "/subfolder/",
});

The the following options are available:

Key Type Default
path string '/' The path for this cookie.
domain string location.hostname The domain to which this cookie belongs, check MDN for some implementation notes.
secure boolean true if the page is served over https Set to true if this cookie should only be available in secure contexts.
sameSite 'lax','strict' or 'none' 'lax' The same site behaviour of this cookie.
maxAge number 14 * 24 * 60 * 60, 14 days The maximum age in seconds before this cookie should be removed, mutually exclusive with the expires parameter.
expires Date The date this cookie should expire, mutually exclusive with the maxAge parameter.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published