UNPKG

3.52 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 */
22 function CookieStorage(data) {
23 if (data.domain) {
24 this.domain = data.domain;
25 } else {
26 throw new Error('The domain of cookieStorage can not be undefined.');
27 }
28
29 if (data.path) {
30 this.path = data.path;
31 } else {
32 this.path = '/';
33 }
34
35 if (Object.prototype.hasOwnProperty.call(data, 'expires')) {
36 this.expires = data.expires;
37 } else {
38 this.expires = 365;
39 }
40
41 if (Object.prototype.hasOwnProperty.call(data, 'secure')) {
42 this.secure = data.secure;
43 } else {
44 this.secure = true;
45 }
46 }
47 /**
48 * This is used to set a specific item in storage
49 * @param {string} key - the key for the item
50 * @param {object} value - the value
51 * @returns {string} value that was set
52 */
53
54
55 var _proto = CookieStorage.prototype;
56
57 _proto.setItem = function setItem(key, value) {
58 Cookies.set(key, value, {
59 path: this.path,
60 expires: this.expires,
61 domain: this.domain,
62 secure: this.secure
63 });
64 return Cookies.get(key);
65 }
66 /**
67 * This is used to get a specific key from storage
68 * @param {string} key - the key for the item
69 * This is used to clear the storage
70 * @returns {string} the data item
71 */
72 ;
73
74 _proto.getItem = function getItem(key) {
75 return Cookies.get(key);
76 }
77 /**
78 * This is used to remove an item from storage
79 * @param {string} key - the key being set
80 * @returns {string} value - value that was deleted
81 */
82 ;
83
84 _proto.removeItem = function removeItem(key) {
85 return Cookies.remove(key, {
86 path: this.path,
87 domain: this.domain,
88 secure: this.secure
89 });
90 }
91 /**
92 * This is used to clear the storage
93 * @returns {string} nothing
94 */
95 ;
96
97 _proto.clear = function clear() {
98 var cookies = Cookies.get();
99 var index;
100
101 for (index = 0; index < cookies.length; ++index) {
102 Cookies.remove(cookies[index]);
103 }
104
105 return {};
106 };
107
108 return CookieStorage;
109}();
110
111exports["default"] = CookieStorage;
\No newline at end of file