1 | import * as Cookies from 'js-cookie';
|
2 |
|
3 |
|
4 | var CookieStorage = function () {
|
5 | |
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
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 |
|
55 |
|
56 |
|
57 |
|
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 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | ;
|
85 |
|
86 | _proto.getItem = function getItem(key) {
|
87 | return Cookies.get(key);
|
88 | }
|
89 | |
90 |
|
91 |
|
92 |
|
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 |
|
112 |
|
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 |
|
130 | export { CookieStorage as default }; |
\ | No newline at end of file |