@adonisjs/session 7.0.0-9 → 7.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.
Files changed (76) hide show
  1. package/build/chunk-2X5L327N.js +28 -0
  2. package/build/chunk-2X5L327N.js.map +1 -0
  3. package/build/chunk-7YIO32ZH.js +134 -0
  4. package/build/chunk-7YIO32ZH.js.map +1 -0
  5. package/build/chunk-K4OSGJVW.js +402 -0
  6. package/build/chunk-K4OSGJVW.js.map +1 -0
  7. package/build/chunk-S6P3TBEK.js +85 -0
  8. package/build/chunk-S6P3TBEK.js.map +1 -0
  9. package/build/chunk-TE5JP3SX.js +151 -0
  10. package/build/chunk-TE5JP3SX.js.map +1 -0
  11. package/build/chunk-WBAYBMJJ.js +15 -0
  12. package/build/chunk-WBAYBMJJ.js.map +1 -0
  13. package/build/config/session.stub +51 -0
  14. package/build/cookie-H7KRZB4T.js +56 -0
  15. package/build/cookie-H7KRZB4T.js.map +1 -0
  16. package/build/factories/main.d.ts +1 -0
  17. package/build/factories/main.js +50 -0
  18. package/build/factories/main.js.map +1 -0
  19. package/build/factories/session_middleware_factory.d.ts +27 -0
  20. package/build/file-YO7C2QWO.js +112 -0
  21. package/build/file-YO7C2QWO.js.map +1 -0
  22. package/build/index.d.ts +1 -3
  23. package/build/index.js +18 -14
  24. package/build/index.js.map +1 -0
  25. package/build/providers/session_provider.d.ts +22 -5
  26. package/build/providers/session_provider.js +51 -59
  27. package/build/providers/session_provider.js.map +1 -0
  28. package/build/redis-KDWIBKUQ.js +58 -0
  29. package/build/redis-KDWIBKUQ.js.map +1 -0
  30. package/build/src/client.d.ts +24 -19
  31. package/build/src/client.js +9 -81
  32. package/build/src/client.js.map +1 -0
  33. package/build/src/debug.d.ts +1 -1
  34. package/build/src/define_config.d.ts +24 -3
  35. package/build/src/{edge_plugin_adonisjs_session.d.ts → plugins/edge.d.ts} +1 -1
  36. package/build/src/plugins/edge.js +102 -0
  37. package/build/src/plugins/edge.js.map +1 -0
  38. package/build/src/plugins/japa/api_client.d.ts +75 -0
  39. package/build/src/plugins/japa/api_client.js +100 -0
  40. package/build/src/plugins/japa/api_client.js.map +1 -0
  41. package/build/src/plugins/japa/browser_client.d.ts +36 -0
  42. package/build/src/plugins/japa/browser_client.js +88 -0
  43. package/build/src/plugins/japa/browser_client.js.map +1 -0
  44. package/build/src/session.d.ts +21 -12
  45. package/build/src/session_middleware.d.ts +7 -4
  46. package/build/src/session_middleware.js +10 -59
  47. package/build/src/session_middleware.js.map +1 -0
  48. package/build/src/{drivers → stores}/cookie.d.ts +4 -4
  49. package/build/src/{drivers → stores}/file.d.ts +4 -4
  50. package/build/src/{drivers → stores}/memory.d.ts +3 -3
  51. package/build/src/{drivers → stores}/redis.d.ts +5 -5
  52. package/build/src/{types/main.d.ts → types.d.ts} +11 -31
  53. package/build/src/types.js +1 -0
  54. package/build/src/types.js.map +1 -0
  55. package/build/src/{store.d.ts → values_store.d.ts} +10 -10
  56. package/package.json +74 -43
  57. package/build/configure.js +0 -45
  58. package/build/src/debug.js +0 -10
  59. package/build/src/define_config.js +0 -42
  60. package/build/src/drivers/cookie.js +0 -60
  61. package/build/src/drivers/file.js +0 -133
  62. package/build/src/drivers/memory.js +0 -33
  63. package/build/src/drivers/redis.js +0 -70
  64. package/build/src/drivers_collection.d.ts +0 -22
  65. package/build/src/drivers_collection.js +0 -38
  66. package/build/src/edge_plugin_adonisjs_session.js +0 -92
  67. package/build/src/errors.js +0 -17
  68. package/build/src/helpers.d.ts +0 -6
  69. package/build/src/helpers.js +0 -43
  70. package/build/src/session.js +0 -384
  71. package/build/src/store.js +0 -159
  72. package/build/src/types/extended.d.ts +0 -19
  73. package/build/src/types/extended.js +0 -9
  74. package/build/src/types/main.js +0 -9
  75. package/build/stubs/config.stub +0 -49
  76. package/build/stubs/main.js +0 -10
@@ -1,92 +0,0 @@
1
- /*
2
- * @adonisjs/session
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import debug from './debug.js';
10
- /**
11
- * The edge plugin for AdonisJS Session adds tags to read
12
- * flash messages
13
- */
14
- export const edgePluginAdonisJSSession = (edge) => {
15
- debug('registering session tags with edge');
16
- edge.registerTag({
17
- tagName: 'flashMessage',
18
- seekable: true,
19
- block: true,
20
- compile(parser, buffer, token) {
21
- const expression = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
22
- const key = parser.utils.stringify(expression);
23
- /**
24
- * Write an if statement
25
- */
26
- buffer.writeStatement(`if (state.flashMessages.has(${key})) {`, token.filename, token.loc.start.line);
27
- /**
28
- * Define a local variable
29
- */
30
- buffer.writeExpression(`let message = state.flashMessages.get(${key})`, token.filename, token.loc.start.line);
31
- /**
32
- * Create a local variables scope and tell the parser about
33
- * the existence of the "message" variable
34
- */
35
- parser.stack.defineScope();
36
- parser.stack.defineVariable('message');
37
- /**
38
- * Process component children using the parser
39
- */
40
- token.children.forEach((child) => {
41
- parser.processToken(child, buffer);
42
- });
43
- /**
44
- * Clear the scope of the local variables before we
45
- * close the if statement
46
- */
47
- parser.stack.clearScope();
48
- /**
49
- * Close if statement
50
- */
51
- buffer.writeStatement(`}`, token.filename, token.loc.start.line);
52
- },
53
- });
54
- edge.registerTag({
55
- tagName: 'error',
56
- seekable: true,
57
- block: true,
58
- compile(parser, buffer, token) {
59
- const expression = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
60
- const key = parser.utils.stringify(expression);
61
- /**
62
- * Write an if statement
63
- */
64
- buffer.writeStatement(`if (!!state.flashMessages.get('errors', {})[${key}]) {`, token.filename, token.loc.start.line);
65
- /**
66
- * Define a local variable
67
- */
68
- buffer.writeExpression(`let messages = state.flashMessages.get('errors', {})[${key}]`, token.filename, token.loc.start.line);
69
- /**
70
- * Create a local variables scope and tell the parser about
71
- * the existence of the "messages" variable
72
- */
73
- parser.stack.defineScope();
74
- parser.stack.defineVariable('messages');
75
- /**
76
- * Process component children using the parser
77
- */
78
- token.children.forEach((child) => {
79
- parser.processToken(child, buffer);
80
- });
81
- /**
82
- * Clear the scope of the local variables before we
83
- * close the if statement
84
- */
85
- parser.stack.clearScope();
86
- /**
87
- * Close if statement
88
- */
89
- buffer.writeStatement(`}`, token.filename, token.loc.start.line);
90
- },
91
- });
92
- };
@@ -1,17 +0,0 @@
1
- /*
2
- * @adonisjs/session
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import { createError } from '@poppinss/utils';
10
- /**
11
- * Raised when session store is not mutable
12
- */
13
- export const E_SESSION_NOT_MUTABLE = createError('Session store is in readonly mode and cannot be mutated', 'E_SESSION_NOT_MUTABLE', 500);
14
- /**
15
- * Raised when session store has been initiated
16
- */
17
- export const E_SESSION_NOT_READY = createError('Session store has not been initiated. Make sure you have registered the session middleware', 'E_SESSION_NOT_READY', 500);
@@ -1,6 +0,0 @@
1
- import type { ApplicationService } from '@adonisjs/core/types';
2
- import type { SessionDriversList } from './types/main.js';
3
- /**
4
- * Lazily imports and registers a driver with the sessionDriversList
5
- */
6
- export declare function registerSessionDriver(app: ApplicationService, driverInUse: keyof SessionDriversList): Promise<void>;
@@ -1,43 +0,0 @@
1
- /*
2
- * @adonisjs/session
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import debug from './debug.js';
10
- import sessionDriversList from './drivers_collection.js';
11
- /**
12
- * Lazily imports and registers a driver with the sessionDriversList
13
- */
14
- export async function registerSessionDriver(app, driverInUse) {
15
- /**
16
- * Noop when the driver is already registered
17
- */
18
- if (sessionDriversList.list[driverInUse]) {
19
- return;
20
- }
21
- debug('registering %s driver', driverInUse);
22
- if (driverInUse === 'cookie') {
23
- const { CookieDriver } = await import('../src/drivers/cookie.js');
24
- sessionDriversList.extend('cookie', (config, ctx) => new CookieDriver(config.cookie, ctx));
25
- return;
26
- }
27
- if (driverInUse === 'memory') {
28
- const { MemoryDriver } = await import('../src/drivers/memory.js');
29
- sessionDriversList.extend('memory', () => new MemoryDriver());
30
- return;
31
- }
32
- if (driverInUse === 'file') {
33
- const { FileDriver } = await import('../src/drivers/file.js');
34
- sessionDriversList.extend('file', (config) => new FileDriver(config.file, config.age));
35
- return;
36
- }
37
- if (driverInUse === 'redis') {
38
- const { RedisDriver } = await import('../src/drivers/redis.js');
39
- const redis = await app.container.make('redis');
40
- sessionDriversList.extend('redis', (config) => new RedisDriver(redis, config.redis, config.age));
41
- return;
42
- }
43
- }
@@ -1,384 +0,0 @@
1
- /*
2
- * @adonisjs/session
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import lodash from '@poppinss/utils/lodash';
10
- import { cuid } from '@adonisjs/core/helpers';
11
- import { ReadOnlyStore, Store } from './store.js';
12
- import * as errors from './errors.js';
13
- import debug from './debug.js';
14
- /**
15
- * The session class exposes the API to read and write values to
16
- * the session store.
17
- *
18
- * A session instance is isolated between requests but
19
- * uses a centralized persistence store and
20
- */
21
- export class Session {
22
- #config;
23
- #driver;
24
- #emitter;
25
- #ctx;
26
- #readonly = false;
27
- #store;
28
- /**
29
- * Session id refers to the session id that will be committed
30
- * as a cookie during the response.
31
- */
32
- #sessionId;
33
- /**
34
- * Session id from cookie refers to the value we read from the
35
- * cookie during the HTTP request.
36
- *
37
- * This only might not exist during the first request. Also during
38
- * session id re-generation, this value will be different from
39
- * the session id.
40
- */
41
- #sessionIdFromCookie;
42
- /**
43
- * Store of flash messages that be written during the
44
- * HTTP request
45
- */
46
- responseFlashMessages = new Store({});
47
- /**
48
- * Store of flash messages for the current HTTP request.
49
- */
50
- flashMessages = new Store({});
51
- /**
52
- * The key to use for storing flash messages inside
53
- * the session store.
54
- */
55
- flashKey = '__flash__';
56
- /**
57
- * Session id for the current HTTP request
58
- */
59
- get sessionId() {
60
- return this.#sessionId;
61
- }
62
- /**
63
- * A boolean to know if a fresh session is created during
64
- * the request
65
- */
66
- get fresh() {
67
- return this.#sessionIdFromCookie === undefined;
68
- }
69
- /**
70
- * A boolean to know if session is in readonly
71
- * state
72
- */
73
- get readonly() {
74
- return this.#readonly;
75
- }
76
- /**
77
- * A boolean to know if session store has been initiated
78
- */
79
- get initiated() {
80
- return !!this.#store;
81
- }
82
- /**
83
- * A boolean to know if the session id has been re-generated
84
- * during the current request
85
- */
86
- get hasRegeneratedSession() {
87
- return !!(this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId);
88
- }
89
- /**
90
- * A boolean to know if the session store is empty
91
- */
92
- get isEmpty() {
93
- return this.#store?.isEmpty ?? true;
94
- }
95
- /**
96
- * A boolean to know if the session store has been
97
- * modified
98
- */
99
- get hasBeenModified() {
100
- return this.#store?.hasBeenModified ?? false;
101
- }
102
- constructor(config, driver, emitter, ctx) {
103
- this.#ctx = ctx;
104
- this.#config = config;
105
- this.#driver = driver;
106
- this.#emitter = emitter;
107
- this.#sessionIdFromCookie = ctx.request.cookie(config.cookieName, undefined);
108
- this.#sessionId = this.#sessionIdFromCookie || cuid();
109
- }
110
- /**
111
- * Returns the flash messages store for a given
112
- * mode
113
- */
114
- #getFlashStore(mode) {
115
- if (!this.#store) {
116
- throw new errors.E_SESSION_NOT_READY();
117
- }
118
- if (mode === 'write' && this.readonly) {
119
- throw new errors.E_SESSION_NOT_MUTABLE();
120
- }
121
- return this.responseFlashMessages;
122
- }
123
- /**
124
- * Returns the store instance for a given mode
125
- */
126
- #getStore(mode) {
127
- if (!this.#store) {
128
- throw new errors.E_SESSION_NOT_READY();
129
- }
130
- if (mode === 'write' && this.readonly) {
131
- throw new errors.E_SESSION_NOT_MUTABLE();
132
- }
133
- return this.#store;
134
- }
135
- /**
136
- * Initiates the session store. The method results in a noop
137
- * when called multiple times
138
- */
139
- async initiate(readonly) {
140
- if (this.#store) {
141
- return;
142
- }
143
- debug('initiating session (readonly: %s)', readonly);
144
- this.#readonly = readonly;
145
- const contents = await this.#driver.read(this.#sessionId);
146
- this.#store = new Store(contents);
147
- /**
148
- * Extract flash messages from the store and keep a local
149
- * copy of it.
150
- */
151
- if (this.has(this.flashKey)) {
152
- debug('reading flash data');
153
- if (this.#readonly) {
154
- this.flashMessages.update(this.get(this.flashKey, null));
155
- }
156
- else {
157
- this.flashMessages.update(this.pull(this.flashKey, null));
158
- }
159
- }
160
- /**
161
- * Share session with the templates. We assume the view property
162
- * is a reference to edge templates
163
- */
164
- if ('view' in this.#ctx) {
165
- this.#ctx.view.share({
166
- session: new ReadOnlyStore(this.#store.all()),
167
- flashMessages: new ReadOnlyStore(this.flashMessages.all()),
168
- old: function (key, defaultValue) {
169
- return this.flashMessages.get(key, defaultValue);
170
- },
171
- });
172
- }
173
- this.#emitter.emit('session:initiated', { session: this });
174
- }
175
- /**
176
- * Put a key-value pair to the session data store
177
- */
178
- put(key, value) {
179
- this.#getStore('write').set(key, value);
180
- }
181
- /**
182
- * Check if a key exists inside the datastore
183
- */
184
- has(key) {
185
- return this.#getStore('read').has(key);
186
- }
187
- /**
188
- * Get the value of a key from the session datastore.
189
- * You can specify a default value to use, when key
190
- * does not exists or has undefined value.
191
- */
192
- get(key, defaultValue) {
193
- return this.#getStore('read').get(key, defaultValue);
194
- }
195
- /**
196
- * Get everything from the session store
197
- */
198
- all() {
199
- return this.#getStore('read').all();
200
- }
201
- /**
202
- * Remove a key from the session datastore
203
- */
204
- forget(key) {
205
- return this.#getStore('write').unset(key);
206
- }
207
- /**
208
- * Read value for a key from the session datastore
209
- * and remove it simultaneously.
210
- */
211
- pull(key, defaultValue) {
212
- return this.#getStore('write').pull(key, defaultValue);
213
- }
214
- /**
215
- * Increment the value of a key inside the session
216
- * store.
217
- *
218
- * A new key will be defined if does not exists already.
219
- * The value of a new key will be 1
220
- */
221
- increment(key, steps = 1) {
222
- return this.#getStore('write').increment(key, steps);
223
- }
224
- /**
225
- * Increment the value of a key inside the session
226
- * store.
227
- *
228
- * A new key will be defined if does not exists already.
229
- * The value of a new key will be -1
230
- */
231
- decrement(key, steps = 1) {
232
- return this.#getStore('write').decrement(key, steps);
233
- }
234
- /**
235
- * Empty the session store
236
- */
237
- clear() {
238
- return this.#getStore('write').clear();
239
- }
240
- /**
241
- * Flash validation error messages. Make sure the error
242
- * is an instance of VineJS ValidationException
243
- */
244
- flashValidationErrors(error) {
245
- const errorsBag = error.messages.reduce((result, message) => {
246
- if (result[message.field]) {
247
- result[message.field].push(message.message);
248
- }
249
- else {
250
- result[message.field] = [message.message];
251
- }
252
- return result;
253
- }, {});
254
- this.flashExcept(['_csrf', '_method']);
255
- this.flash('errors', errorsBag);
256
- }
257
- flash(key, value) {
258
- if (typeof key === 'string') {
259
- if (value) {
260
- this.#getFlashStore('write').set(key, value);
261
- }
262
- }
263
- else {
264
- this.#getFlashStore('write').merge(key);
265
- }
266
- }
267
- /**
268
- * Flash form input data to the flash messages store
269
- */
270
- flashAll() {
271
- return this.#getFlashStore('write').set('input', this.#ctx.request.original());
272
- }
273
- /**
274
- * Flash form input data (except some keys) to the flash messages store
275
- */
276
- flashExcept(keys) {
277
- this.#getFlashStore('write').set('input', lodash.omit(this.#ctx.request.original(), keys));
278
- }
279
- /**
280
- * Flash form input data (only some keys) to the flash messages store
281
- */
282
- flashOnly(keys) {
283
- this.#getFlashStore('write').set('input', lodash.pick(this.#ctx.request.original(), keys));
284
- }
285
- /**
286
- * Reflash messages from the last request in the current response
287
- */
288
- reflash() {
289
- this.#getFlashStore('write').set('reflashed', this.flashMessages.all());
290
- }
291
- /**
292
- * Reflash messages (only some keys) from the last
293
- * request in the current response
294
- */
295
- reflashOnly(keys) {
296
- this.#getFlashStore('write').set('reflashed', lodash.pick(this.flashMessages.all(), keys));
297
- }
298
- /**
299
- * Reflash messages (except some keys) from the last
300
- * request in the current response
301
- */
302
- reflashExcept(keys) {
303
- this.#getFlashStore('write').set('reflashed', lodash.omit(this.flashMessages.all(), keys));
304
- }
305
- /**
306
- * Re-generate the session id and migrate data to it.
307
- */
308
- regenerate() {
309
- this.#sessionId = cuid();
310
- }
311
- /**
312
- * Commit session changes. No more mutations will be
313
- * allowed after commit.
314
- */
315
- async commit() {
316
- if (!this.#store || this.readonly) {
317
- return;
318
- }
319
- /**
320
- * If the flash messages store is not empty, we should put
321
- * its messages inside main session store.
322
- */
323
- if (!this.responseFlashMessages.isEmpty) {
324
- const { input, reflashed, ...others } = this.responseFlashMessages.all();
325
- this.put(this.flashKey, { ...reflashed, ...input, ...others });
326
- }
327
- debug('committing session data');
328
- /**
329
- * Touch the session id cookie to stay alive
330
- */
331
- this.#ctx.response.cookie(this.#config.cookieName, this.#sessionId, this.#config.cookie);
332
- /**
333
- * Delete the session data when the session store
334
- * is empty.
335
- *
336
- * Also we only destroy the session id we read from the cookie.
337
- * If there was no session id in the cookie, there won't be
338
- * any data inside the store either.
339
- */
340
- if (this.isEmpty) {
341
- if (this.#sessionIdFromCookie) {
342
- await this.#driver.destroy(this.#sessionIdFromCookie);
343
- }
344
- this.#emitter.emit('session:committed', { session: this });
345
- return;
346
- }
347
- /**
348
- * Touch the store expiry when the session store was
349
- * not modified.
350
- */
351
- if (!this.hasBeenModified) {
352
- if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) {
353
- await this.#driver.destroy(this.#sessionIdFromCookie);
354
- await this.#driver.write(this.#sessionId, this.#store.toJSON());
355
- this.#emitter.emit('session:migrated', {
356
- fromSessionId: this.#sessionIdFromCookie,
357
- toSessionId: this.sessionId,
358
- session: this,
359
- });
360
- }
361
- else {
362
- await this.#driver.touch(this.#sessionId);
363
- }
364
- this.#emitter.emit('session:committed', { session: this });
365
- return;
366
- }
367
- /**
368
- * Otherwise commit to the session store
369
- */
370
- if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) {
371
- await this.#driver.destroy(this.#sessionIdFromCookie);
372
- await this.#driver.write(this.#sessionId, this.#store.toJSON());
373
- this.#emitter.emit('session:migrated', {
374
- fromSessionId: this.#sessionIdFromCookie,
375
- toSessionId: this.sessionId,
376
- session: this,
377
- });
378
- }
379
- else {
380
- await this.#driver.write(this.#sessionId, this.#store.toJSON());
381
- }
382
- this.#emitter.emit('session:committed', { session: this });
383
- }
384
- }
@@ -1,159 +0,0 @@
1
- /*
2
- * @adonisjs/redis
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import lodash from '@poppinss/utils/lodash';
10
- import { RuntimeException } from '@poppinss/utils';
11
- /**
12
- * Readonly session store
13
- */
14
- export class ReadOnlyStore {
15
- /**
16
- * Underlying store values
17
- */
18
- values;
19
- /**
20
- * Find if store is empty or not
21
- */
22
- get isEmpty() {
23
- return !this.values || Object.keys(this.values).length === 0;
24
- }
25
- constructor(values) {
26
- this.values = values || {};
27
- }
28
- /**
29
- * Get value for a given key
30
- */
31
- get(key, defaultValue) {
32
- const value = lodash.get(this.values, key);
33
- if (defaultValue !== undefined && (value === null || value === undefined)) {
34
- return defaultValue;
35
- }
36
- return value;
37
- }
38
- /**
39
- * A boolean to know if value exists. Extra guards to check
40
- * arrays for it's length as well.
41
- */
42
- has(key, checkForArraysLength = true) {
43
- const value = this.get(key);
44
- if (!Array.isArray(value)) {
45
- return !!value;
46
- }
47
- return checkForArraysLength ? value.length > 0 : !!value;
48
- }
49
- /**
50
- * Get all values
51
- */
52
- all() {
53
- return this.values;
54
- }
55
- /**
56
- * Returns object representation of values
57
- */
58
- toObject() {
59
- return this.all();
60
- }
61
- /**
62
- * Returns the store values
63
- */
64
- toJSON() {
65
- return this.all();
66
- }
67
- /**
68
- * Returns string representation of the store
69
- */
70
- toString() {
71
- return JSON.stringify(this.all());
72
- }
73
- }
74
- /**
75
- * Session store encapsulates the session data and offers a
76
- * declarative API to mutate it.
77
- */
78
- export class Store extends ReadOnlyStore {
79
- /**
80
- * A boolean to know if store has been
81
- * modified
82
- */
83
- #modified = false;
84
- constructor(values) {
85
- super(values);
86
- }
87
- /**
88
- * Find if the store has been modified.
89
- */
90
- get hasBeenModified() {
91
- return this.#modified;
92
- }
93
- /**
94
- * Set key/value pair
95
- */
96
- set(key, value) {
97
- this.#modified = true;
98
- lodash.set(this.values, key, value);
99
- }
100
- /**
101
- * Remove key
102
- */
103
- unset(key) {
104
- this.#modified = true;
105
- lodash.unset(this.values, key);
106
- }
107
- /**
108
- * Pull value from the store. It is same as calling
109
- * store.get and then store.unset
110
- */
111
- pull(key, defaultValue) {
112
- return ((value) => {
113
- this.unset(key);
114
- return value;
115
- })(this.get(key, defaultValue));
116
- }
117
- /**
118
- * Increment number. The method raises an error when
119
- * nderlying value is not a number
120
- */
121
- increment(key, steps = 1) {
122
- const value = this.get(key, 0);
123
- if (typeof value !== 'number') {
124
- throw new RuntimeException(`Cannot increment "${key}". Existing value is not a number`);
125
- }
126
- this.set(key, value + steps);
127
- }
128
- /**
129
- * Increment number. The method raises an error when
130
- * nderlying value is not a number
131
- */
132
- decrement(key, steps = 1) {
133
- const value = this.get(key, 0);
134
- if (typeof value !== 'number') {
135
- throw new RuntimeException(`Cannot decrement "${key}". Existing value is not a number`);
136
- }
137
- this.set(key, value - steps);
138
- }
139
- /**
140
- * Overwrite existing store data with new values.
141
- */
142
- update(values) {
143
- this.#modified = true;
144
- this.values = values;
145
- }
146
- /**
147
- * Update to merge values
148
- */
149
- merge(values) {
150
- this.#modified = true;
151
- lodash.merge(this.values, values);
152
- }
153
- /**
154
- * Reset store by clearing it's values.
155
- */
156
- clear() {
157
- this.update({});
158
- }
159
- }