1 | import * as Cookies from 'js-cookie';
|
2 |
|
3 |
|
4 | export default class CookieStorage {
|
5 | |
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | constructor(data) {
|
14 | if (data.domain) {
|
15 | this.domain = data.domain;
|
16 | } else {
|
17 | throw new Error('The domain of cookieStorage can not be undefined.');
|
18 | }
|
19 | if (data.path) {
|
20 | this.path = data.path;
|
21 | } else {
|
22 | this.path = '/';
|
23 | }
|
24 | if (Object.prototype.hasOwnProperty.call(data, 'expires')) {
|
25 | this.expires = data.expires;
|
26 | } else {
|
27 | this.expires = 365;
|
28 | }
|
29 | if (Object.prototype.hasOwnProperty.call(data, 'secure')) {
|
30 | this.secure = data.secure;
|
31 | } else {
|
32 | this.secure = true;
|
33 | }
|
34 | }
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | setItem(key, value) {
|
43 | Cookies.set(key, value, {
|
44 | path: this.path,
|
45 | expires: this.expires,
|
46 | domain: this.domain,
|
47 | secure: this.secure,
|
48 | });
|
49 | return Cookies.get(key);
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | getItem(key) {
|
59 | return Cookies.get(key);
|
60 | }
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 | removeItem(key) {
|
68 | return Cookies.remove(key, {
|
69 | path: this.path,
|
70 | domain: this.domain,
|
71 | secure: this.secure,
|
72 | });
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 | clear() {
|
80 | const cookies = Cookies.get();
|
81 | let index;
|
82 | for (index = 0; index < cookies.length; ++index) {
|
83 | Cookies.remove(cookies[index]);
|
84 | }
|
85 | return {};
|
86 | }
|
87 | }
|