UNPKG

3.64 kBMarkdownView Raw
1<h3 align="center">
2 universal-cookie
3</h3>
4
5<p align="center">
6 Universal cookies for JavaScript<br />
7 <a href="https://badge.fury.io/js/universal-cookie"><img src="https://badge.fury.io/js/universal-cookie.svg" /></a>
8</p>
9
10[![Build Status](https://travis-ci.org/reactivestack/cookies.svg?branch=master)](https://travis-ci.org/reactivestack/cookies)
11<br />
12[![Sauce Test Status](https://saucelabs.com/browser-matrix/coookies.svg)](https://saucelabs.com/u/coookies)
13
14## Integrations
15 - [`react-cookie`](https://www.npmjs.com/package/react-cookie) - Universal cookies for React
16 - [`universal-cookie-express`](https://www.npmjs.com/package/universal-cookie-express) - Hook cookies get/set on Express for server-rendering
17
18## Getting started
19
20`npm install universal-cookie`
21
22 or in the browser (global variable `UniversalCookie`):
23
24```html
25<script crossorigin src="https://unpkg.com/universal-cookie@3/umd/universalCookie.min.js"></script>
26```
27
28## API - Cookies class
29
30### `constructor([cookieHeader])`
31Create a cookies context
32 - cookieHeader (string|object): specify the cookie header or object
33
34### `get(name, [options])`
35Get a cookie value
36 - name (string): cookie name
37 - options (object):
38 - doNotParse (boolean): do not convert the cookie into an object no matter what
39
40### `getAll([options])`
41Get all cookies
42 - options (object):
43 - doNotParse (boolean): do not convert the cookie into an object no matter what
44
45### `set(name, value, [options])`
46Set a cookie value
47- name (string): cookie name
48- value (string|object): save the value and stringify the object if needed
49- options (object): Support all the cookie options from RFC 6265
50 - path (string): cookie path, use `/` as the path if you want your cookie to be accessible on all pages
51 - expires (Date): absolute expiration date for the cookie
52 - maxAge (number): relative max age of the cookie from when the client receives it in second
53 - domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
54 - secure (boolean): Is only accessible through HTTPS?
55 - httpOnly (boolean): Is only the server can access the cookie?
56 - sameSite (boolean|none|lax|strict): Strict or Lax enforcement
57
58### `remove(name, [options])`
59Remove a cookie
60- name (string): cookie name
61- options (object): Support all the cookie options from RFC 6265
62 - path (string): cookie path, use `/` as the path if you want your cookie to be accessible on all pages
63 - expires (Date): absolute expiration date for the cookie
64 - maxAge (number): relative max age of the cookie from when the client receives it in second
65 - domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
66 - secure (boolean): Is only accessible through HTTPS?
67 - httpOnly (boolean): Is only the server can access the cookie?
68 - sameSite (boolean|none|lax|strict): Strict or Lax enforcement
69
70### `addChangeListener(callback)`
71Add a listener to when a cookie is set or removed.
72 - callback (function): Call that will be called with the first argument containing `name`, `value` and `options` of the changed cookie.
73
74### `removeChangeListener(callback)`
75Remove a listener from the change callback.
76
77## Browser Example
78
79```js
80import Cookies from 'universal-cookie';
81
82const cookies = new Cookies();
83
84cookies.set('myCat', 'Pacman', { path: '/' });
85console.log(cookies.get('myCat')); // Pacman
86```
87
88## Server Example
89
90```js
91import Cookies from 'universal-cookie';
92
93const cookies = new Cookies(req.headers.cookie);
94
95console.log(cookies.get('myCat')); // Pacman or undefined if not set yet
96```