-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
CookieStorage.js
116 lines (107 loc) · 2.77 KB
/
CookieStorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as Cookies from 'js-cookie';
/** @class */
export default class CookieStorage {
/**
* Constructs a new CookieStorage object
* @param {object} data Creation options.
* @param {string} data.domain Cookies domain (mandatory).
* @param {string} data.path Cookies path (default: '/')
* @param {integer} data.expires Cookie expiration (in days, default: 365)
* @param {boolean} data.secure Cookie secure flag (default: true)
* @param {string} data.sameSite Cookie request behaviour (default: null)
*/
constructor(data) {
if (data.domain) {
this.domain = data.domain;
} else {
throw new Error('The domain of cookieStorage can not be undefined.');
}
if (data.path) {
this.path = data.path;
} else {
this.path = '/';
}
if (Object.prototype.hasOwnProperty.call(data, 'expires')) {
this.expires = data.expires;
} else {
this.expires = 365;
}
if (Object.prototype.hasOwnProperty.call(data, 'secure')) {
this.secure = data.secure;
} else {
this.secure = true;
}
if (Object.prototype.hasOwnProperty.call(data, 'sameSite')) {
if (!['strict','lax','none'].includes(data.sameSite)) {
throw new Error(
'The sameSite value of cookieStorage must be "lax", "strict" or "none".'
);
}
if (data.sameSite === 'none' && !this.secure) {
throw new Error(
'sameSite = None requires the Secure attribute in latest browser versions.'
);
}
this.sameSite = data.sameSite;
} else {
this.sameSite = null;
}
}
/**
* This is used to set a specific item in storage
* @param {string} key - the key for the item
* @param {object} value - the value
* @returns {string} value that was set
*/
setItem(key, value) {
const options = {
path: this.path,
expires: this.expires,
domain: this.domain,
secure: this.secure,
};
if (this.sameSite) {
options.sameSite = this.sameSite;
}
Cookies.set(key, value, options);
return Cookies.get(key);
}
/**
* This is used to get a specific key from storage
* @param {string} key - the key for the item
* This is used to clear the storage
* @returns {string} the data item
*/
getItem(key) {
return Cookies.get(key);
}
/**
* This is used to remove an item from storage
* @param {string} key - the key being set
* @returns {string} value - value that was deleted
*/
removeItem(key) {
const options = {
path: this.path,
expires: this.expires,
domain: this.domain,
secure: this.secure,
};
if (this.sameSite) {
options.sameSite = this.sameSite;
}
return Cookies.remove(key, options);
}
/**
* This is used to clear the storage
* @returns {string} nothing
*/
clear() {
const cookies = Cookies.get();
let index;
for (index = 0; index < cookies.length; ++index) {
Cookies.remove(cookies[index]);
}
return {};
}
}