UNPKG

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