universal-cookie
Universal cookies for JavaScript
npm downloads npm version npm license
universal-cookie유사 패키지:
npm 다운로드 트렌드
3 년
🌟 universal-cookie의 README.md에 실시간 사용 차트를 표시하려면 아래 코드를 복사하세요.
## Usage Trend
[![Usage Trend of universal-cookie](https://npm-compare.com/img/npm-trend/THREE_YEARS/universal-cookie.png)](https://npm-compare.com/universal-cookie#timeRange=THREE_YEARS)
Cumulative GitHub Star Trend
🌟 universal-cookie의 README.md에 GitHub Stars 트렌드 차트를 표시하려면 아래 코드를 복사하세요.
## GitHub Stars Trend
[![GitHub Stars Trend of universal-cookie](https://npm-compare.com/img/github-trend/universal-cookie.png)](https://npm-compare.com/universal-cookie)
통계 세부사항
패키지
다운로드
Stars
크기
Issues
발행일
라이선스
universal-cookie1,783,22820254.4 kB197ヶ月前MIT
universal-cookie의 README

universal-cookie

Universal cookies for JavaScript

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.

removeAllChangeListeners()

Remove all change listeners that were previously added with addChangeListener().

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