JSPM

  • Created
  • Published
  • Downloads 1909319
  • Score
    100M100P100Q202611F
  • License MIT

Universal cookies for JavaScript

Package Exports

  • universal-cookie
  • universal-cookie/lib/utils

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (universal-cookie) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

universal-cookie

Universal cookies for JavaScript

Build Status
Sauce Test Status

Integrations

Getting started

npm install universal-cookie

API - Cookies class

constructor([cookieHeader])

Create a cookies context

  • cookieHeader (string|object): specify the cookie header on the server only

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 second
    • 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?

Browser Example

import Cookies from 'universal-cookie';

const cookies = new Cookies();

cookies.set('myCat', 'Pacman', { path: '/' });
console.log(cookies.get('myCat')); // Pacman

Server Example

import Cookies from 'universal-cookie';

const cookies = new Cookies(req.headers.cookie);

console.log(cookies.get('myCat')); // Pacman or undefined if not set yet