1
1
import Vue from 'vue'
2
- import Cookies from 'js-cookie'
3
2
import getProp from 'dotprop'
4
- import { parse as parseCookie } from 'cookie'
5
-
6
- import { isUnset , isSet } from './utilities'
3
+ import { parse as parseCookie , serialize as serializeCookie } from 'cookie'
4
+ import { isUnset , isSet , decodeValue , encodeValue } from './utilities'
7
5
8
6
export default class Storage {
9
7
constructor ( ctx , options ) {
@@ -17,38 +15,43 @@ export default class Storage {
17
15
// Universal
18
16
// ------------------------------------
19
17
20
- setUniversal ( key , value , isJson ) {
18
+ setUniversal ( key , value ) {
19
+ // Unset null, undefined
20
+ if ( isUnset ( value ) ) {
21
+ return this . removeUniversal ( key )
22
+ }
23
+
21
24
// Local state
22
25
this . setState ( key , value )
23
26
24
27
// Cookies
25
28
this . setCookie ( key , value )
26
29
27
30
// Local Storage
28
- this . setLocalStorage ( key , value , isJson )
31
+ this . setLocalStorage ( key , value )
29
32
30
33
return value
31
34
}
32
35
33
- getUniversal ( key , isJson ) {
36
+ getUniversal ( key ) {
34
37
// Local state
35
38
let value = this . getState ( key )
36
39
37
40
// Cookies
38
41
if ( isUnset ( value ) ) {
39
- value = this . getCookie ( key , isJson )
42
+ value = this . getCookie ( key )
40
43
}
41
44
42
45
// Local Storage
43
46
if ( isUnset ( value ) ) {
44
- value = this . getLocalStorage ( key , isJson )
47
+ value = this . getLocalStorage ( key )
45
48
}
46
49
47
50
return value
48
51
}
49
52
50
- syncUniversal ( key , defaultValue , isJson ) {
51
- let value = this . getUniversal ( key , isJson )
53
+ syncUniversal ( key , defaultValue ) {
54
+ let value = this . getUniversal ( key )
52
55
53
56
if ( isUnset ( value ) && isSet ( defaultValue ) ) {
54
57
value = defaultValue
@@ -61,6 +64,12 @@ export default class Storage {
61
64
return value
62
65
}
63
66
67
+ removeUniversal ( key ) {
68
+ this . removeState ( key )
69
+ this . removeLocalStorage ( key )
70
+ this . removeCookie ( key )
71
+ }
72
+
64
73
// ------------------------------------
65
74
// Local state (reactive)
66
75
// ------------------------------------
@@ -128,79 +137,118 @@ export default class Storage {
128
137
}
129
138
}
130
139
140
+ removeState ( key ) {
141
+ this . setState ( key , undefined )
142
+ }
143
+
131
144
// ------------------------------------
132
145
// Local storage
133
146
// ------------------------------------
134
147
135
- setLocalStorage ( key , value , isJson ) {
148
+ setLocalStorage ( key , value ) {
149
+ // Unset null, undefined
150
+ if ( isUnset ( value ) ) {
151
+ return this . removeLocalStorage ( key )
152
+ }
153
+
136
154
if ( typeof localStorage === 'undefined' || ! this . options . localStorage ) {
137
155
return
138
156
}
139
157
140
158
const _key = this . options . localStorage . prefix + key
141
159
142
- if ( isUnset ( value ) ) {
143
- localStorage . removeItem ( _key )
144
- } else {
145
- localStorage . setItem ( _key , isJson ? JSON . stringify ( value ) : value )
160
+ try {
161
+ localStorage . setItem ( _key , encodeValue ( value ) )
162
+ } catch ( e ) {
163
+ if ( ! this . options . ignoreExceptions ) {
164
+ throw e
165
+ }
146
166
}
147
167
148
168
return value
149
169
}
150
170
151
- getLocalStorage ( key , isJson ) {
171
+ getLocalStorage ( key ) {
152
172
if ( typeof localStorage === 'undefined' || ! this . options . localStorage ) {
153
173
return
154
174
}
155
175
156
176
const _key = this . options . localStorage . prefix + key
157
177
158
178
const value = localStorage . getItem ( _key )
159
- if ( value === 'false' ) {
160
- return false
179
+
180
+ return decodeValue ( value )
181
+ }
182
+
183
+ removeLocalStorage ( key ) {
184
+ if ( typeof localStorage === 'undefined' || ! this . options . localStorage ) {
185
+ return
161
186
}
162
187
163
- return isJson ? JSON . parse ( value ) : value
188
+ const _key = this . options . localStorage . prefix + key
189
+ localStorage . removeItem ( _key )
164
190
}
165
191
166
192
// ------------------------------------
167
193
// Cookies
168
194
// ------------------------------------
195
+ getCookies ( ) {
196
+ const cookieStr = process . client
197
+ ? document . cookie
198
+ : this . ctx . req . headers . cookie
199
+
200
+ return parseCookie ( cookieStr || '' ) || { }
201
+ }
169
202
170
203
setCookie ( key , value , options = { } ) {
171
- if ( process . server || ! this . options . cookie ) {
204
+ if ( ! this . options . cookie || ( process . server && ! this . ctx . res ) ) {
172
205
return
173
206
}
174
207
175
208
const _key = this . options . cookie . prefix + key
176
-
177
209
const _options = Object . assign ( { } , this . options . cookie . options , options )
210
+ const _value = encodeValue ( value )
178
211
212
+ // Unset null, undefined
179
213
if ( isUnset ( value ) ) {
180
- Cookies . remove ( _key , _options )
181
- } else {
182
- Cookies . set ( _key , value , _options )
214
+ _options . maxAge = - 1
215
+ }
216
+
217
+ const serializedCookie = serializeCookie ( _key , _value , _options )
218
+
219
+ if ( process . client ) {
220
+ // Set in browser
221
+ document . cookie = serializedCookie
222
+ } else if ( process . server && this . ctx . res ) {
223
+ // Send Set-Cookie header from server side
224
+ const prev = this . ctx . res . getHeader ( 'Set-Cookie' )
225
+ let value = serializedCookie
226
+ if ( prev ) {
227
+ value = Array . isArray ( prev )
228
+ ? prev . concat ( serializedCookie )
229
+ : [ prev , serializedCookie ]
230
+ }
231
+ this . ctx . res . setHeader ( 'Set-Cookie' , value )
183
232
}
184
233
185
234
return value
186
235
}
187
236
188
- getCookie ( key , isJson ) {
237
+ getCookie ( key ) {
189
238
if ( ! this . options . cookie || ( process . server && ! this . ctx . req ) ) {
190
239
return
191
240
}
192
241
193
242
const _key = this . options . cookie . prefix + key
194
243
195
- const cookieStr = process . client
196
- ? document . cookie
197
- : this . ctx . req . headers . cookie
244
+ const cookies = this . getCookies ( )
198
245
199
- const cookies = parseCookie ( cookieStr || '' ) || { }
200
- const value = cookies [ _key ]
201
- if ( value === 'false' ) {
202
- return false
203
- }
204
- return isJson ? JSON . parse ( value ) : value
246
+ const value = cookies [ _key ] ? decodeURIComponent ( cookies [ _key ] ) : undefined
247
+
248
+ return decodeValue ( value )
249
+ }
250
+
251
+ removeCookie ( key , options ) {
252
+ this . setCookie ( key , undefined , options )
205
253
}
206
254
}
0 commit comments