7.2.2 • Published 24 days ago

universal-cookie v7.2.2

Weekly downloads
727,899
License
MIT
Repository
github
Last release
24 days ago

Integrations

Getting started

npm install universal-cookie

or in the browser (global variable UniversalCookie):

<script
  crossorigin
  src="https://unpkg.com/universal-cookie@7/umd/universalCookie.min.js"
></script>

API - Cookies class

constructor([cookieHeader], [defaultSetOptions])

Create a cookies context

  • cookieHeader (string|object): specify the cookie header or object
  • defaultSetOptions (object): specify the default options when setting cookies
    • path (string): cookie path, use / as the path if you want your cookie to be accessible on all pages
    • expires (Date): absolute expiration date for the cookie
    • maxAge (number): relative max age of the cookie from when the client receives it in seconds
    • domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
    • secure (boolean): Is only accessible through HTTPS?
    • httpOnly (boolean): Is only the server can access the cookie? Note: You cannot get or set httpOnly cookies from the browser, only the server.
    • sameSite (boolean|none|lax|strict): Strict or Lax enforcement
    • partitioned (boolean): Indicates that the cookie should be stored using partitioned storage

get(name, [options])

Get a cookie value

  • name (string): cookie name
  • options (object):
    • doNotParse (boolean): do not convert the cookie into an object no matter what

getAll([options])

Get all cookies

  • options (object):
    • doNotParse (boolean): do not convert the cookie into an object no matter what

set(name, value, [options])

Set a cookie value

  • name (string): cookie name
  • value (string|object): save the value and stringify the object if needed
  • options (object): Support all the cookie options from RFC 6265
    • path (string): cookie path, use / as the path if you want your cookie to be accessible on all pages
    • expires (Date): absolute expiration date for the cookie
    • maxAge (number): relative max age of the cookie from when the client receives it in seconds
    • domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
    • secure (boolean): Is only accessible through HTTPS?
    • httpOnly (boolean): Is only the server can access the cookie? Note: You cannot get or set httpOnly cookies from the browser, only the server.
    • sameSite (boolean|none|lax|strict): Strict or Lax enforcement
    • partitioned (boolean): Indicates that the cookie should be stored using partitioned storage

remove(name, [options])

Remove a cookie

  • name (string): cookie name
  • options (object): Support all the cookie options from RFC 6265
    • path (string): cookie path, use / as the path if you want your cookie to be accessible on all pages
    • expires (Date): absolute expiration date for the cookie
    • maxAge (number): relative max age of the cookie from when the client receives it in seconds
    • domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
    • secure (boolean): Is only accessible through HTTPS?
    • httpOnly (boolean): Is only the server can access the cookie? Note: You cannot get or set httpOnly cookies from the browser, only the server.
    • sameSite (boolean|none|lax|strict): Strict or Lax enforcement
    • partitioned (boolean): Indicates that the cookie should be stored using partitioned storage

addChangeListener(callback)

Add a listener to when a cookie is set or removed.

  • callback (function): Call that will be called with the first argument containing name, value and options of the changed cookie.

removeChangeListener(callback)

Remove a listener from the change callback.

update()

Read back the cookies from the browser and triggers the change listeners. This should normally not be necessary because this library detects cookie changes automatically.

Browser Example

import Cookies from 'universal-cookie';

const cookies = new Cookies(null, { path: '/' });

cookies.set('myCat', 'Pacman');
console.log(cookies.get('myCat')); // Pacman

Server Example

import Cookies from 'universal-cookie';

const cookies = new Cookies(req.headers.cookie, { path: '/' });

console.log(cookies.get('myCat')); // Pacman or undefined if not set yet
@akifo/studio-store-auth-sandbox@sprucelabs/spruce-skill@bodiless/core@patrick_gratao/webappframeworknext@brainz_group/webappframeworknextverdaccio-okta-auth@jeyz/cookie-managerwebapp-user@piotrowiczmateusz/common-modulesusers-webunom-componentstest-component-login2schriftlage-components@gomothership/client-toolskwe-common-components@atg-digital/backdrop-flagsyab-test-uiachiga-notifications@nhatle/react-web-sdkmaga-functionsminsilo-react-client@digimaker/uidcg-direct-purchase@dewen_li/gatsby-theme-bodiless@atg-digital/flags@icelandair/session-storagestartse-webapp-templatecra-template-startse-tspops-app-sstvpops-kids-sstvpops-plus-webkwe-common-component@joshpress/git-cmsdigimaker@youversion/utilswapl-worksama-use-cookisehzero-boothzero-boot-purehzero-front-runtimechoerodon-hap-front-bootjv-vuex-pluginmdlinx-smartestdocluong-frontend-platform@digi-edu/dynamic-lesson-content@in-core/incore-playbook@29cm/admin-contextsincore-playbook@ncsa/incorejiva-liblegitiplatform-viladys-npm-baseubiportalraylo-apollo@everything-registry/sub-chunk-3015azuresdkmethodstestingsnbc-paas-bootcpaas-bootminimalism-node-librarytcs-module-testtabexseriescomponentstdevopt123_tabexseriescomponentstagus-cmssynapse-react-clienttest-crawlerwhatap-designed-componentswidget-feelsocialwolf-uiwordpress-rest-adminxandax-vaultwhopshared-fsdsau-core-web-libsanctum-composablesschofield-main-componentsshops-commonrespond-frameworkreacticareacting-squirrelredux-casesspotify-oauth-refresherspotify-oauth-simplespendanalyticsplatformsecondstep-marketingtestpackagetargetkiv2thinkful-uivlangv-header-testveem-web-sdkviasocket-shared-pluginsviasocket-shared-plugins-kunalvexlib-jsvmslab-component@corpdk/themes.theme-selector@ribon.io/sharedaquasis-fe-designmeri-i18nzymber-react-utils
7.2.2

24 days ago

7.2.1

1 month ago

7.2.0

4 months ago

7.1.3

8 months ago

7.1.2

8 months ago

7.1.4

8 months ago

7.1.1

8 months ago

7.1.0

9 months ago

7.0.2

10 months ago

7.0.1

11 months ago

6.1.2

11 months ago

6.1.3

11 months ago

7.0.0

11 months ago

5.0.0

1 year ago

6.1.0

1 year ago

6.0.1

1 year ago

6.0.0

1 year ago

6.1.1

1 year ago

4.0.4

4 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

6 years ago

3.1.0

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.2.0

6 years ago

2.1.5

7 years ago

2.1.2

7 years ago

2.1.0

7 years ago

2.0.9

7 years ago

2.0.8

7 years ago

2.0.7

8 years ago

2.0.6

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago