1.0.2 • Published 6 years ago

flexible-cookies v1.0.2

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

Flexible Cookies 🍪

NPM version Build Status

flexible-cookies is a flexible way to manage cookies in javascript/node.js.


Installation

npm install --save flexible-cookies

Basic Usage

import { Cookies } from 'flexible-cookies';
// or default import
import Cookies from 'flexible-cookies/cookies'

Set cookie

Cookies.set('COOKIE-NAME', 'COOKIE-VALUE', options);

Get cookie

const cookieValue = Cookies.get('COOKIE-NAME', options);

Delete cookie

Cookies.delete('COOKIE-NAME', options);

Configuration

You can specify a source where flexible-cookies will read/edit the cookie. By default, the source is document (which is the browser working-behavior) but you can change it to make cookies working in a server context:

Cookies.set('CONTEXT-COOKIE', 'Hello !', {
  source: ctx.req.headers
});
const cookieValue = Cookies.get('CONTEXT-COOKIE', {
  source: ctx.req.headers
});
console.log(cookieValue) // Hello !

Cookie validity duration

By default, the cookie is valid until the browser session is closed, but you can increase the validity duration of the cookie:

Cookies.set('ONE-YEAR-VALID-COOKIE', 'COOKIE-VALUE', {
  days: 365
});

Cookie path

Cookies.set('SPECIFIC-PATH-COOKIE', 'COOKIE-VALUE', {
  path: '/'
});

🚀