UNPKG

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