Skip to content

Commit 9d8600f

Browse files
committed
Added the 'domain' to the 'cookieStorage' options
1 parent 29a9fd7 commit 9d8600f

11 files changed

+385
-227
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"no-underscore-dangle": 0,
4848
"object-curly-spacing": [2, "never"],
4949
"import/no-mutable-exports": 1,
50-
"import/prefer-default-export": 1,
50+
"import/prefer-default-export": 0,
5151
"import/no-extraneous-dependencies": [
5252
"error", { "devDependencies": true }
5353
]

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
<!-- markdownlint-disable MD024 MD033 -->
44

5+
## 2.1.0
6+
7+
### Features
8+
9+
1. Added the `domain` to the `cookieStorage` options.
10+
11+
---
12+
513
## 2.0.2
614

715
### Improvements

README.md

Lines changed: 306 additions & 196 deletions
Large diffs are not rendered by default.

dist/proxy-storage.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! proxyStorage@v2.0.2. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
1+
/*! proxyStorage@v2.1.0. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
22
(function webpackUniversalModuleDefinition(root, factory) {
33
if(typeof exports === 'object' && typeof module === 'object')
44
module.exports = factory();
@@ -88,7 +88,7 @@ Object.defineProperty(exports, "__esModule", {
8888
value: true
8989
});
9090
exports.isObject = isObject;
91-
exports.setTimestamp = setTimestamp;
91+
exports.alterDate = alterDate;
9292
exports.setProperty = setProperty;
9393
exports.checkEmpty = checkEmpty;
9494
/**
@@ -102,21 +102,25 @@ function isObject(value) {
102102
}
103103

104104
/**
105-
* Allows add or subtract timestamps to the current date or to a specific date.
105+
* Adds or subtracts date portions to the given date and returns the new date.
106106
*
107-
* @param {object} options: It contains the timestamps to add or remove to the date, and have the following properties:
108-
* - {Date} date: if provided, the timestamps will affect this date, otherwise a new current date will be used.
107+
* @see https://gist.github.com/jherax/bbc43e479a492cc9cbfc7ccc20c53cd2
108+
*
109+
* @param {object} options: It contains the date parts to add or remove, and can have the following properties:
110+
* - {Date} date: if provided, this date will be affected, otherwise the current date will be used.
111+
* - {number} minutes: minutes to add/subtract
109112
* - {number} hours: hours to add/subtract
110113
* - {number} days: days to add/subtract
111114
* - {number} months: months to add/subtract
112115
* - {number} years: years to add/subtract
113116
* @return {Date}
114117
*/
115-
function setTimestamp() {
118+
function alterDate() {
116119
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
117120

118121
var opt = Object.assign({}, options);
119122
var d = opt.date instanceof Date ? opt.date : new Date();
123+
if (+opt.minutes) d.setMinutes(d.getMinutes() + opt.minutes);
120124
if (+opt.hours) d.setHours(d.getHours() + opt.hours);
121125
if (+opt.days) d.setDate(d.getDate() + opt.days);
122126
if (+opt.months) d.setMonth(d.getMonth() + opt.months);
@@ -194,6 +198,8 @@ exports.proxy = exports.webStorageSettings = exports.default = undefined;
194198

195199
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
196200

201+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
202+
197203
var _proxyMechanism = __webpack_require__(5);
198204

199205
var _utils = __webpack_require__(0);
@@ -241,6 +247,10 @@ function executeInterceptors(command) {
241247

242248
var key = args.shift();
243249
var value = args.shift();
250+
if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
251+
// clone the object to prevent mutations
252+
value = JSON.parse(JSON.stringify(value));
253+
}
244254
return _interceptors[command].reduce(function (val, action) {
245255
var transformed = action.apply(undefined, [key, val].concat(args));
246256
if (transformed === undefined) return val;
@@ -387,7 +397,7 @@ var WebStorage = function () {
387397
value: function getItem(key) {
388398
(0, _utils.checkEmpty)(key);
389399
var value = _proxyMechanism.proxy[this.__storage__].getItem(key);
390-
if (value === undefined) {
400+
if (value == null) {
391401
delete this[key];
392402
value = null;
393403
} else {
@@ -518,14 +528,14 @@ var $cookie = {
518528
*
519529
* Builds the expiration part for the cookie.
520530
*
521-
* @see utils.setTimestamp(options)
531+
* @see utils.alterDate(options)
522532
*
523533
* @param {Date|object} date: the expiration date
524534
* @return {string}
525535
*/
526536
/* eslint-disable no-invalid-this */
527537
function buildExpirationString(date) {
528-
var expires = date instanceof Date ? (0, _utils.setTimestamp)({ date: date }) : (0, _utils.setTimestamp)(date);
538+
var expires = date instanceof Date ? (0, _utils.alterDate)({ date: date }) : (0, _utils.alterDate)(date);
529539
return '; expires=' + expires.toUTCString();
530540
}
531541

@@ -554,13 +564,24 @@ function findCookie(cookie) {
554564
function cookieStorage() {
555565
var api = {
556566
setItem: function setItem(key, value, options) {
557-
var expires = '';
567+
var domain = '',
568+
expires = '';
558569
options = Object.assign({ path: '/' }, options);
559570
if ((0, _utils.isObject)(options.expires) || options.expires instanceof Date) {
560571
expires = buildExpirationString(options.expires);
561572
}
562-
var cookie = key + '=' + encodeURIComponent(value) + expires + '; path=' + options.path;
573+
// http://stackoverflow.com/a/5671466/2247494
574+
if (typeof options.domain === 'string') {
575+
domain = '; domain=' + options.domain.trim();
576+
}
577+
var cookie = key + '=' + encodeURIComponent(value) + expires + domain + '; path=' + options.path;
578+
// TODO: add metadata to store options for the cookie
579+
// TODO: remove cookies are failing when domain or path were set
580+
// TODO: prevent adding cookies when the domain or path are not valid
581+
// TODO: remove expired cookies through getItem or setTimeout for expires
582+
// console.log('before set', $cookie.get()); // eslint-disable-line
563583
$cookie.set(cookie);
584+
// console.log('after set', $cookie.get()); // eslint-disable-line
564585
},
565586
getItem: function getItem(key) {
566587
var value = null;
@@ -777,8 +798,6 @@ var _isAvailable = __webpack_require__(1);
777798

778799
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
779800

780-
// eslint-disable-line
781-
782801
// If you want to support all ES6 features, uncomment the next line
783802
// import 'babel-polyfill';
784803

0 commit comments

Comments
 (0)