Skip to content

Commit d05fcca

Browse files
authored
feat: improve storage (#360)
1 parent e68c4eb commit d05fcca

File tree

2 files changed

+102
-35
lines changed

2 files changed

+102
-35
lines changed

lib/core/storage.js

Lines changed: 83 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import Vue from 'vue'
2-
import Cookies from 'js-cookie'
32
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'
75

86
export default class Storage {
97
constructor (ctx, options) {
@@ -17,38 +15,43 @@ export default class Storage {
1715
// Universal
1816
// ------------------------------------
1917

20-
setUniversal (key, value, isJson) {
18+
setUniversal (key, value) {
19+
// Unset null, undefined
20+
if (isUnset(value)) {
21+
return this.removeUniversal(key)
22+
}
23+
2124
// Local state
2225
this.setState(key, value)
2326

2427
// Cookies
2528
this.setCookie(key, value)
2629

2730
// Local Storage
28-
this.setLocalStorage(key, value, isJson)
31+
this.setLocalStorage(key, value)
2932

3033
return value
3134
}
3235

33-
getUniversal (key, isJson) {
36+
getUniversal (key) {
3437
// Local state
3538
let value = this.getState(key)
3639

3740
// Cookies
3841
if (isUnset(value)) {
39-
value = this.getCookie(key, isJson)
42+
value = this.getCookie(key)
4043
}
4144

4245
// Local Storage
4346
if (isUnset(value)) {
44-
value = this.getLocalStorage(key, isJson)
47+
value = this.getLocalStorage(key)
4548
}
4649

4750
return value
4851
}
4952

50-
syncUniversal (key, defaultValue, isJson) {
51-
let value = this.getUniversal(key, isJson)
53+
syncUniversal (key, defaultValue) {
54+
let value = this.getUniversal(key)
5255

5356
if (isUnset(value) && isSet(defaultValue)) {
5457
value = defaultValue
@@ -61,6 +64,12 @@ export default class Storage {
6164
return value
6265
}
6366

67+
removeUniversal (key) {
68+
this.removeState(key)
69+
this.removeLocalStorage(key)
70+
this.removeCookie(key)
71+
}
72+
6473
// ------------------------------------
6574
// Local state (reactive)
6675
// ------------------------------------
@@ -128,79 +137,118 @@ export default class Storage {
128137
}
129138
}
130139

140+
removeState (key) {
141+
this.setState(key, undefined)
142+
}
143+
131144
// ------------------------------------
132145
// Local storage
133146
// ------------------------------------
134147

135-
setLocalStorage (key, value, isJson) {
148+
setLocalStorage (key, value) {
149+
// Unset null, undefined
150+
if (isUnset(value)) {
151+
return this.removeLocalStorage(key)
152+
}
153+
136154
if (typeof localStorage === 'undefined' || !this.options.localStorage) {
137155
return
138156
}
139157

140158
const _key = this.options.localStorage.prefix + key
141159

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+
}
146166
}
147167

148168
return value
149169
}
150170

151-
getLocalStorage (key, isJson) {
171+
getLocalStorage (key) {
152172
if (typeof localStorage === 'undefined' || !this.options.localStorage) {
153173
return
154174
}
155175

156176
const _key = this.options.localStorage.prefix + key
157177

158178
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
161186
}
162187

163-
return isJson ? JSON.parse(value) : value
188+
const _key = this.options.localStorage.prefix + key
189+
localStorage.removeItem(_key)
164190
}
165191

166192
// ------------------------------------
167193
// Cookies
168194
// ------------------------------------
195+
getCookies () {
196+
const cookieStr = process.client
197+
? document.cookie
198+
: this.ctx.req.headers.cookie
199+
200+
return parseCookie(cookieStr || '') || {}
201+
}
169202

170203
setCookie (key, value, options = {}) {
171-
if (process.server || !this.options.cookie) {
204+
if (!this.options.cookie || (process.server && !this.ctx.res)) {
172205
return
173206
}
174207

175208
const _key = this.options.cookie.prefix + key
176-
177209
const _options = Object.assign({}, this.options.cookie.options, options)
210+
const _value = encodeValue(value)
178211

212+
// Unset null, undefined
179213
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)
183232
}
184233

185234
return value
186235
}
187236

188-
getCookie (key, isJson) {
237+
getCookie (key) {
189238
if (!this.options.cookie || (process.server && !this.ctx.req)) {
190239
return
191240
}
192241

193242
const _key = this.options.cookie.prefix + key
194243

195-
const cookieStr = process.client
196-
? document.cookie
197-
: this.ctx.req.headers.cookie
244+
const cookies = this.getCookies()
198245

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)
205253
}
206254
}

lib/core/utilities.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,22 @@ export function normalizePath (path = '') {
6363

6464
return result
6565
}
66+
67+
export function encodeValue (val) {
68+
if (typeof val === 'string') {
69+
return val
70+
}
71+
return JSON.stringify(val)
72+
}
73+
74+
export function decodeValue (val) {
75+
// Try to parse as json
76+
if (typeof val === 'string') {
77+
try {
78+
return JSON.parse(val)
79+
} catch (_) { }
80+
}
81+
82+
// Return as is
83+
return val
84+
}

0 commit comments

Comments
 (0)