concorde-cookie
![npm version](https://badge.fury.io/js/%40wetransfer%2Fconcorde-cookie.svg)
Cookie module. Handy functions to deal with cookies on the browser.
Installation
npm install @wetransfer/concorde-cookie --save
Usage
In the browser
Import the package if your are using a package bundler like Webpack or Parcel:
import Cookie from '@wetransfer/concorde-cookie';
if (Cookie.get('SSID')) {
}
Or load directly the final bundle on your browser, using a script tag. All concorde.js modules will be available in a global variable called WT
:
<script src="https://unpkg.com/@wetransfer/concorde-cookie/dist/concorde-cookie.min.js"></script>
<script>
WT.cookie.set('SSID', 'BBlPjuhTimjov9RCB', { secure: true });
</script>
On the server
const Cookie = require('@wetransfer/concorde-cookie');
Cookie.set('SSID', 'BBlPjuhTimjov9RCB', { secure: true });
Methods
Cookie.configure
Configure default options used when settings values for cookies. If all your cookies must present the same property, like secure, use this method before setting the cookie values.
Cookie.configure({ secure: true });
Cookie.set('foo', 'bar');
The following options can be configured:
Cookie.configure({
expires: new Date('Fri, 01 Jan 2016 09:05:12 GMT'),
path: '/path',
domain: 'wetransfer.com',
secure:true,
});
Cookie.get
Get the value from a cookie, given a cookie name.
Cookie.get('foo');
Cookie.get('nonexistant', { defaultValue: 1 });
Cookie.get('bar', { raw: true });
Cookie.set
Set the value for a cookie. If extra options are specified, they will override default options defined with configure method
.
Cookie.set('foo', 'bar');
Cookie.set('foo', 'bar', { secure: true});
Cookie.unset
Unsets the value of a cookie, with null
, and expires it.
Cookie.unset('foo');
Development
In case you want to develop/debug this module while integrating with other project, please follow these steps:
- Run
npm link
to create a global symlink to that module - Run
npm run build:watch
to listen to changes and rebuild the module - Link to this module from your project with
npm link @wetransfer/concorde-cookie