@aaltra-devs/aaltra_universal-storage 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -0
- package/README.md +64 -0
- package/dist/lib.esm.js +302 -0
- package/dist/lib.js +306 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, Aaltra BVB (https://www.aaltra.eu/)
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Universal Storage Helper
|
|
2
|
+
|
|
3
|
+
Universal storage helper for Nuxt projects with tweaks to facilitate use with older browsers.
|
|
4
|
+
|
|
5
|
+
The project is based on the equivalent [nuxt community module](https://github.com/nuxt-community/universal-storage-module).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
1. `npm i --save @aaltra-devs/aaltra_universal-storage`
|
|
10
|
+
|
|
11
|
+
2. Create a nuxt plugin:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import Storage from "@aaltra-devs/aaltra_universal-storage";
|
|
15
|
+
|
|
16
|
+
export default function (ctx, inject) {
|
|
17
|
+
const options = {
|
|
18
|
+
vuex: {
|
|
19
|
+
namespace: "storage",
|
|
20
|
+
},
|
|
21
|
+
cookie: {
|
|
22
|
+
prefix: "",
|
|
23
|
+
options: {
|
|
24
|
+
path: "/",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
localStorage: {
|
|
28
|
+
prefix: "",
|
|
29
|
+
},
|
|
30
|
+
ignoreExceptions: false,
|
|
31
|
+
};
|
|
32
|
+
const storage = new Storage(ctx, options);
|
|
33
|
+
ctx.$storage = storage;
|
|
34
|
+
inject("storage", storage);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
- Universal storage support for Nuxt.js applications
|
|
41
|
+
- Works on both client and server side
|
|
42
|
+
- Supports localStorage, cookies, and Vuex state
|
|
43
|
+
- Synchronization between different storage mechanisms
|
|
44
|
+
- Compatible with IE and Safari 10
|
|
45
|
+
- Exception handling options
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
|
|
49
|
+
### Methods
|
|
50
|
+
|
|
51
|
+
- `setUniversal(key, value)` - Set value in all storage types
|
|
52
|
+
- `getUniversal(key)` - Get value from any storage type
|
|
53
|
+
- `syncUniversal(key, defaultValue)` - Sync value across all storage types
|
|
54
|
+
- `removeUniversal(key)` - Remove value from all storage types
|
|
55
|
+
- `setState(key, value)` - Set value in Vuex state
|
|
56
|
+
- `getState(key)` - Get value from Vuex state
|
|
57
|
+
- `setLocalStorage(key, value)` - Set value in localStorage
|
|
58
|
+
- `getLocalStorage(key)` - Get value from localStorage
|
|
59
|
+
- `setCookie(key, value, options)` - Set cookie value
|
|
60
|
+
- `getCookie(key)` - Get cookie value
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
BSD-3-Clause
|
package/dist/lib.esm.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import Vue from 'vue';
|
|
2
|
+
import getProp from 'dotprop';
|
|
3
|
+
import { parse, serialize } from 'cookie';
|
|
4
|
+
|
|
5
|
+
function _classCallCheck(a, n) {
|
|
6
|
+
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
7
|
+
}
|
|
8
|
+
function _defineProperties(e, r) {
|
|
9
|
+
for (var t = 0; t < r.length; t++) {
|
|
10
|
+
var o = r[t];
|
|
11
|
+
o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function _createClass(e, r, t) {
|
|
15
|
+
return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
|
|
16
|
+
writable: !1
|
|
17
|
+
}), e;
|
|
18
|
+
}
|
|
19
|
+
function _toPrimitive(t, r) {
|
|
20
|
+
if ("object" != typeof t || !t) return t;
|
|
21
|
+
var e = t[Symbol.toPrimitive];
|
|
22
|
+
if (void 0 !== e) {
|
|
23
|
+
var i = e.call(t, r || "default");
|
|
24
|
+
if ("object" != typeof i) return i;
|
|
25
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
26
|
+
}
|
|
27
|
+
return ("string" === r ? String : Number)(t);
|
|
28
|
+
}
|
|
29
|
+
function _toPropertyKey(t) {
|
|
30
|
+
var i = _toPrimitive(t, "string");
|
|
31
|
+
return "symbol" == typeof i ? i : i + "";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var Storage = /*#__PURE__*/function () {
|
|
35
|
+
function Storage(ctx, options) {
|
|
36
|
+
_classCallCheck(this, Storage);
|
|
37
|
+
this.ctx = ctx;
|
|
38
|
+
this.options = options;
|
|
39
|
+
this._initState(options.initialState);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ------------------------------------
|
|
43
|
+
// Universal
|
|
44
|
+
// ------------------------------------
|
|
45
|
+
return _createClass(Storage, [{
|
|
46
|
+
key: "setUniversal",
|
|
47
|
+
value: function setUniversal(key, value) {
|
|
48
|
+
// Local state
|
|
49
|
+
this.setState(key, value);
|
|
50
|
+
// Cookies
|
|
51
|
+
this.setCookie(key, value);
|
|
52
|
+
// Local Storage
|
|
53
|
+
this.setLocalStorage(key, value);
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
}, {
|
|
57
|
+
key: "getUniversal",
|
|
58
|
+
value: function getUniversal(key) {
|
|
59
|
+
// Local state
|
|
60
|
+
var value = this.getState(key);
|
|
61
|
+
|
|
62
|
+
// Cookies
|
|
63
|
+
if (isUnset(value)) {
|
|
64
|
+
value = this.getCookie(key);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Local Storage
|
|
68
|
+
if (isUnset(value)) {
|
|
69
|
+
value = this.getLocalStorage(key);
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
}, {
|
|
74
|
+
key: "syncUniversal",
|
|
75
|
+
value: function syncUniversal(key, defaultValue) {
|
|
76
|
+
var value = this.getUniversal(key);
|
|
77
|
+
if (isUnset(value) && isSet(defaultValue)) {
|
|
78
|
+
value = defaultValue;
|
|
79
|
+
}
|
|
80
|
+
if (isSet(value)) {
|
|
81
|
+
this.setUniversal(key, value);
|
|
82
|
+
}
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
}, {
|
|
86
|
+
key: "removeUniversal",
|
|
87
|
+
value: function removeUniversal(key) {
|
|
88
|
+
this.removeState(key);
|
|
89
|
+
this.removeLocalStorage(key);
|
|
90
|
+
this.removeCookie(key);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ------------------------------------
|
|
94
|
+
// Local state (reactive)
|
|
95
|
+
// ------------------------------------
|
|
96
|
+
}, {
|
|
97
|
+
key: "_initState",
|
|
98
|
+
value: function _initState(initData) {
|
|
99
|
+
// Private state is suitable to keep information not being exposed to Vuex store
|
|
100
|
+
// This helps prevent stealing token from SSR response HTML
|
|
101
|
+
Vue.set(this, "_state", {});
|
|
102
|
+
|
|
103
|
+
// Use vuex for local state's if possible
|
|
104
|
+
this._useVuex = this.options.vuex && this.ctx.store;
|
|
105
|
+
if (this._useVuex) {
|
|
106
|
+
var storeModule = {
|
|
107
|
+
namespaced: true,
|
|
108
|
+
state: function state() {
|
|
109
|
+
return {};
|
|
110
|
+
},
|
|
111
|
+
mutations: {
|
|
112
|
+
SET: function SET(state, payload) {
|
|
113
|
+
Vue.set(state, payload.key, payload.value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
this.ctx.store.registerModule(this.options.vuex.namespace, storeModule, {
|
|
118
|
+
preserveState: Boolean(this.ctx.store.state[this.options.vuex.namespace])
|
|
119
|
+
});
|
|
120
|
+
this.state = this.ctx.store.state[this.options.vuex.namespace];
|
|
121
|
+
} else {
|
|
122
|
+
Vue.set(this, "state", {});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Synchronise initial values
|
|
126
|
+
for (var key in initData) {
|
|
127
|
+
this.syncUniversal(key, initData[key]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}, {
|
|
131
|
+
key: "setState",
|
|
132
|
+
value: function setState(key, value) {
|
|
133
|
+
if (key[0] === "_") {
|
|
134
|
+
Vue.set(this._state, key, value);
|
|
135
|
+
} else {
|
|
136
|
+
if (this._useVuex) {
|
|
137
|
+
this.ctx.store.commit(this.options.vuex.namespace + "/SET", {
|
|
138
|
+
key: key,
|
|
139
|
+
value: value
|
|
140
|
+
});
|
|
141
|
+
} else {
|
|
142
|
+
Vue.set(this.state, key, value);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
}, {
|
|
148
|
+
key: "getState",
|
|
149
|
+
value: function getState(key) {
|
|
150
|
+
if (key[0] !== "_") {
|
|
151
|
+
return this.state[key];
|
|
152
|
+
} else {
|
|
153
|
+
return this._state[key];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}, {
|
|
157
|
+
key: "watchState",
|
|
158
|
+
value: function watchState(key, fn) {
|
|
159
|
+
var _this = this;
|
|
160
|
+
if (this._useVuex) {
|
|
161
|
+
return this.ctx.store.watch(function (state) {
|
|
162
|
+
return getProp(state[_this.options.vuex.namespace], key);
|
|
163
|
+
}, fn);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}, {
|
|
167
|
+
key: "removeState",
|
|
168
|
+
value: function removeState(key) {
|
|
169
|
+
this.setState(key, undefined);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ------------------------------------
|
|
173
|
+
// Local storage
|
|
174
|
+
// ------------------------------------
|
|
175
|
+
}, {
|
|
176
|
+
key: "setLocalStorage",
|
|
177
|
+
value: function setLocalStorage(key, value) {
|
|
178
|
+
if (typeof localStorage === "undefined" || !this.options.localStorage) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
var _key = this.options.localStorage.prefix + key;
|
|
182
|
+
try {
|
|
183
|
+
if (isObjectOrArray(value)) {
|
|
184
|
+
localStorage.setItem(_key, JSON.stringify(value));
|
|
185
|
+
} else {
|
|
186
|
+
localStorage.setItem(_key, value);
|
|
187
|
+
}
|
|
188
|
+
} catch (e) {
|
|
189
|
+
if (!this.options.ignoreExceptions) {
|
|
190
|
+
throw e;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
}, {
|
|
196
|
+
key: "getLocalStorage",
|
|
197
|
+
value: function getLocalStorage(key) {
|
|
198
|
+
if (typeof localStorage === "undefined" || !this.options.localStorage) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
var _key = this.options.localStorage.prefix + key;
|
|
202
|
+
var value = localStorage.getItem(_key);
|
|
203
|
+
return decodeValue(value);
|
|
204
|
+
}
|
|
205
|
+
}, {
|
|
206
|
+
key: "removeLocalStorage",
|
|
207
|
+
value: function removeLocalStorage(key) {
|
|
208
|
+
if (typeof localStorage === "undefined" || !this.options.localStorage) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
var _key = this.options.localStorage.prefix + key;
|
|
212
|
+
localStorage.removeItem(_key);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ------------------------------------
|
|
216
|
+
// Cookies
|
|
217
|
+
// ------------------------------------
|
|
218
|
+
}, {
|
|
219
|
+
key: "getCookies",
|
|
220
|
+
value: function getCookies() {
|
|
221
|
+
var cookieStr = process.client ? document.cookie : this.ctx.req.headers.cookie;
|
|
222
|
+
return parse(cookieStr || "") || {};
|
|
223
|
+
}
|
|
224
|
+
}, {
|
|
225
|
+
key: "setCookie",
|
|
226
|
+
value: function setCookie(key, value) {
|
|
227
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
228
|
+
if (!this.options.cookie) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
var _key = this.options.cookie.prefix + key;
|
|
232
|
+
var _options = Object.assign({}, this.options.cookie.options, options);
|
|
233
|
+
var _value = encodeValue(value);
|
|
234
|
+
var serializedCookie = serialize(_key, _value, _options);
|
|
235
|
+
if (process.client) {
|
|
236
|
+
// Set in browser
|
|
237
|
+
document.cookie = serializedCookie;
|
|
238
|
+
} else if (process.server && this.ctx.res) {
|
|
239
|
+
// Send Set-Cookie header from server side
|
|
240
|
+
this.ctx.res.setHeader("Set-Cookie", serializedCookie);
|
|
241
|
+
}
|
|
242
|
+
return value;
|
|
243
|
+
}
|
|
244
|
+
}, {
|
|
245
|
+
key: "getCookie",
|
|
246
|
+
value: function getCookie(key) {
|
|
247
|
+
if (!this.options.cookie || process.server && !this.ctx.req) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
var _key = this.options.cookie.prefix + key;
|
|
251
|
+
var cookies = this.getCookies();
|
|
252
|
+
var value = cookies[_key] ? decodeURIComponent(cookies[_key]) : undefined;
|
|
253
|
+
return decodeValue(value);
|
|
254
|
+
}
|
|
255
|
+
}, {
|
|
256
|
+
key: "removeCookie",
|
|
257
|
+
value: function removeCookie(key, options) {
|
|
258
|
+
this.setCookie(key, undefined, Object.assign({
|
|
259
|
+
maxAge: -1
|
|
260
|
+
}, options));
|
|
261
|
+
}
|
|
262
|
+
}]);
|
|
263
|
+
}(); // ------------------------------------
|
|
264
|
+
function isUnset(o) {
|
|
265
|
+
return typeof o === "undefined" || o === null;
|
|
266
|
+
}
|
|
267
|
+
function isSet(o) {
|
|
268
|
+
return !isUnset(o);
|
|
269
|
+
}
|
|
270
|
+
function isObjectOrArray(obj) {
|
|
271
|
+
var type = Object.prototype.toString.call(obj);
|
|
272
|
+
return type === "[object Object]" || type === "[object Array]";
|
|
273
|
+
}
|
|
274
|
+
function encodeValue(val) {
|
|
275
|
+
if (typeof val === "string") {
|
|
276
|
+
return val;
|
|
277
|
+
}
|
|
278
|
+
return JSON.stringify(val);
|
|
279
|
+
}
|
|
280
|
+
function decodeValue(val) {
|
|
281
|
+
// Find current value's type
|
|
282
|
+
var type = Object.prototype.toString.call(val);
|
|
283
|
+
|
|
284
|
+
// If value is an object, return object
|
|
285
|
+
if (type === "[object Object]") {
|
|
286
|
+
return val;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// If value is somehow undefined, return as is (erroneous code)
|
|
290
|
+
if (type === "undefined") {
|
|
291
|
+
return val;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Finally try to parse it as json, or fallback to original value
|
|
295
|
+
try {
|
|
296
|
+
return JSON.parse(val);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
return val;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export default Storage;
|
package/dist/lib.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
4
|
+
|
|
5
|
+
var Vue = _interopDefault(require('vue'));
|
|
6
|
+
var getProp = _interopDefault(require('dotprop'));
|
|
7
|
+
var cookie = require('cookie');
|
|
8
|
+
|
|
9
|
+
function _classCallCheck(a, n) {
|
|
10
|
+
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
11
|
+
}
|
|
12
|
+
function _defineProperties(e, r) {
|
|
13
|
+
for (var t = 0; t < r.length; t++) {
|
|
14
|
+
var o = r[t];
|
|
15
|
+
o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function _createClass(e, r, t) {
|
|
19
|
+
return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
|
|
20
|
+
writable: !1
|
|
21
|
+
}), e;
|
|
22
|
+
}
|
|
23
|
+
function _toPrimitive(t, r) {
|
|
24
|
+
if ("object" != typeof t || !t) return t;
|
|
25
|
+
var e = t[Symbol.toPrimitive];
|
|
26
|
+
if (void 0 !== e) {
|
|
27
|
+
var i = e.call(t, r || "default");
|
|
28
|
+
if ("object" != typeof i) return i;
|
|
29
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
30
|
+
}
|
|
31
|
+
return ("string" === r ? String : Number)(t);
|
|
32
|
+
}
|
|
33
|
+
function _toPropertyKey(t) {
|
|
34
|
+
var i = _toPrimitive(t, "string");
|
|
35
|
+
return "symbol" == typeof i ? i : i + "";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var Storage = /*#__PURE__*/function () {
|
|
39
|
+
function Storage(ctx, options) {
|
|
40
|
+
_classCallCheck(this, Storage);
|
|
41
|
+
this.ctx = ctx;
|
|
42
|
+
this.options = options;
|
|
43
|
+
this._initState(options.initialState);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ------------------------------------
|
|
47
|
+
// Universal
|
|
48
|
+
// ------------------------------------
|
|
49
|
+
return _createClass(Storage, [{
|
|
50
|
+
key: "setUniversal",
|
|
51
|
+
value: function setUniversal(key, value) {
|
|
52
|
+
// Local state
|
|
53
|
+
this.setState(key, value);
|
|
54
|
+
// Cookies
|
|
55
|
+
this.setCookie(key, value);
|
|
56
|
+
// Local Storage
|
|
57
|
+
this.setLocalStorage(key, value);
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
}, {
|
|
61
|
+
key: "getUniversal",
|
|
62
|
+
value: function getUniversal(key) {
|
|
63
|
+
// Local state
|
|
64
|
+
var value = this.getState(key);
|
|
65
|
+
|
|
66
|
+
// Cookies
|
|
67
|
+
if (isUnset(value)) {
|
|
68
|
+
value = this.getCookie(key);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Local Storage
|
|
72
|
+
if (isUnset(value)) {
|
|
73
|
+
value = this.getLocalStorage(key);
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
}, {
|
|
78
|
+
key: "syncUniversal",
|
|
79
|
+
value: function syncUniversal(key, defaultValue) {
|
|
80
|
+
var value = this.getUniversal(key);
|
|
81
|
+
if (isUnset(value) && isSet(defaultValue)) {
|
|
82
|
+
value = defaultValue;
|
|
83
|
+
}
|
|
84
|
+
if (isSet(value)) {
|
|
85
|
+
this.setUniversal(key, value);
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
}, {
|
|
90
|
+
key: "removeUniversal",
|
|
91
|
+
value: function removeUniversal(key) {
|
|
92
|
+
this.removeState(key);
|
|
93
|
+
this.removeLocalStorage(key);
|
|
94
|
+
this.removeCookie(key);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ------------------------------------
|
|
98
|
+
// Local state (reactive)
|
|
99
|
+
// ------------------------------------
|
|
100
|
+
}, {
|
|
101
|
+
key: "_initState",
|
|
102
|
+
value: function _initState(initData) {
|
|
103
|
+
// Private state is suitable to keep information not being exposed to Vuex store
|
|
104
|
+
// This helps prevent stealing token from SSR response HTML
|
|
105
|
+
Vue.set(this, "_state", {});
|
|
106
|
+
|
|
107
|
+
// Use vuex for local state's if possible
|
|
108
|
+
this._useVuex = this.options.vuex && this.ctx.store;
|
|
109
|
+
if (this._useVuex) {
|
|
110
|
+
var storeModule = {
|
|
111
|
+
namespaced: true,
|
|
112
|
+
state: function state() {
|
|
113
|
+
return {};
|
|
114
|
+
},
|
|
115
|
+
mutations: {
|
|
116
|
+
SET: function SET(state, payload) {
|
|
117
|
+
Vue.set(state, payload.key, payload.value);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
this.ctx.store.registerModule(this.options.vuex.namespace, storeModule, {
|
|
122
|
+
preserveState: Boolean(this.ctx.store.state[this.options.vuex.namespace])
|
|
123
|
+
});
|
|
124
|
+
this.state = this.ctx.store.state[this.options.vuex.namespace];
|
|
125
|
+
} else {
|
|
126
|
+
Vue.set(this, "state", {});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Synchronise initial values
|
|
130
|
+
for (var key in initData) {
|
|
131
|
+
this.syncUniversal(key, initData[key]);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}, {
|
|
135
|
+
key: "setState",
|
|
136
|
+
value: function setState(key, value) {
|
|
137
|
+
if (key[0] === "_") {
|
|
138
|
+
Vue.set(this._state, key, value);
|
|
139
|
+
} else {
|
|
140
|
+
if (this._useVuex) {
|
|
141
|
+
this.ctx.store.commit(this.options.vuex.namespace + "/SET", {
|
|
142
|
+
key: key,
|
|
143
|
+
value: value
|
|
144
|
+
});
|
|
145
|
+
} else {
|
|
146
|
+
Vue.set(this.state, key, value);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return value;
|
|
150
|
+
}
|
|
151
|
+
}, {
|
|
152
|
+
key: "getState",
|
|
153
|
+
value: function getState(key) {
|
|
154
|
+
if (key[0] !== "_") {
|
|
155
|
+
return this.state[key];
|
|
156
|
+
} else {
|
|
157
|
+
return this._state[key];
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}, {
|
|
161
|
+
key: "watchState",
|
|
162
|
+
value: function watchState(key, fn) {
|
|
163
|
+
var _this = this;
|
|
164
|
+
if (this._useVuex) {
|
|
165
|
+
return this.ctx.store.watch(function (state) {
|
|
166
|
+
return getProp(state[_this.options.vuex.namespace], key);
|
|
167
|
+
}, fn);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}, {
|
|
171
|
+
key: "removeState",
|
|
172
|
+
value: function removeState(key) {
|
|
173
|
+
this.setState(key, undefined);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ------------------------------------
|
|
177
|
+
// Local storage
|
|
178
|
+
// ------------------------------------
|
|
179
|
+
}, {
|
|
180
|
+
key: "setLocalStorage",
|
|
181
|
+
value: function setLocalStorage(key, value) {
|
|
182
|
+
if (typeof localStorage === "undefined" || !this.options.localStorage) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
var _key = this.options.localStorage.prefix + key;
|
|
186
|
+
try {
|
|
187
|
+
if (isObjectOrArray(value)) {
|
|
188
|
+
localStorage.setItem(_key, JSON.stringify(value));
|
|
189
|
+
} else {
|
|
190
|
+
localStorage.setItem(_key, value);
|
|
191
|
+
}
|
|
192
|
+
} catch (e) {
|
|
193
|
+
if (!this.options.ignoreExceptions) {
|
|
194
|
+
throw e;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
}, {
|
|
200
|
+
key: "getLocalStorage",
|
|
201
|
+
value: function getLocalStorage(key) {
|
|
202
|
+
if (typeof localStorage === "undefined" || !this.options.localStorage) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
var _key = this.options.localStorage.prefix + key;
|
|
206
|
+
var value = localStorage.getItem(_key);
|
|
207
|
+
return decodeValue(value);
|
|
208
|
+
}
|
|
209
|
+
}, {
|
|
210
|
+
key: "removeLocalStorage",
|
|
211
|
+
value: function removeLocalStorage(key) {
|
|
212
|
+
if (typeof localStorage === "undefined" || !this.options.localStorage) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
var _key = this.options.localStorage.prefix + key;
|
|
216
|
+
localStorage.removeItem(_key);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ------------------------------------
|
|
220
|
+
// Cookies
|
|
221
|
+
// ------------------------------------
|
|
222
|
+
}, {
|
|
223
|
+
key: "getCookies",
|
|
224
|
+
value: function getCookies() {
|
|
225
|
+
var cookieStr = process.client ? document.cookie : this.ctx.req.headers.cookie;
|
|
226
|
+
return cookie.parse(cookieStr || "") || {};
|
|
227
|
+
}
|
|
228
|
+
}, {
|
|
229
|
+
key: "setCookie",
|
|
230
|
+
value: function setCookie(key, value) {
|
|
231
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
232
|
+
if (!this.options.cookie) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
var _key = this.options.cookie.prefix + key;
|
|
236
|
+
var _options = Object.assign({}, this.options.cookie.options, options);
|
|
237
|
+
var _value = encodeValue(value);
|
|
238
|
+
var serializedCookie = cookie.serialize(_key, _value, _options);
|
|
239
|
+
if (process.client) {
|
|
240
|
+
// Set in browser
|
|
241
|
+
document.cookie = serializedCookie;
|
|
242
|
+
} else if (process.server && this.ctx.res) {
|
|
243
|
+
// Send Set-Cookie header from server side
|
|
244
|
+
this.ctx.res.setHeader("Set-Cookie", serializedCookie);
|
|
245
|
+
}
|
|
246
|
+
return value;
|
|
247
|
+
}
|
|
248
|
+
}, {
|
|
249
|
+
key: "getCookie",
|
|
250
|
+
value: function getCookie(key) {
|
|
251
|
+
if (!this.options.cookie || process.server && !this.ctx.req) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
var _key = this.options.cookie.prefix + key;
|
|
255
|
+
var cookies = this.getCookies();
|
|
256
|
+
var value = cookies[_key] ? decodeURIComponent(cookies[_key]) : undefined;
|
|
257
|
+
return decodeValue(value);
|
|
258
|
+
}
|
|
259
|
+
}, {
|
|
260
|
+
key: "removeCookie",
|
|
261
|
+
value: function removeCookie(key, options) {
|
|
262
|
+
this.setCookie(key, undefined, Object.assign({
|
|
263
|
+
maxAge: -1
|
|
264
|
+
}, options));
|
|
265
|
+
}
|
|
266
|
+
}]);
|
|
267
|
+
}(); // ------------------------------------
|
|
268
|
+
function isUnset(o) {
|
|
269
|
+
return typeof o === "undefined" || o === null;
|
|
270
|
+
}
|
|
271
|
+
function isSet(o) {
|
|
272
|
+
return !isUnset(o);
|
|
273
|
+
}
|
|
274
|
+
function isObjectOrArray(obj) {
|
|
275
|
+
var type = Object.prototype.toString.call(obj);
|
|
276
|
+
return type === "[object Object]" || type === "[object Array]";
|
|
277
|
+
}
|
|
278
|
+
function encodeValue(val) {
|
|
279
|
+
if (typeof val === "string") {
|
|
280
|
+
return val;
|
|
281
|
+
}
|
|
282
|
+
return JSON.stringify(val);
|
|
283
|
+
}
|
|
284
|
+
function decodeValue(val) {
|
|
285
|
+
// Find current value's type
|
|
286
|
+
var type = Object.prototype.toString.call(val);
|
|
287
|
+
|
|
288
|
+
// If value is an object, return object
|
|
289
|
+
if (type === "[object Object]") {
|
|
290
|
+
return val;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// If value is somehow undefined, return as is (erroneous code)
|
|
294
|
+
if (type === "undefined") {
|
|
295
|
+
return val;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Finally try to parse it as json, or fallback to original value
|
|
299
|
+
try {
|
|
300
|
+
return JSON.parse(val);
|
|
301
|
+
} catch (error) {
|
|
302
|
+
return val;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
module.exports = Storage;
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aaltra-devs/aaltra_universal-storage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A universal storage library for Nuxt projects.",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "rollup -c --environment BUILD:production"
|
|
7
|
+
},
|
|
8
|
+
"main": "./dist/lib.js",
|
|
9
|
+
"module": "./dist/lib.esm.js",
|
|
10
|
+
"author": "Aaltra <info@aaltra.eu> (https://www.aaltra.eu/)",
|
|
11
|
+
"license": "BSD-3-Clause",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://bitbucket.org/aaltra/aaltra_universal-storage.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"nuxt",
|
|
18
|
+
"storage",
|
|
19
|
+
"universal",
|
|
20
|
+
"cookie",
|
|
21
|
+
"localstorage",
|
|
22
|
+
"vuex"
|
|
23
|
+
],
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@babel/core": "^7.7.7",
|
|
26
|
+
"@babel/preset-env": "^7.7.7",
|
|
27
|
+
"@rollup/plugin-commonjs": "^11.0.0",
|
|
28
|
+
"@rollup/plugin-node-resolve": "^6.0.0",
|
|
29
|
+
"rollup": "^1.27.14",
|
|
30
|
+
"rollup-plugin-babel": "^4.3.3",
|
|
31
|
+
"rollup-plugin-vue": "^5.1.4"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"cookie": "^0.4.0",
|
|
35
|
+
"dotprop": "^1.2.0",
|
|
36
|
+
"vue": "^2.6.11"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
]
|
|
43
|
+
}
|