@aegis-framework/artemis 0.3.29 → 0.4.1
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 +1 -1
- package/dist/artemis.browser.js +4 -0
- package/dist/artemis.browser.js.map +24 -0
- package/dist/artemis.js +3 -1
- package/dist/artemis.js.map +23 -1
- package/dist/types/DOM.d.ts +383 -0
- package/dist/types/DOM.d.ts.map +1 -0
- package/dist/types/Debug.d.ts +118 -0
- package/dist/types/Debug.d.ts.map +1 -0
- package/dist/types/FileSystem.d.ts +69 -0
- package/dist/types/FileSystem.d.ts.map +1 -0
- package/dist/types/Form.d.ts +32 -0
- package/dist/types/Form.d.ts.map +1 -0
- package/dist/types/Platform.d.ts +93 -0
- package/dist/types/Platform.d.ts.map +1 -0
- package/dist/types/Preload.d.ts +26 -0
- package/dist/types/Preload.d.ts.map +1 -0
- package/dist/types/Request.d.ts +86 -0
- package/dist/types/Request.d.ts.map +1 -0
- package/dist/types/Space.d.ts +205 -0
- package/dist/types/Space.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/IndexedDB.d.ts +130 -0
- package/dist/types/SpaceAdapter/IndexedDB.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/LocalStorage.d.ts +125 -0
- package/dist/types/SpaceAdapter/LocalStorage.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/RemoteStorage.d.ts +122 -0
- package/dist/types/SpaceAdapter/RemoteStorage.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/SessionStorage.d.ts +30 -0
- package/dist/types/SpaceAdapter/SessionStorage.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/types.d.ts +76 -0
- package/dist/types/SpaceAdapter/types.d.ts.map +1 -0
- package/dist/types/Text.d.ts +47 -0
- package/dist/types/Text.d.ts.map +1 -0
- package/dist/types/Util.d.ts +31 -0
- package/dist/types/Util.d.ts.map +1 -0
- package/dist/types/browser.d.ts +7 -0
- package/dist/types/browser.d.ts.map +1 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +42 -53
- package/dist/artemis.min.js +0 -2
- package/dist/artemis.min.js.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/index.js +0 -10
- package/src/DOM.js +0 -993
- package/src/Debug.js +0 -233
- package/src/FileSystem.js +0 -109
- package/src/Form.js +0 -73
- package/src/Platform.js +0 -166
- package/src/Preload.js +0 -48
- package/src/Request.js +0 -163
- package/src/Space.js +0 -334
- package/src/SpaceAdapter/IndexedDB.js +0 -345
- package/src/SpaceAdapter/LocalStorage.js +0 -395
- package/src/SpaceAdapter/RemoteStorage.js +0 -225
- package/src/SpaceAdapter/SessionStorage.js +0 -46
- package/src/Text.js +0 -133
- package/src/Util.js +0 -55
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ==============================
|
|
3
|
-
* Local Storage Adapter
|
|
4
|
-
* ==============================
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The Local Storage Adapter provides the Space Class the ability to interact
|
|
9
|
-
* with the localStorage api found in most modern browsers.
|
|
10
|
-
*
|
|
11
|
-
* @class
|
|
12
|
-
*/
|
|
13
|
-
export class LocalStorage {
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Create a new LocalStorage. If no configuration is provided, the LocalStorage
|
|
17
|
-
* global object is used. The LocalStorage Adapter can provide independency
|
|
18
|
-
* by store name and space name.
|
|
19
|
-
*
|
|
20
|
-
* @constructor
|
|
21
|
-
* @param {Object} [configuration={name = '', version = '', store = ''}] - Configuration Object for the Adapter
|
|
22
|
-
* @param {string} configuration.name - Name of the Space
|
|
23
|
-
* @param {string} configuration.version - Version of the Space in Semantic versioning syntax
|
|
24
|
-
* @param {string} configuration.store - Name of the Object Store to use
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
constructor ({name = '', version = '', store = ''}) {
|
|
28
|
-
this.name = name;
|
|
29
|
-
this.version = version;
|
|
30
|
-
this.store = store;
|
|
31
|
-
|
|
32
|
-
this.upgrades = {};
|
|
33
|
-
|
|
34
|
-
if (this.version === '') {
|
|
35
|
-
this.numericVersion = 0;
|
|
36
|
-
} else {
|
|
37
|
-
this.numericVersion = parseInt (version.replace (/\./g, ''));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (name !== '' && version !== '' && store !== '') {
|
|
41
|
-
this.id = `${this.name}::${this.store}::${this.version}_`;
|
|
42
|
-
} else if (name !== '' && version !== '') {
|
|
43
|
-
this.id = `${this.name}::${this.version}_`;
|
|
44
|
-
} else if (name !== '') {
|
|
45
|
-
this.id = `${this.name}::_`;
|
|
46
|
-
} else {
|
|
47
|
-
this.id = '';
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Open the Storage Object
|
|
53
|
-
*
|
|
54
|
-
* @return {Promise}
|
|
55
|
-
*/
|
|
56
|
-
open () {
|
|
57
|
-
if (typeof this.storage === 'object' && !(this.storage instanceof Promise)) {
|
|
58
|
-
return Promise.resolve (this);
|
|
59
|
-
} else if (this.storage instanceof Promise) {
|
|
60
|
-
return this.storage;
|
|
61
|
-
} else {
|
|
62
|
-
this.storage = new Promise ((resolve) => {
|
|
63
|
-
let upgradesToApply = [];
|
|
64
|
-
|
|
65
|
-
// Check if this space is versioned
|
|
66
|
-
if (this.version !== '') {
|
|
67
|
-
// Get the versionless part of the ID to check if an upgrade needs
|
|
68
|
-
// to ocurr based on the version available on storage and the current
|
|
69
|
-
// version.
|
|
70
|
-
let versionless = '';
|
|
71
|
-
if (this.name !== '' && this.version !== '' && this.store !== '') {
|
|
72
|
-
versionless = `${this.name}::${this.store}::`;
|
|
73
|
-
} else if (this.name !== '' && this.version !== '') {
|
|
74
|
-
versionless = `${this.name}::`;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Get all the currently stored keys that contain the versionless
|
|
78
|
-
// ID, which means they belong to this space
|
|
79
|
-
const storedVersions = Object.keys (window.localStorage).filter ((key) => {
|
|
80
|
-
return key.indexOf (versionless) === 0;
|
|
81
|
-
}).map ((key) => {
|
|
82
|
-
// Remove the versionless part of the ID and keep only the
|
|
83
|
-
// part of the key belonging to the ID
|
|
84
|
-
return key.replace (versionless, '').split ('_')[0];
|
|
85
|
-
}). filter ((key) => {
|
|
86
|
-
// Filter all that didn't match the versionless part fully
|
|
87
|
-
return key.indexOf ('::') === -1;
|
|
88
|
-
}).sort ();
|
|
89
|
-
|
|
90
|
-
if (storedVersions.length > 0) {
|
|
91
|
-
// We'll only take the lowest one every time
|
|
92
|
-
const oldVersion = storedVersions[0];
|
|
93
|
-
const oldVersionNumeric = parseInt (oldVersion.replace (/\./g, ''));
|
|
94
|
-
|
|
95
|
-
if (oldVersionNumeric < this.numericVersion) {
|
|
96
|
-
// Check what upgrade functions have been declared in their respective order
|
|
97
|
-
const availableUpgrades = Object.keys (this.upgrades).sort ();
|
|
98
|
-
|
|
99
|
-
// Find the first update that needs to be applied to the database given
|
|
100
|
-
// the old version it currently has.
|
|
101
|
-
const startFrom = availableUpgrades.findIndex (u => {
|
|
102
|
-
const [old, ] = u.split ('::');
|
|
103
|
-
return parseInt (old) === oldVersionNumeric;
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
if (startFrom > -1) {
|
|
107
|
-
upgradesToApply = availableUpgrades.slice (startFrom).filter ((u) => {
|
|
108
|
-
const [old, next] = u.split ('::');
|
|
109
|
-
return parseInt (old) < this.numericVersion && parseInt (next) <= this.numericVersion;
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Get the previous ID using the old version
|
|
114
|
-
let previousId = `${this.name}::${oldVersion}_`;
|
|
115
|
-
|
|
116
|
-
if (this.name !== '' && this.version !== '' && this.store !== '') {
|
|
117
|
-
previousId = `${this.name}::${this.store}::${oldVersion}_`;
|
|
118
|
-
} else if (this.name !== '' && this.version !== '') {
|
|
119
|
-
previousId = `${this.name}::${oldVersion}_`;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Get all keys from the previous version
|
|
123
|
-
const keys = Object.keys (window.localStorage).filter ((key) => {
|
|
124
|
-
return key.indexOf (previousId) === 0;
|
|
125
|
-
}).map ((key) => {
|
|
126
|
-
return key.replace (previousId, '');
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
for (const key of keys) {
|
|
130
|
-
// Get the value stored with the previous version
|
|
131
|
-
const previous = window.localStorage.getItem (`${previousId}${key}`);
|
|
132
|
-
|
|
133
|
-
// Re-insert the value using the new ID as a key
|
|
134
|
-
window.localStorage.setItem (this.id + key, previous);
|
|
135
|
-
|
|
136
|
-
// Delete the previous value.
|
|
137
|
-
window.localStorage.removeItem (`${previousId}${key}`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
resolve ({ upgrades: upgradesToApply });
|
|
143
|
-
}).then (({ upgrades }) => {
|
|
144
|
-
this.storage = window.localStorage;
|
|
145
|
-
return new Promise ((resolve) => {
|
|
146
|
-
const res = () => resolve (this);
|
|
147
|
-
this._upgrade (upgrades, res);
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
return this.storage;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Store a key-value pair
|
|
156
|
-
*
|
|
157
|
-
* @param {string} key - Key with which this value will be saved
|
|
158
|
-
* @param {Object|string|Number} - Value to save
|
|
159
|
-
* @return {Promise<{key, value}>}
|
|
160
|
-
*/
|
|
161
|
-
set (key, value) {
|
|
162
|
-
return this.open ().then (() => {
|
|
163
|
-
|
|
164
|
-
if (typeof value === 'object') {
|
|
165
|
-
this.storage.setItem (this.id + key, JSON.stringify (value));
|
|
166
|
-
} else {
|
|
167
|
-
this.storage.setItem (this.id + key, value);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return Promise.resolve ({key, value});
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Update a key-value pair. In difference with the set () method, the update
|
|
176
|
-
* method will use an Object.assign () in the case of objects so no value is
|
|
177
|
-
* lost.
|
|
178
|
-
*
|
|
179
|
-
* @param {string} key - Key with which this value will be saved
|
|
180
|
-
* @param {Object|string|Number} - Value to save
|
|
181
|
-
* @return {Promise<{key, value}>}
|
|
182
|
-
*/
|
|
183
|
-
update (key, value) {
|
|
184
|
-
return this.get (key).then ((currentValue) => {
|
|
185
|
-
if (typeof currentValue === 'object') {
|
|
186
|
-
if (typeof value === 'object') {
|
|
187
|
-
value = Object.assign ({}, currentValue, value);
|
|
188
|
-
}
|
|
189
|
-
this.storage.setItem (this.id + key, JSON.stringify (value));
|
|
190
|
-
} else {
|
|
191
|
-
this.storage.setItem (this.id + key, value);
|
|
192
|
-
}
|
|
193
|
-
return Promise.resolve ({key, value});
|
|
194
|
-
}).catch (() => {
|
|
195
|
-
return this.set (key, value);
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Retrieves a value from storage given it's key
|
|
201
|
-
*
|
|
202
|
-
* @param {string} - Key with which the value was saved
|
|
203
|
-
* @return {Promise<Object>|Promise<string>|Promise<Number>} - Resolves to the retreived value
|
|
204
|
-
* or its rejected if it doesn't exist
|
|
205
|
-
*/
|
|
206
|
-
get (key) {
|
|
207
|
-
return this.open ().then (() => {
|
|
208
|
-
return new Promise ((resolve, reject) => {
|
|
209
|
-
let value = null;
|
|
210
|
-
value = this.storage.getItem (this.id + key);
|
|
211
|
-
try {
|
|
212
|
-
const o = JSON.parse (value);
|
|
213
|
-
if (o && typeof o === 'object') {
|
|
214
|
-
value = o;
|
|
215
|
-
}
|
|
216
|
-
} catch (exception) {
|
|
217
|
-
// Unable to parse to JSON
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (typeof value !== 'undefined' && value !== null) {
|
|
221
|
-
resolve (value);
|
|
222
|
-
} else {
|
|
223
|
-
reject ();
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Retrieves all the values in the space in a key-value JSON object
|
|
232
|
-
*
|
|
233
|
-
* @return {Promise<Object>} - Resolves to the retreived values
|
|
234
|
-
*/
|
|
235
|
-
getAll () {
|
|
236
|
-
return this.keys ().then ((keys) => {
|
|
237
|
-
const values = {};
|
|
238
|
-
const promises = [];
|
|
239
|
-
for (const key of keys) {
|
|
240
|
-
promises.push (this.get (key).then ((value) => {
|
|
241
|
-
values[key] = value;
|
|
242
|
-
}));
|
|
243
|
-
}
|
|
244
|
-
return Promise.all (promises).then (() => {
|
|
245
|
-
return values;
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Check if the space contains a given key.
|
|
252
|
-
*
|
|
253
|
-
* @param {string} key - Key to look for.
|
|
254
|
-
* @return {Promise} Promise gets resolved if it exists and rejected if
|
|
255
|
-
* doesn't
|
|
256
|
-
*/
|
|
257
|
-
contains (key) {
|
|
258
|
-
return this.keys ().then ((keys) => {
|
|
259
|
-
if (keys.includes (key)) {
|
|
260
|
-
Promise.resolve ();
|
|
261
|
-
} else {
|
|
262
|
-
return Promise.reject ();
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Upgrade a Space Version
|
|
269
|
-
*
|
|
270
|
-
* @param oldVersion {string} - The version of the storage to be upgraded
|
|
271
|
-
* @param newVersion {string} - The version to be upgraded to
|
|
272
|
-
* @param callback {function} - Function to transform the old stored values to the new version's format
|
|
273
|
-
* @returns {Promise}
|
|
274
|
-
*/
|
|
275
|
-
upgrade (oldVersion, newVersion, callback) {
|
|
276
|
-
this.upgrades[`${parseInt (oldVersion.replace (/\./g, ''))}::${parseInt (newVersion.replace (/\./g, ''))}`] = callback;
|
|
277
|
-
return Promise.resolve ();
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// This function acts as a helper for the upgrade progress by executing the
|
|
281
|
-
// needed upgrade callbacks in the correct order and sychronously.
|
|
282
|
-
_upgrade (upgradesToApply, resolve) {
|
|
283
|
-
// Check if there are still upgrades to apply
|
|
284
|
-
if (upgradesToApply.length > 0) {
|
|
285
|
-
this.upgrades[upgradesToApply[0]].call (this, this).then (() => {
|
|
286
|
-
this._upgrade (upgradesToApply.slice (1), resolve);
|
|
287
|
-
}).catch ((e) => console.error (e));
|
|
288
|
-
} else {
|
|
289
|
-
resolve ();
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Rename a Space
|
|
295
|
-
*
|
|
296
|
-
* @param {string} name - New name to be used.
|
|
297
|
-
* @returns {Promise} - Result of the rename operation
|
|
298
|
-
*/
|
|
299
|
-
rename (name) {
|
|
300
|
-
// Check if the name is different
|
|
301
|
-
if (this.name !== name) {
|
|
302
|
-
return this.keys ().then ((keys) => {
|
|
303
|
-
// Save the previous Space id
|
|
304
|
-
const oldId = this.id;
|
|
305
|
-
|
|
306
|
-
// Set new object properties with the new name
|
|
307
|
-
this.name = name;
|
|
308
|
-
|
|
309
|
-
if (this.name !== '' && this.version !== '' && this.store !== '') {
|
|
310
|
-
this.id = `${this.name}::${this.store}::${this.version}_`;
|
|
311
|
-
} else if (this.name !== '' && this.version !== '') {
|
|
312
|
-
this.id = `${this.name}::${this.version}_`;
|
|
313
|
-
} else if (this.name !== '') {
|
|
314
|
-
this.id = `${this.name}::_`;
|
|
315
|
-
} else {
|
|
316
|
-
this.id = '';
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
const promises = [];
|
|
320
|
-
for (const key of keys) {
|
|
321
|
-
promises.push (this.set (key, this.storage.getItem (`${oldId}${key}`)).then (() => {
|
|
322
|
-
this.storage.removeItem (`${oldId}${key}`);
|
|
323
|
-
}));
|
|
324
|
-
}
|
|
325
|
-
return Promise.all (promises);
|
|
326
|
-
});
|
|
327
|
-
} else {
|
|
328
|
-
return Promise.reject ();
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Get the key that corresponds to a given index in the storage
|
|
334
|
-
*
|
|
335
|
-
* @param {Number} index - Index to get the key from
|
|
336
|
-
* @param {boolean} [full=false] - Whether to return the full key name including space id or just the key name
|
|
337
|
-
* @return {Promise<string>} - Resolves to the key's name
|
|
338
|
-
*/
|
|
339
|
-
key (index, full = false) {
|
|
340
|
-
return this.open ().then (() => {
|
|
341
|
-
if (full === true) {
|
|
342
|
-
return Promise.resolve (this.storage.key (index));
|
|
343
|
-
} else {
|
|
344
|
-
return Promise.resolve (this.storage.key (index).replace (this.id, ''));
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Return all keys stored in the space.
|
|
351
|
-
*
|
|
352
|
-
* @param {boolean} [full=false] - Whether to return the full key name including space id or just the key name
|
|
353
|
-
* @return {Promise<string[]>} - Array of keys
|
|
354
|
-
*/
|
|
355
|
-
keys (full = false) {
|
|
356
|
-
return this.open ().then (() => {
|
|
357
|
-
return Promise.resolve (Object.keys (this.storage).filter ((key) => {
|
|
358
|
-
return key.indexOf (this.id) === 0;
|
|
359
|
-
}).map ((key) => {
|
|
360
|
-
if (full === true) {
|
|
361
|
-
return key;
|
|
362
|
-
} else {
|
|
363
|
-
return key.replace (this.id, '');
|
|
364
|
-
}
|
|
365
|
-
}));
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Delete a value from the space given its key
|
|
371
|
-
*
|
|
372
|
-
* @param {string} key - Key of the item to delete
|
|
373
|
-
* @return {Promise<value>} - Resolves to the value of the deleted object
|
|
374
|
-
*/
|
|
375
|
-
remove (key) {
|
|
376
|
-
return this.get (key).then ((value) => {
|
|
377
|
-
this.storage.removeItem (this.id + key);
|
|
378
|
-
return Promise.resolve (value);
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* Clear the entire space
|
|
384
|
-
*
|
|
385
|
-
* @return {Promise} - Result of the clear operation
|
|
386
|
-
*/
|
|
387
|
-
clear () {
|
|
388
|
-
return this.keys ().then ((keys) => {
|
|
389
|
-
for (const key of keys) {
|
|
390
|
-
this.remove (key);
|
|
391
|
-
}
|
|
392
|
-
return Promise.resolve ();
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
}
|
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ==============================
|
|
3
|
-
* Remote Storage Adapter
|
|
4
|
-
* ==============================
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { Request } from './../Request';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* The Remote Storage Adapter provides the Space Class the ability to interact
|
|
11
|
-
* with a server in order to handle data persistance. The server's implementation
|
|
12
|
-
* is up to the developer but it will need to respond to this adapter's request
|
|
13
|
-
* formatting. This adapter uses the Request class to perfom its tasks.
|
|
14
|
-
*
|
|
15
|
-
* @class
|
|
16
|
-
*/
|
|
17
|
-
export class RemoteStorage {
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Create a new Remote Storage. This adapter requires an endpoint url where
|
|
21
|
-
* it will make the requests. If a store is defined, the request will be made
|
|
22
|
-
* using the store as an URL, for example, let's assume the following endpoint:
|
|
23
|
-
*
|
|
24
|
-
* https://example.com/api/v1/
|
|
25
|
-
*
|
|
26
|
-
* If no store is defined, then the requests will be made to that simple route,
|
|
27
|
-
* with a store definition, requests will be made to:
|
|
28
|
-
*
|
|
29
|
-
* https://example.com/api/v1/{myStore}/
|
|
30
|
-
*
|
|
31
|
-
* The key of each item in this store represents another part of the request
|
|
32
|
-
*
|
|
33
|
-
* https://example.com/api/v1/{key}/
|
|
34
|
-
*
|
|
35
|
-
* Or:
|
|
36
|
-
*
|
|
37
|
-
* https://example.com/api/v1/{myStore}/{key}/
|
|
38
|
-
*
|
|
39
|
-
* This adapter just as the IndexedDB, works with JSON objects instead of string or
|
|
40
|
-
* numeric values.
|
|
41
|
-
*
|
|
42
|
-
* @constructor
|
|
43
|
-
* @param {object} [configuration={name = '', version = '', store = '', endpoint = '', props = {}] - Configuration Object for the Adapter
|
|
44
|
-
* @param {string} configuration.name - Name of the Space
|
|
45
|
-
* @param {string} configuration.version - Version of the Space in Semantic versioning syntax
|
|
46
|
-
* @param {string} configuration.store - Name of the Object Store to use
|
|
47
|
-
* @param {string} configuration.endpoint - Endpoint URL where the requests will be made
|
|
48
|
-
* @param {string} configuration.props - Properties object to use for the fetch requests
|
|
49
|
-
*/
|
|
50
|
-
constructor ({name = '', version = '', store = '', endpoint = '', props = {}}) {
|
|
51
|
-
this.name = name;
|
|
52
|
-
this.version = version;
|
|
53
|
-
this.store = store;
|
|
54
|
-
this.endpoint = `${endpoint}${store}/`;
|
|
55
|
-
this.props = props;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Open the Storage Object
|
|
60
|
-
*
|
|
61
|
-
* @return {Promise<RemoteStorage>}
|
|
62
|
-
*/
|
|
63
|
-
open () {
|
|
64
|
-
if (typeof this.storage === 'undefined') {
|
|
65
|
-
this.storage = Request;
|
|
66
|
-
}
|
|
67
|
-
return Promise.resolve (this);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Store a key-value pair. This function sends a POST request to the server
|
|
72
|
-
*
|
|
73
|
-
* @param {string} key - Key with which this value will be saved
|
|
74
|
-
* @param {Object} value - Value to save
|
|
75
|
-
* @return {Promise<Response>}
|
|
76
|
-
*/
|
|
77
|
-
set (key, value) {
|
|
78
|
-
return this.open ().then (() => {
|
|
79
|
-
return this.storage.post (this.endpoint + key, value, this.props)
|
|
80
|
-
.then ((response) => response.json ())
|
|
81
|
-
.then ((value) => {
|
|
82
|
-
return Promise.resolve ({ key, value });
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Update a key-value pair. In difference with the set () method, the update
|
|
89
|
-
* method will use an Object.assign () in the case of objects so no value is
|
|
90
|
-
* lost. This function sends a PUT request to the server.
|
|
91
|
-
*
|
|
92
|
-
* @param {string} key - Key with which this value will be saved
|
|
93
|
-
* @param {Object} value - Value to save
|
|
94
|
-
* @return {Promise<Object>}
|
|
95
|
-
*/
|
|
96
|
-
update (key, value) {
|
|
97
|
-
return this.get (key).then ((currentValue) => {
|
|
98
|
-
return this.storage.put (this.endpoint + key, Object.assign ({}, currentValue, value), this.props)
|
|
99
|
-
.then ((response) => response.json ())
|
|
100
|
-
.then ((value) => {
|
|
101
|
-
return Promise.resolve ({ key, value });
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Retrieves a value from storage given it's key
|
|
108
|
-
*
|
|
109
|
-
* @param {string} - Key with which the value was saved
|
|
110
|
-
* @return {Promise<Object>} - Resolves to the retreived value or its rejected
|
|
111
|
-
* if it doesn't exist
|
|
112
|
-
*/
|
|
113
|
-
get (key) {
|
|
114
|
-
return this.open ().then (() => {
|
|
115
|
-
return this.storage.json (this.endpoint + key, {}, this.props);
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Retrieves all the values in the space in a key-value JSON object
|
|
121
|
-
*
|
|
122
|
-
* @return {Promise<Object>} - Resolves to the retreived values
|
|
123
|
-
*/
|
|
124
|
-
getAll () {
|
|
125
|
-
return this.open ().then (() => {
|
|
126
|
-
return this.storage.json (this.endpoint, {}, this.props);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Check if a space contains a given key.
|
|
132
|
-
*
|
|
133
|
-
* @param {string} key - Key to look for.
|
|
134
|
-
* @return {Promise} Promise gets resolved if it exists and rejected if it
|
|
135
|
-
* doesn't
|
|
136
|
-
*/
|
|
137
|
-
contains (key) {
|
|
138
|
-
return this.keys ().then ((keys) => {
|
|
139
|
-
if (keys.includes (key)) {
|
|
140
|
-
Promise.resolve ();
|
|
141
|
-
} else {
|
|
142
|
-
return Promise.reject ();
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Upgrading the Storage must be done on the server side, therefore this function
|
|
149
|
-
* always gets rejected.
|
|
150
|
-
*
|
|
151
|
-
* @returns {Promise}
|
|
152
|
-
*/
|
|
153
|
-
upgrade () {
|
|
154
|
-
return Promise.reject ();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Renaming the Storage must be done on the server side, therefore this function
|
|
159
|
-
* always gets rejected.
|
|
160
|
-
*
|
|
161
|
-
* @returns {Promise}
|
|
162
|
-
*/
|
|
163
|
-
rename () {
|
|
164
|
-
return Promise.reject ();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Getting a key by its index is not possible in this adapter, therefore this
|
|
169
|
-
* function always gets rejected.
|
|
170
|
-
*
|
|
171
|
-
* @return {Promise} - Promise Rejection
|
|
172
|
-
*/
|
|
173
|
-
key () {
|
|
174
|
-
return Promise.reject ();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Return all keys stored in the space. This makes a GET request to the full
|
|
179
|
-
* endpoint (the URL of the endpoint and store name) with a keys query
|
|
180
|
-
* parameter:
|
|
181
|
-
*
|
|
182
|
-
* https://example.com/api/v1/?keys=true
|
|
183
|
-
*
|
|
184
|
-
* Or:
|
|
185
|
-
*
|
|
186
|
-
* https://example.com/api/v1/{myStore}/?keys=true
|
|
187
|
-
*
|
|
188
|
-
* @return {Promise<string[]>} - Array of keys
|
|
189
|
-
*/
|
|
190
|
-
keys () {
|
|
191
|
-
return this.open ().then (() => {
|
|
192
|
-
return this.storage.json (this.endpoint, {keys: true}, this.props);
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Delete a value from the space given it's key. This function sends a DELETE
|
|
198
|
-
* request to the server.
|
|
199
|
-
*
|
|
200
|
-
* @param {string} key - Key of the item to delete
|
|
201
|
-
* @return {Promise<key, value>} - Resolves to the key and value of the deleted object
|
|
202
|
-
*/
|
|
203
|
-
remove (key) {
|
|
204
|
-
return this.open ().then (() => {
|
|
205
|
-
return this.storage.delete (this.endpoint + key, {}, this.props)
|
|
206
|
-
.then ((response) => response.json ())
|
|
207
|
-
.then ((value) => {
|
|
208
|
-
return Promise.resolve (key,value);
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Clear the entire space. This function sends a DELETE request to the server.
|
|
215
|
-
* The difference between a clear () and remove () operation is that the clear
|
|
216
|
-
* operation does not uses a key in the URL where the request is made.
|
|
217
|
-
*
|
|
218
|
-
* @return {Promise} - Result of the clear operation
|
|
219
|
-
*/
|
|
220
|
-
clear () {
|
|
221
|
-
return this.open ().then (() => {
|
|
222
|
-
return this.storage.delete (this.endpoint, {}, this.props);
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ==============================
|
|
3
|
-
* Session Storage Adapter
|
|
4
|
-
* ==============================
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { LocalStorage } from './LocalStorage';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* The Session Storage Adapter provides the Space Class the ability to interact
|
|
11
|
-
* with the sessionStorage api found in most modern browsers. Since this API
|
|
12
|
-
* shares pretty much the same methods to the local storage one, this class
|
|
13
|
-
* inherits from the LocalStorage adapter.
|
|
14
|
-
*
|
|
15
|
-
* @class
|
|
16
|
-
*/
|
|
17
|
-
export class SessionStorage extends LocalStorage {
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Create a new SessionStorage. If no configuration is provided, the SessionStorage
|
|
21
|
-
* global object is used.The SessionStorage Adapter can provide independency
|
|
22
|
-
* by store name and space name.
|
|
23
|
-
*
|
|
24
|
-
* @constructor
|
|
25
|
-
* @param {Object} [configuration={name = '', version = '', store = ''}] - Configuration Object for the Adapter
|
|
26
|
-
* @param {string} configuration.name - Name of the Space
|
|
27
|
-
* @param {string} configuration.version - Version of the Space in Semantic versioning syntax
|
|
28
|
-
* @param {string} configuration.store - Name of the Object Store to use
|
|
29
|
-
*
|
|
30
|
-
*/
|
|
31
|
-
constructor ({name = '', version = '', store = ''}) {
|
|
32
|
-
super ({name, version, store});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Open the Storage Object
|
|
37
|
-
|
|
38
|
-
* @return {Promise<SessionStorage>}
|
|
39
|
-
*/
|
|
40
|
-
open () {
|
|
41
|
-
if (typeof this.storage === 'undefined') {
|
|
42
|
-
this.storage = window.sessionStorage;
|
|
43
|
-
}
|
|
44
|
-
return Promise.resolve (this);
|
|
45
|
-
}
|
|
46
|
-
}
|