@monterosa/sdk-storage-kit 0.18.2-rc.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/README.md ADDED
@@ -0,0 +1 @@
1
+ # Monterosa JS SDK / Storage Kit
package/dist/api.d.ts ADDED
@@ -0,0 +1,104 @@
1
+ /**
2
+ * @license
3
+ * api.ts
4
+ * storage-kit
5
+ *
6
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
7
+ *
8
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
9
+ */
10
+ import { Unsubscribe } from '@monterosa/sdk-util';
11
+ import { Experience } from '@monterosa/sdk-launcher-kit';
12
+ import { StorageImpl } from './storage_impl';
13
+ /**
14
+ * @internal
15
+ */
16
+ export declare enum StorageAction {
17
+ Read = "storageRead",
18
+ Write = "storageWrite",
19
+ Remove = "storageRemove",
20
+ Clear = "storageClear"
21
+ }
22
+ /**
23
+ * @internal
24
+ */
25
+ export declare const storage: StorageImpl;
26
+ /**
27
+ * @internal
28
+ */
29
+ export declare function listenStorageMessages(experience: Experience): Unsubscribe;
30
+ /**
31
+ * The `setStoragePersistent` function is a simple function that allows to
32
+ * control the persistence of the SDK storage. If the argument `persistent` is
33
+ * set to `true`, then the storage will be persistent across browser sessions
34
+ * and if it is set to `false`, then the storage will save to memory.
35
+ * It is important to note that the use of persistent storage may be subject
36
+ * to laws and regulations, such as those related to data privacy and protection.
37
+ *
38
+ * @remarks
39
+ * - We transition from persistent to memory and memory to persistent in
40
+ * a seamless manner for you
41
+ *
42
+ * - By default we store in memory
43
+ *
44
+ * - The value of storage persistent persists across session (aka put it to true,
45
+ * it will remain so, put it back to false, it will remain so)
46
+ *
47
+ * - You have the responsibility to comply with any laws and regulations, and
48
+ * store only the data if you know it's valid to do so
49
+ *
50
+ * @param persistent - Determines whether or not SDK storage should persist
51
+ * across browser sessions.
52
+ */
53
+ export declare function setStoragePersistent(persistent: boolean): void;
54
+ /**
55
+ * The function allows to read data from the SDK storage.
56
+ *
57
+ * @param key - The name of the item to be read from storage.
58
+ *
59
+ * @throws
60
+ * The function throws an error if there is a timeout reading the data
61
+ * from the parent application storage.
62
+ *
63
+ * @returns A promise that resolves to the value
64
+ * of the item in storage or `null` if the item doesn't exist.
65
+ */
66
+ export declare function storageRead(key: string): Promise<string | null>;
67
+ /**
68
+ * The function allows to write data to the SDK storage.
69
+ *
70
+ * @param key - A name of the item to be stored.
71
+ * @param value - A value to be stored.
72
+ *
73
+ * @throws
74
+ * The function throws an error if there is a timeout writing the data
75
+ * to the parent's application storage.
76
+ *
77
+ * @returns A promise that resolves to `void` once the data has
78
+ * been successfully stored.
79
+ */
80
+ export declare function storageWrite(key: string, value: string): Promise<void>;
81
+ /**
82
+ * The function allows to remove an item from the SDK storage.
83
+ *
84
+ * @param key - A name of the item to be removed.
85
+ *
86
+ * @throws
87
+ * The function throws an error if there is a timeout removing the data
88
+ * from the parent's application storage.
89
+ *
90
+ * @returns A promise that resolves to `void` once the data has
91
+ * been successfully removed.
92
+ */
93
+ export declare function storageRemove(key: string): Promise<void>;
94
+ /**
95
+ * The function allows to clear all data from the SDK storage.
96
+ *
97
+ * @throws
98
+ * The function throws an error if there is a timeout clearing the parent's
99
+ * application storage.
100
+ *
101
+ * @returns A promise that resolves to `void` once the data has been
102
+ * successfully cleared.
103
+ */
104
+ export declare function storageClear(): Promise<void>;
@@ -0,0 +1,351 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var sdkLauncherKit = require('@monterosa/sdk-launcher-kit');
6
+ var sdkUtil = require('@monterosa/sdk-util');
7
+
8
+ /*! *****************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+
23
+ function __awaiter(thisArg, _arguments, P, generator) {
24
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
25
+ return new (P || (P = Promise))(function (resolve, reject) {
26
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
27
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ }
32
+
33
+ function __generator(thisArg, body) {
34
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
35
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
36
+ function verb(n) { return function (v) { return step([n, v]); }; }
37
+ function step(op) {
38
+ if (f) throw new TypeError("Generator is already executing.");
39
+ while (_) try {
40
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
41
+ if (y = 0, t) op = [op[0] & 2, t.value];
42
+ switch (op[0]) {
43
+ case 0: case 1: t = op; break;
44
+ case 4: _.label++; return { value: op[1], done: false };
45
+ case 5: _.label++; y = op[1]; op = [0]; continue;
46
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
47
+ default:
48
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
49
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
50
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
51
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
52
+ if (t[2]) _.ops.pop();
53
+ _.trys.pop(); continue;
54
+ }
55
+ op = body.call(thisArg, _);
56
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
57
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
58
+ }
59
+ }
60
+
61
+ /**
62
+ * @license
63
+ * storage_impl.ts
64
+ * storage-kit
65
+ *
66
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
67
+ *
68
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
69
+ */
70
+ var StorageImpl = /** @class */ (function () {
71
+ function StorageImpl() {
72
+ this.memoryStore = {};
73
+ this.accessible = sdkUtil.checkAvailability();
74
+ this._persistent = true;
75
+ }
76
+ Object.defineProperty(StorageImpl.prototype, "persistent", {
77
+ get: function () {
78
+ return this._persistent;
79
+ },
80
+ set: function (newValue) {
81
+ var oldValue = this._persistent;
82
+ if (oldValue === newValue) {
83
+ return;
84
+ }
85
+ var swapToStorage = newValue === true && this.accessible;
86
+ var swapToMemory = newValue === false && this.accessible;
87
+ if (swapToStorage) {
88
+ for (var _i = 0, _a = Object.entries(this.memoryStore); _i < _a.length; _i++) {
89
+ var _b = _a[_i], key = _b[0], value = _b[1];
90
+ sdkUtil.setItem(key, value);
91
+ }
92
+ }
93
+ if (swapToMemory) {
94
+ sdkUtil.clear();
95
+ }
96
+ this._persistent = newValue;
97
+ },
98
+ enumerable: false,
99
+ configurable: true
100
+ });
101
+ StorageImpl.prototype.getItem = function (key) {
102
+ if (this.persistent && this.accessible) {
103
+ return sdkUtil.getItem(key);
104
+ }
105
+ if (Object.prototype.hasOwnProperty.call(this.memoryStore, key)) {
106
+ return this.memoryStore[key];
107
+ }
108
+ return null;
109
+ };
110
+ StorageImpl.prototype.setItem = function (key, value) {
111
+ if (this.persistent && this.accessible) {
112
+ sdkUtil.setItem(key, value);
113
+ }
114
+ this.memoryStore[key] = value;
115
+ };
116
+ StorageImpl.prototype.removeItem = function (key) {
117
+ if (this.persistent && this.accessible) {
118
+ sdkUtil.removeItem(key);
119
+ }
120
+ delete this.memoryStore[key];
121
+ };
122
+ StorageImpl.prototype.clear = function () {
123
+ if (this.persistent && this.accessible) {
124
+ sdkUtil.clear();
125
+ }
126
+ this.memoryStore = {};
127
+ };
128
+ return StorageImpl;
129
+ }());
130
+
131
+ /**
132
+ * @license
133
+ * api.ts
134
+ * storage-kit
135
+ *
136
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
137
+ *
138
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
139
+ */
140
+ /**
141
+ * @internal
142
+ */
143
+ exports.StorageAction = void 0;
144
+ (function (StorageAction) {
145
+ StorageAction["Read"] = "storageRead";
146
+ StorageAction["Write"] = "storageWrite";
147
+ StorageAction["Remove"] = "storageRemove";
148
+ StorageAction["Clear"] = "storageClear";
149
+ })(exports.StorageAction || (exports.StorageAction = {}));
150
+ /**
151
+ * @internal
152
+ */
153
+ var storage = new StorageImpl();
154
+ /**
155
+ * @internal
156
+ */
157
+ function listenStorageMessages(experience) {
158
+ return sdkLauncherKit.onSdkMessage(experience, function (message) {
159
+ var payload = {};
160
+ if (!Object.values(exports.StorageAction).includes(message.action)) {
161
+ return;
162
+ }
163
+ switch (message.action) {
164
+ case exports.StorageAction.Read:
165
+ payload = {
166
+ value: storage.getItem(message.payload.key),
167
+ };
168
+ break;
169
+ case exports.StorageAction.Write:
170
+ storage.setItem(message.payload.key, message.payload.value);
171
+ break;
172
+ case exports.StorageAction.Remove:
173
+ storage.removeItem(message.payload.key);
174
+ break;
175
+ case exports.StorageAction.Clear:
176
+ storage.clear();
177
+ break;
178
+ }
179
+ sdkLauncherKit.respondToSdkMessage(experience, message, payload);
180
+ });
181
+ }
182
+ /**
183
+ * The `setStoragePersistent` function is a simple function that allows to
184
+ * control the persistence of the SDK storage. If the argument `persistent` is
185
+ * set to `true`, then the storage will be persistent across browser sessions
186
+ * and if it is set to `false`, then the storage will save to memory.
187
+ * It is important to note that the use of persistent storage may be subject
188
+ * to laws and regulations, such as those related to data privacy and protection.
189
+ *
190
+ * @remarks
191
+ * - We transition from persistent to memory and memory to persistent in
192
+ * a seamless manner for you
193
+ *
194
+ * - By default we store in memory
195
+ *
196
+ * - The value of storage persistent persists across session (aka put it to true,
197
+ * it will remain so, put it back to false, it will remain so)
198
+ *
199
+ * - You have the responsibility to comply with any laws and regulations, and
200
+ * store only the data if you know it's valid to do so
201
+ *
202
+ * @param persistent - Determines whether or not SDK storage should persist
203
+ * across browser sessions.
204
+ */
205
+ function setStoragePersistent(persistent) {
206
+ storage.persistent = persistent;
207
+ }
208
+ /**
209
+ * The function allows to read data from the SDK storage.
210
+ *
211
+ * @param key - The name of the item to be read from storage.
212
+ *
213
+ * @throws
214
+ * The function throws an error if there is a timeout reading the data
215
+ * from the parent application storage.
216
+ *
217
+ * @returns A promise that resolves to the value
218
+ * of the item in storage or `null` if the item doesn't exist.
219
+ */
220
+ function storageRead(key) {
221
+ return __awaiter(this, void 0, void 0, function () {
222
+ var parentApp, value, result;
223
+ return __generator(this, function (_a) {
224
+ switch (_a.label) {
225
+ case 0:
226
+ parentApp = sdkLauncherKit.getParentApplication();
227
+ value = null;
228
+ if (!(parentApp !== null)) return [3 /*break*/, 2];
229
+ return [4 /*yield*/, sdkLauncherKit.sendSdkRequest(parentApp, exports.StorageAction.Read, {
230
+ key: key,
231
+ })];
232
+ case 1:
233
+ result = _a.sent();
234
+ value = result.payload.value;
235
+ return [3 /*break*/, 3];
236
+ case 2:
237
+ value = storage.getItem(key);
238
+ _a.label = 3;
239
+ case 3: return [2 /*return*/, Promise.resolve(value)];
240
+ }
241
+ });
242
+ });
243
+ }
244
+ /**
245
+ * The function allows to write data to the SDK storage.
246
+ *
247
+ * @param key - A name of the item to be stored.
248
+ * @param value - A value to be stored.
249
+ *
250
+ * @throws
251
+ * The function throws an error if there is a timeout writing the data
252
+ * to the parent's application storage.
253
+ *
254
+ * @returns A promise that resolves to `void` once the data has
255
+ * been successfully stored.
256
+ */
257
+ function storageWrite(key, value) {
258
+ return __awaiter(this, void 0, void 0, function () {
259
+ var parentApp;
260
+ return __generator(this, function (_a) {
261
+ switch (_a.label) {
262
+ case 0:
263
+ parentApp = sdkLauncherKit.getParentApplication();
264
+ if (!(parentApp !== null)) return [3 /*break*/, 2];
265
+ return [4 /*yield*/, sdkLauncherKit.sendSdkRequest(parentApp, exports.StorageAction.Write, {
266
+ key: key,
267
+ value: value,
268
+ })];
269
+ case 1:
270
+ _a.sent();
271
+ return [3 /*break*/, 3];
272
+ case 2:
273
+ storage.setItem(key, value);
274
+ _a.label = 3;
275
+ case 3: return [2 /*return*/, Promise.resolve()];
276
+ }
277
+ });
278
+ });
279
+ }
280
+ /**
281
+ * The function allows to remove an item from the SDK storage.
282
+ *
283
+ * @param key - A name of the item to be removed.
284
+ *
285
+ * @throws
286
+ * The function throws an error if there is a timeout removing the data
287
+ * from the parent's application storage.
288
+ *
289
+ * @returns A promise that resolves to `void` once the data has
290
+ * been successfully removed.
291
+ */
292
+ function storageRemove(key) {
293
+ return __awaiter(this, void 0, void 0, function () {
294
+ var parentApp;
295
+ return __generator(this, function (_a) {
296
+ switch (_a.label) {
297
+ case 0:
298
+ parentApp = sdkLauncherKit.getParentApplication();
299
+ if (!(parentApp !== null)) return [3 /*break*/, 2];
300
+ return [4 /*yield*/, sdkLauncherKit.sendSdkRequest(parentApp, exports.StorageAction.Remove, { key: key })];
301
+ case 1:
302
+ _a.sent();
303
+ return [3 /*break*/, 3];
304
+ case 2:
305
+ storage.removeItem(key);
306
+ _a.label = 3;
307
+ case 3: return [2 /*return*/, Promise.resolve()];
308
+ }
309
+ });
310
+ });
311
+ }
312
+ /**
313
+ * The function allows to clear all data from the SDK storage.
314
+ *
315
+ * @throws
316
+ * The function throws an error if there is a timeout clearing the parent's
317
+ * application storage.
318
+ *
319
+ * @returns A promise that resolves to `void` once the data has been
320
+ * successfully cleared.
321
+ */
322
+ function storageClear() {
323
+ return __awaiter(this, void 0, void 0, function () {
324
+ var parentApp;
325
+ return __generator(this, function (_a) {
326
+ switch (_a.label) {
327
+ case 0:
328
+ parentApp = sdkLauncherKit.getParentApplication();
329
+ if (!(parentApp !== null)) return [3 /*break*/, 2];
330
+ return [4 /*yield*/, sdkLauncherKit.sendSdkRequest(parentApp, exports.StorageAction.Clear)];
331
+ case 1:
332
+ _a.sent();
333
+ return [3 /*break*/, 3];
334
+ case 2:
335
+ storage.clear();
336
+ _a.label = 3;
337
+ case 3: return [2 /*return*/, Promise.resolve()];
338
+ }
339
+ });
340
+ });
341
+ }
342
+ sdkLauncherKit.registerEmbedHook(listenStorageMessages);
343
+
344
+ exports.listenStorageMessages = listenStorageMessages;
345
+ exports.setStoragePersistent = setStoragePersistent;
346
+ exports.storage = storage;
347
+ exports.storageClear = storageClear;
348
+ exports.storageRead = storageRead;
349
+ exports.storageRemove = storageRemove;
350
+ exports.storageWrite = storageWrite;
351
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * storage_impl.ts\n * storage-kit\n *\n * Copyright © 2025 Monterosa Productions Limited. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport {\n checkAvailability,\n getItem,\n setItem,\n removeItem,\n clear,\n} from '@monterosa/sdk-util';\n\nexport class StorageImpl {\n private memoryStore: { [key: string]: string } = {};\n private accessible: boolean = checkAvailability();\n private _persistent: boolean = true;\n\n set persistent(newValue: boolean) {\n const oldValue = this._persistent;\n\n if (oldValue === newValue) {\n return;\n }\n\n const swapToStorage = newValue === true && this.accessible;\n const swapToMemory = newValue === false && this.accessible;\n\n if (swapToStorage) {\n for (const [key, value] of Object.entries(this.memoryStore)) {\n setItem(key, value);\n }\n }\n\n if (swapToMemory) {\n clear();\n }\n\n this._persistent = newValue;\n }\n\n get persistent(): boolean {\n return this._persistent;\n }\n\n getItem(key: string): string | null {\n if (this.persistent && this.accessible) {\n return getItem(key);\n }\n\n if (Object.prototype.hasOwnProperty.call(this.memoryStore, key)) {\n return this.memoryStore[key];\n }\n\n return null;\n }\n\n setItem(key: string, value: string): void {\n if (this.persistent && this.accessible) {\n setItem(key, value);\n }\n\n this.memoryStore[key] = value;\n }\n\n removeItem(key: string): void {\n if (this.persistent && this.accessible) {\n removeItem(key);\n }\n\n delete this.memoryStore[key];\n }\n\n clear(): void {\n if (this.persistent && this.accessible) {\n clear();\n }\n\n this.memoryStore = {};\n }\n}\n","/**\n * @license\n * api.ts\n * storage-kit\n *\n * Copyright © 2025 Monterosa Productions Limited. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n} from '@monterosa/sdk-launcher-kit';\n\nimport { StorageImpl } from './storage_impl';\n\n/**\n * @internal\n */\nexport enum StorageAction {\n Read = 'storageRead',\n Write = 'storageWrite',\n Remove = 'storageRemove',\n Clear = 'storageClear',\n}\n\n/**\n * @internal\n */\nexport const storage = new StorageImpl();\n\n/**\n * @internal\n */\nexport function listenStorageMessages(experience: Experience): Unsubscribe {\n return onSdkMessage(experience, (message) => {\n let payload: Payload = {};\n\n if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n storage.removeItem(message.payload.key);\n break;\n case StorageAction.Clear:\n storage.clear();\n break;\n }\n\n respondToSdkMessage(experience, message, payload);\n });\n}\n\n/**\n * The `setStoragePersistent` function is a simple function that allows to\n * control the persistence of the SDK storage. If the argument `persistent` is\n * set to `true`, then the storage will be persistent across browser sessions\n * and if it is set to `false`, then the storage will save to memory.\n * It is important to note that the use of persistent storage may be subject\n * to laws and regulations, such as those related to data privacy and protection.\n *\n * @remarks\n * - We transition from persistent to memory and memory to persistent in\n * a seamless manner for you\n *\n * - By default we store in memory\n *\n * - The value of storage persistent persists across session (aka put it to true,\n * it will remain so, put it back to false, it will remain so)\n *\n * - You have the responsibility to comply with any laws and regulations, and\n * store only the data if you know it's valid to do so\n *\n * @param persistent - Determines whether or not SDK storage should persist\n * across browser sessions.\n */\nexport function setStoragePersistent(persistent: boolean): void {\n storage.persistent = persistent;\n}\n\n/**\n * The function allows to read data from the SDK storage.\n *\n * @param key - The name of the item to be read from storage.\n *\n * @throws\n * The function throws an error if there is a timeout reading the data\n * from the parent application storage.\n *\n * @returns A promise that resolves to the value\n * of the item in storage or `null` if the item doesn't exist.\n */\nexport async function storageRead(key: string): Promise<string | null> {\n const parentApp = getParentApplication();\n\n let value: string | null = null;\n\n if (parentApp !== null) {\n const result = await sendSdkRequest(parentApp, StorageAction.Read, {\n key,\n });\n\n value = result.payload.value as string;\n } else {\n value = storage.getItem(key);\n }\n\n return Promise.resolve(value);\n}\n\n/**\n * The function allows to write data to the SDK storage.\n *\n * @param key - A name of the item to be stored.\n * @param value - A value to be stored.\n *\n * @throws\n * The function throws an error if there is a timeout writing the data\n * to the parent's application storage.\n *\n * @returns A promise that resolves to `void` once the data has\n * been successfully stored.\n */\nexport async function storageWrite(key: string, value: string): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n\n return Promise.resolve();\n}\n\n/**\n * The function allows to remove an item from the SDK storage.\n *\n * @param key - A name of the item to be removed.\n *\n * @throws\n * The function throws an error if there is a timeout removing the data\n * from the parent's application storage.\n *\n * @returns A promise that resolves to `void` once the data has\n * been successfully removed.\n */\nexport async function storageRemove(key: string): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n\n return Promise.resolve();\n}\n\n/**\n * The function allows to clear all data from the SDK storage.\n *\n * @throws\n * The function throws an error if there is a timeout clearing the parent's\n * application storage.\n *\n * @returns A promise that resolves to `void` once the data has been\n * successfully cleared.\n */\nexport async function storageClear(): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n\n return Promise.resolve();\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":["checkAvailability","setItem","clear","getItem","removeItem","StorageAction","onSdkMessage","respondToSdkMessage","getParentApplication","sendSdkRequest","registerEmbedHook"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;AAkBA;IAAA;QACU,gBAAW,GAA8B,EAAE,CAAC;QAC5C,eAAU,GAAYA,yBAAiB,EAAE,CAAC;QAC1C,gBAAW,GAAY,IAAI,CAAC;KAgErC;IA9DC,sBAAI,mCAAU;aAuBd;YACE,OAAO,IAAI,CAAC,WAAW,CAAC;SACzB;aAzBD,UAAe,QAAiB;YAC9B,IAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAC;YAElC,IAAI,QAAQ,KAAK,QAAQ,EAAE;gBACzB,OAAO;aACR;YAED,IAAM,aAAa,GAAG,QAAQ,KAAK,IAAI,IAAI,IAAI,CAAC,UAAU,CAAC;YAC3D,IAAM,YAAY,GAA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AE,EAAE,GAAG,KAAA,EAAE,CAAC,EAAA;;oBAA9D,SAA8D,CAAC;;;oBAE/D,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;;wBAG1B,sBAAO,OAAO,CAAC,OAAO,EAAE,EAAC;;;;CAC1B;AAED;;;;;;;;;;SAUsB,YAAY;;;;;;oBAC1B,SAAS,GAAGG,mCAAoB,EAAE,CAAC;0BAErC,SAAS,KAAK,IAAI,CAAA,EAAlB,wBAAkB;oBACpB,qBAAMC,6BAAc,CAAC,SAAS,EAAEJ,qBAAa,CAAC,KAAK,CAAC,EAAA;;oBAApD,SAAoD,CAAC;;;oBAErD,OAAO,CAAC,KAAK,EAAE,CAAC;;wBAGlB,sBAAO,OAAO,CAAC,OAAO,EAAE,EAAC;;;;CAC1B;AAEDK,gCAAiB,CAAC,qBAAqB,CAAC;;;;;;;;;;"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @license
3
+ * index.ts
4
+ * storage-kit
5
+ *
6
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
7
+ *
8
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
9
+ */
10
+ /**
11
+ * Monterosa SDK / Storage Kit
12
+ *
13
+ * @packageDocumentation
14
+ */
15
+ export * from './api';
@@ -0,0 +1,242 @@
1
+ import { registerEmbedHook, onSdkMessage, respondToSdkMessage, getParentApplication, sendSdkRequest } from '@monterosa/sdk-launcher-kit';
2
+ import { checkAvailability, setItem, clear, getItem, removeItem } from '@monterosa/sdk-util';
3
+
4
+ /**
5
+ * @license
6
+ * storage_impl.ts
7
+ * storage-kit
8
+ *
9
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
10
+ *
11
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
12
+ */
13
+ class StorageImpl {
14
+ constructor() {
15
+ this.memoryStore = {};
16
+ this.accessible = checkAvailability();
17
+ this._persistent = true;
18
+ }
19
+ set persistent(newValue) {
20
+ const oldValue = this._persistent;
21
+ if (oldValue === newValue) {
22
+ return;
23
+ }
24
+ const swapToStorage = newValue === true && this.accessible;
25
+ const swapToMemory = newValue === false && this.accessible;
26
+ if (swapToStorage) {
27
+ for (const [key, value] of Object.entries(this.memoryStore)) {
28
+ setItem(key, value);
29
+ }
30
+ }
31
+ if (swapToMemory) {
32
+ clear();
33
+ }
34
+ this._persistent = newValue;
35
+ }
36
+ get persistent() {
37
+ return this._persistent;
38
+ }
39
+ getItem(key) {
40
+ if (this.persistent && this.accessible) {
41
+ return getItem(key);
42
+ }
43
+ if (Object.prototype.hasOwnProperty.call(this.memoryStore, key)) {
44
+ return this.memoryStore[key];
45
+ }
46
+ return null;
47
+ }
48
+ setItem(key, value) {
49
+ if (this.persistent && this.accessible) {
50
+ setItem(key, value);
51
+ }
52
+ this.memoryStore[key] = value;
53
+ }
54
+ removeItem(key) {
55
+ if (this.persistent && this.accessible) {
56
+ removeItem(key);
57
+ }
58
+ delete this.memoryStore[key];
59
+ }
60
+ clear() {
61
+ if (this.persistent && this.accessible) {
62
+ clear();
63
+ }
64
+ this.memoryStore = {};
65
+ }
66
+ }
67
+
68
+ /**
69
+ * @license
70
+ * api.ts
71
+ * storage-kit
72
+ *
73
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
74
+ *
75
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
76
+ */
77
+ /**
78
+ * @internal
79
+ */
80
+ var StorageAction;
81
+ (function (StorageAction) {
82
+ StorageAction["Read"] = "storageRead";
83
+ StorageAction["Write"] = "storageWrite";
84
+ StorageAction["Remove"] = "storageRemove";
85
+ StorageAction["Clear"] = "storageClear";
86
+ })(StorageAction || (StorageAction = {}));
87
+ /**
88
+ * @internal
89
+ */
90
+ const storage = new StorageImpl();
91
+ /**
92
+ * @internal
93
+ */
94
+ function listenStorageMessages(experience) {
95
+ return onSdkMessage(experience, (message) => {
96
+ let payload = {};
97
+ if (!Object.values(StorageAction).includes(message.action)) {
98
+ return;
99
+ }
100
+ switch (message.action) {
101
+ case StorageAction.Read:
102
+ payload = {
103
+ value: storage.getItem(message.payload.key),
104
+ };
105
+ break;
106
+ case StorageAction.Write:
107
+ storage.setItem(message.payload.key, message.payload.value);
108
+ break;
109
+ case StorageAction.Remove:
110
+ storage.removeItem(message.payload.key);
111
+ break;
112
+ case StorageAction.Clear:
113
+ storage.clear();
114
+ break;
115
+ }
116
+ respondToSdkMessage(experience, message, payload);
117
+ });
118
+ }
119
+ /**
120
+ * The `setStoragePersistent` function is a simple function that allows to
121
+ * control the persistence of the SDK storage. If the argument `persistent` is
122
+ * set to `true`, then the storage will be persistent across browser sessions
123
+ * and if it is set to `false`, then the storage will save to memory.
124
+ * It is important to note that the use of persistent storage may be subject
125
+ * to laws and regulations, such as those related to data privacy and protection.
126
+ *
127
+ * @remarks
128
+ * - We transition from persistent to memory and memory to persistent in
129
+ * a seamless manner for you
130
+ *
131
+ * - By default we store in memory
132
+ *
133
+ * - The value of storage persistent persists across session (aka put it to true,
134
+ * it will remain so, put it back to false, it will remain so)
135
+ *
136
+ * - You have the responsibility to comply with any laws and regulations, and
137
+ * store only the data if you know it's valid to do so
138
+ *
139
+ * @param persistent - Determines whether or not SDK storage should persist
140
+ * across browser sessions.
141
+ */
142
+ function setStoragePersistent(persistent) {
143
+ storage.persistent = persistent;
144
+ }
145
+ /**
146
+ * The function allows to read data from the SDK storage.
147
+ *
148
+ * @param key - The name of the item to be read from storage.
149
+ *
150
+ * @throws
151
+ * The function throws an error if there is a timeout reading the data
152
+ * from the parent application storage.
153
+ *
154
+ * @returns A promise that resolves to the value
155
+ * of the item in storage or `null` if the item doesn't exist.
156
+ */
157
+ async function storageRead(key) {
158
+ const parentApp = getParentApplication();
159
+ let value = null;
160
+ if (parentApp !== null) {
161
+ const result = await sendSdkRequest(parentApp, StorageAction.Read, {
162
+ key,
163
+ });
164
+ value = result.payload.value;
165
+ }
166
+ else {
167
+ value = storage.getItem(key);
168
+ }
169
+ return Promise.resolve(value);
170
+ }
171
+ /**
172
+ * The function allows to write data to the SDK storage.
173
+ *
174
+ * @param key - A name of the item to be stored.
175
+ * @param value - A value to be stored.
176
+ *
177
+ * @throws
178
+ * The function throws an error if there is a timeout writing the data
179
+ * to the parent's application storage.
180
+ *
181
+ * @returns A promise that resolves to `void` once the data has
182
+ * been successfully stored.
183
+ */
184
+ async function storageWrite(key, value) {
185
+ const parentApp = getParentApplication();
186
+ if (parentApp !== null) {
187
+ await sendSdkRequest(parentApp, StorageAction.Write, {
188
+ key,
189
+ value,
190
+ });
191
+ }
192
+ else {
193
+ storage.setItem(key, value);
194
+ }
195
+ return Promise.resolve();
196
+ }
197
+ /**
198
+ * The function allows to remove an item from the SDK storage.
199
+ *
200
+ * @param key - A name of the item to be removed.
201
+ *
202
+ * @throws
203
+ * The function throws an error if there is a timeout removing the data
204
+ * from the parent's application storage.
205
+ *
206
+ * @returns A promise that resolves to `void` once the data has
207
+ * been successfully removed.
208
+ */
209
+ async function storageRemove(key) {
210
+ const parentApp = getParentApplication();
211
+ if (parentApp !== null) {
212
+ await sendSdkRequest(parentApp, StorageAction.Remove, { key });
213
+ }
214
+ else {
215
+ storage.removeItem(key);
216
+ }
217
+ return Promise.resolve();
218
+ }
219
+ /**
220
+ * The function allows to clear all data from the SDK storage.
221
+ *
222
+ * @throws
223
+ * The function throws an error if there is a timeout clearing the parent's
224
+ * application storage.
225
+ *
226
+ * @returns A promise that resolves to `void` once the data has been
227
+ * successfully cleared.
228
+ */
229
+ async function storageClear() {
230
+ const parentApp = getParentApplication();
231
+ if (parentApp !== null) {
232
+ await sendSdkRequest(parentApp, StorageAction.Clear);
233
+ }
234
+ else {
235
+ storage.clear();
236
+ }
237
+ return Promise.resolve();
238
+ }
239
+ registerEmbedHook(listenStorageMessages);
240
+
241
+ export { StorageAction, listenStorageMessages, setStoragePersistent, storage, storageClear, storageRead, storageRemove, storageWrite };
242
+ //# sourceMappingURL=index.esm2017.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm2017.js","sources":["../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * storage_impl.ts\n * storage-kit\n *\n * Copyright © 2025 Monterosa Productions Limited. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport {\n checkAvailability,\n getItem,\n setItem,\n removeItem,\n clear,\n} from '@monterosa/sdk-util';\n\nexport class StorageImpl {\n private memoryStore: { [key: string]: string } = {};\n private accessible: boolean = checkAvailability();\n private _persistent: boolean = true;\n\n set persistent(newValue: boolean) {\n const oldValue = this._persistent;\n\n if (oldValue === newValue) {\n return;\n }\n\n const swapToStorage = newValue === true && this.accessible;\n const swapToMemory = newValue === false && this.accessible;\n\n if (swapToStorage) {\n for (const [key, value] of Object.entries(this.memoryStore)) {\n setItem(key, value);\n }\n }\n\n if (swapToMemory) {\n clear();\n }\n\n this._persistent = newValue;\n }\n\n get persistent(): boolean {\n return this._persistent;\n }\n\n getItem(key: string): string | null {\n if (this.persistent && this.accessible) {\n return getItem(key);\n }\n\n if (Object.prototype.hasOwnProperty.call(this.memoryStore, key)) {\n return this.memoryStore[key];\n }\n\n return null;\n }\n\n setItem(key: string, value: string): void {\n if (this.persistent && this.accessible) {\n setItem(key, value);\n }\n\n this.memoryStore[key] = value;\n }\n\n removeItem(key: string): void {\n if (this.persistent && this.accessible) {\n removeItem(key);\n }\n\n delete this.memoryStore[key];\n }\n\n clear(): void {\n if (this.persistent && this.accessible) {\n clear();\n }\n\n this.memoryStore = {};\n }\n}\n","/**\n * @license\n * api.ts\n * storage-kit\n *\n * Copyright © 2025 Monterosa Productions Limited. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n} from '@monterosa/sdk-launcher-kit';\n\nimport { StorageImpl } from './storage_impl';\n\n/**\n * @internal\n */\nexport enum StorageAction {\n Read = 'storageRead',\n Write = 'storageWrite',\n Remove = 'storageRemove',\n Clear = 'storageClear',\n}\n\n/**\n * @internal\n */\nexport const storage = new StorageImpl();\n\n/**\n * @internal\n */\nexport function listenStorageMessages(experience: Experience): Unsubscribe {\n return onSdkMessage(experience, (message) => {\n let payload: Payload = {};\n\n if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n storage.removeItem(message.payload.key);\n break;\n case StorageAction.Clear:\n storage.clear();\n break;\n }\n\n respondToSdkMessage(experience, message, payload);\n });\n}\n\n/**\n * The `setStoragePersistent` function is a simple function that allows to\n * control the persistence of the SDK storage. If the argument `persistent` is\n * set to `true`, then the storage will be persistent across browser sessions\n * and if it is set to `false`, then the storage will save to memory.\n * It is important to note that the use of persistent storage may be subject\n * to laws and regulations, such as those related to data privacy and protection.\n *\n * @remarks\n * - We transition from persistent to memory and memory to persistent in\n * a seamless manner for you\n *\n * - By default we store in memory\n *\n * - The value of storage persistent persists across session (aka put it to true,\n * it will remain so, put it back to false, it will remain so)\n *\n * - You have the responsibility to comply with any laws and regulations, and\n * store only the data if you know it's valid to do so\n *\n * @param persistent - Determines whether or not SDK storage should persist\n * across browser sessions.\n */\nexport function setStoragePersistent(persistent: boolean): void {\n storage.persistent = persistent;\n}\n\n/**\n * The function allows to read data from the SDK storage.\n *\n * @param key - The name of the item to be read from storage.\n *\n * @throws\n * The function throws an error if there is a timeout reading the data\n * from the parent application storage.\n *\n * @returns A promise that resolves to the value\n * of the item in storage or `null` if the item doesn't exist.\n */\nexport async function storageRead(key: string): Promise<string | null> {\n const parentApp = getParentApplication();\n\n let value: string | null = null;\n\n if (parentApp !== null) {\n const result = await sendSdkRequest(parentApp, StorageAction.Read, {\n key,\n });\n\n value = result.payload.value as string;\n } else {\n value = storage.getItem(key);\n }\n\n return Promise.resolve(value);\n}\n\n/**\n * The function allows to write data to the SDK storage.\n *\n * @param key - A name of the item to be stored.\n * @param value - A value to be stored.\n *\n * @throws\n * The function throws an error if there is a timeout writing the data\n * to the parent's application storage.\n *\n * @returns A promise that resolves to `void` once the data has\n * been successfully stored.\n */\nexport async function storageWrite(key: string, value: string): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n\n return Promise.resolve();\n}\n\n/**\n * The function allows to remove an item from the SDK storage.\n *\n * @param key - A name of the item to be removed.\n *\n * @throws\n * The function throws an error if there is a timeout removing the data\n * from the parent's application storage.\n *\n * @returns A promise that resolves to `void` once the data has\n * been successfully removed.\n */\nexport async function storageRemove(key: string): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n\n return Promise.resolve();\n}\n\n/**\n * The function allows to clear all data from the SDK storage.\n *\n * @throws\n * The function throws an error if there is a timeout clearing the parent's\n * application storage.\n *\n * @returns A promise that resolves to `void` once the data has been\n * successfully cleared.\n */\nexport async function storageClear(): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n\n return Promise.resolve();\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":[],"mappings":";;;AAAA;;;;;;;;;MAkBa,WAAW;IAAxB;QACU,gBAAW,GAA8B,EAAE,CAAC;QAC5C,eAAU,GAAY,iBAAiB,EAAE,CAAC;QAC1C,gBAAW,GAAY,IAAI,CAAC;KAgErC;IA9DC,IAAI,UAAU,CAAC,QAAiB;QAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAC;QAElC,IAAI,QAAQ,KAAK,QAAQ,EAAE;YACzB,OAAO;SACR;QA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CAAC,CAAC;KACzB;IAED,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;AAC3B,CAAC;AAED;;;;;;;;;;AAUO,eAAe,YAAY;IAChC,MAAM,SAAS,GAAG,oBAAoB,EAAE,CAAC;IAEzC,IAAI,SAAS,KAAK,IAAI,EAAE;QACtB,MAAM,cAAc,CAAC,SAAS,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC;KACtD;SAAM;QACL,OAAO,CAAC,KAAK,EAAE,CAAC;KACjB;IAED,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;AAC3B,CAAC;AAED,iBAAiB,CAAC,qBAAqB,CAAC;;;;"}
@@ -0,0 +1,341 @@
1
+ import { registerEmbedHook, onSdkMessage, respondToSdkMessage, getParentApplication, sendSdkRequest } from '@monterosa/sdk-launcher-kit';
2
+ import { setItem, clear, getItem, removeItem, checkAvailability } from '@monterosa/sdk-util';
3
+
4
+ /*! *****************************************************************************
5
+ Copyright (c) Microsoft Corporation.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
+ PERFORMANCE OF THIS SOFTWARE.
17
+ ***************************************************************************** */
18
+
19
+ function __awaiter(thisArg, _arguments, P, generator) {
20
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
+ return new (P || (P = Promise))(function (resolve, reject) {
22
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
23
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
24
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
25
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
26
+ });
27
+ }
28
+
29
+ function __generator(thisArg, body) {
30
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
31
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
32
+ function verb(n) { return function (v) { return step([n, v]); }; }
33
+ function step(op) {
34
+ if (f) throw new TypeError("Generator is already executing.");
35
+ while (_) try {
36
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
37
+ if (y = 0, t) op = [op[0] & 2, t.value];
38
+ switch (op[0]) {
39
+ case 0: case 1: t = op; break;
40
+ case 4: _.label++; return { value: op[1], done: false };
41
+ case 5: _.label++; y = op[1]; op = [0]; continue;
42
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
43
+ default:
44
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
45
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
46
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
47
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
48
+ if (t[2]) _.ops.pop();
49
+ _.trys.pop(); continue;
50
+ }
51
+ op = body.call(thisArg, _);
52
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
53
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
54
+ }
55
+ }
56
+
57
+ /**
58
+ * @license
59
+ * storage_impl.ts
60
+ * storage-kit
61
+ *
62
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
63
+ *
64
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
65
+ */
66
+ var StorageImpl = /** @class */ (function () {
67
+ function StorageImpl() {
68
+ this.memoryStore = {};
69
+ this.accessible = checkAvailability();
70
+ this._persistent = true;
71
+ }
72
+ Object.defineProperty(StorageImpl.prototype, "persistent", {
73
+ get: function () {
74
+ return this._persistent;
75
+ },
76
+ set: function (newValue) {
77
+ var oldValue = this._persistent;
78
+ if (oldValue === newValue) {
79
+ return;
80
+ }
81
+ var swapToStorage = newValue === true && this.accessible;
82
+ var swapToMemory = newValue === false && this.accessible;
83
+ if (swapToStorage) {
84
+ for (var _i = 0, _a = Object.entries(this.memoryStore); _i < _a.length; _i++) {
85
+ var _b = _a[_i], key = _b[0], value = _b[1];
86
+ setItem(key, value);
87
+ }
88
+ }
89
+ if (swapToMemory) {
90
+ clear();
91
+ }
92
+ this._persistent = newValue;
93
+ },
94
+ enumerable: false,
95
+ configurable: true
96
+ });
97
+ StorageImpl.prototype.getItem = function (key) {
98
+ if (this.persistent && this.accessible) {
99
+ return getItem(key);
100
+ }
101
+ if (Object.prototype.hasOwnProperty.call(this.memoryStore, key)) {
102
+ return this.memoryStore[key];
103
+ }
104
+ return null;
105
+ };
106
+ StorageImpl.prototype.setItem = function (key, value) {
107
+ if (this.persistent && this.accessible) {
108
+ setItem(key, value);
109
+ }
110
+ this.memoryStore[key] = value;
111
+ };
112
+ StorageImpl.prototype.removeItem = function (key) {
113
+ if (this.persistent && this.accessible) {
114
+ removeItem(key);
115
+ }
116
+ delete this.memoryStore[key];
117
+ };
118
+ StorageImpl.prototype.clear = function () {
119
+ if (this.persistent && this.accessible) {
120
+ clear();
121
+ }
122
+ this.memoryStore = {};
123
+ };
124
+ return StorageImpl;
125
+ }());
126
+
127
+ /**
128
+ * @license
129
+ * api.ts
130
+ * storage-kit
131
+ *
132
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
133
+ *
134
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
135
+ */
136
+ /**
137
+ * @internal
138
+ */
139
+ var StorageAction;
140
+ (function (StorageAction) {
141
+ StorageAction["Read"] = "storageRead";
142
+ StorageAction["Write"] = "storageWrite";
143
+ StorageAction["Remove"] = "storageRemove";
144
+ StorageAction["Clear"] = "storageClear";
145
+ })(StorageAction || (StorageAction = {}));
146
+ /**
147
+ * @internal
148
+ */
149
+ var storage = new StorageImpl();
150
+ /**
151
+ * @internal
152
+ */
153
+ function listenStorageMessages(experience) {
154
+ return onSdkMessage(experience, function (message) {
155
+ var payload = {};
156
+ if (!Object.values(StorageAction).includes(message.action)) {
157
+ return;
158
+ }
159
+ switch (message.action) {
160
+ case StorageAction.Read:
161
+ payload = {
162
+ value: storage.getItem(message.payload.key),
163
+ };
164
+ break;
165
+ case StorageAction.Write:
166
+ storage.setItem(message.payload.key, message.payload.value);
167
+ break;
168
+ case StorageAction.Remove:
169
+ storage.removeItem(message.payload.key);
170
+ break;
171
+ case StorageAction.Clear:
172
+ storage.clear();
173
+ break;
174
+ }
175
+ respondToSdkMessage(experience, message, payload);
176
+ });
177
+ }
178
+ /**
179
+ * The `setStoragePersistent` function is a simple function that allows to
180
+ * control the persistence of the SDK storage. If the argument `persistent` is
181
+ * set to `true`, then the storage will be persistent across browser sessions
182
+ * and if it is set to `false`, then the storage will save to memory.
183
+ * It is important to note that the use of persistent storage may be subject
184
+ * to laws and regulations, such as those related to data privacy and protection.
185
+ *
186
+ * @remarks
187
+ * - We transition from persistent to memory and memory to persistent in
188
+ * a seamless manner for you
189
+ *
190
+ * - By default we store in memory
191
+ *
192
+ * - The value of storage persistent persists across session (aka put it to true,
193
+ * it will remain so, put it back to false, it will remain so)
194
+ *
195
+ * - You have the responsibility to comply with any laws and regulations, and
196
+ * store only the data if you know it's valid to do so
197
+ *
198
+ * @param persistent - Determines whether or not SDK storage should persist
199
+ * across browser sessions.
200
+ */
201
+ function setStoragePersistent(persistent) {
202
+ storage.persistent = persistent;
203
+ }
204
+ /**
205
+ * The function allows to read data from the SDK storage.
206
+ *
207
+ * @param key - The name of the item to be read from storage.
208
+ *
209
+ * @throws
210
+ * The function throws an error if there is a timeout reading the data
211
+ * from the parent application storage.
212
+ *
213
+ * @returns A promise that resolves to the value
214
+ * of the item in storage or `null` if the item doesn't exist.
215
+ */
216
+ function storageRead(key) {
217
+ return __awaiter(this, void 0, void 0, function () {
218
+ var parentApp, value, result;
219
+ return __generator(this, function (_a) {
220
+ switch (_a.label) {
221
+ case 0:
222
+ parentApp = getParentApplication();
223
+ value = null;
224
+ if (!(parentApp !== null)) return [3 /*break*/, 2];
225
+ return [4 /*yield*/, sendSdkRequest(parentApp, StorageAction.Read, {
226
+ key: key,
227
+ })];
228
+ case 1:
229
+ result = _a.sent();
230
+ value = result.payload.value;
231
+ return [3 /*break*/, 3];
232
+ case 2:
233
+ value = storage.getItem(key);
234
+ _a.label = 3;
235
+ case 3: return [2 /*return*/, Promise.resolve(value)];
236
+ }
237
+ });
238
+ });
239
+ }
240
+ /**
241
+ * The function allows to write data to the SDK storage.
242
+ *
243
+ * @param key - A name of the item to be stored.
244
+ * @param value - A value to be stored.
245
+ *
246
+ * @throws
247
+ * The function throws an error if there is a timeout writing the data
248
+ * to the parent's application storage.
249
+ *
250
+ * @returns A promise that resolves to `void` once the data has
251
+ * been successfully stored.
252
+ */
253
+ function storageWrite(key, value) {
254
+ return __awaiter(this, void 0, void 0, function () {
255
+ var parentApp;
256
+ return __generator(this, function (_a) {
257
+ switch (_a.label) {
258
+ case 0:
259
+ parentApp = getParentApplication();
260
+ if (!(parentApp !== null)) return [3 /*break*/, 2];
261
+ return [4 /*yield*/, sendSdkRequest(parentApp, StorageAction.Write, {
262
+ key: key,
263
+ value: value,
264
+ })];
265
+ case 1:
266
+ _a.sent();
267
+ return [3 /*break*/, 3];
268
+ case 2:
269
+ storage.setItem(key, value);
270
+ _a.label = 3;
271
+ case 3: return [2 /*return*/, Promise.resolve()];
272
+ }
273
+ });
274
+ });
275
+ }
276
+ /**
277
+ * The function allows to remove an item from the SDK storage.
278
+ *
279
+ * @param key - A name of the item to be removed.
280
+ *
281
+ * @throws
282
+ * The function throws an error if there is a timeout removing the data
283
+ * from the parent's application storage.
284
+ *
285
+ * @returns A promise that resolves to `void` once the data has
286
+ * been successfully removed.
287
+ */
288
+ function storageRemove(key) {
289
+ return __awaiter(this, void 0, void 0, function () {
290
+ var parentApp;
291
+ return __generator(this, function (_a) {
292
+ switch (_a.label) {
293
+ case 0:
294
+ parentApp = getParentApplication();
295
+ if (!(parentApp !== null)) return [3 /*break*/, 2];
296
+ return [4 /*yield*/, sendSdkRequest(parentApp, StorageAction.Remove, { key: key })];
297
+ case 1:
298
+ _a.sent();
299
+ return [3 /*break*/, 3];
300
+ case 2:
301
+ storage.removeItem(key);
302
+ _a.label = 3;
303
+ case 3: return [2 /*return*/, Promise.resolve()];
304
+ }
305
+ });
306
+ });
307
+ }
308
+ /**
309
+ * The function allows to clear all data from the SDK storage.
310
+ *
311
+ * @throws
312
+ * The function throws an error if there is a timeout clearing the parent's
313
+ * application storage.
314
+ *
315
+ * @returns A promise that resolves to `void` once the data has been
316
+ * successfully cleared.
317
+ */
318
+ function storageClear() {
319
+ return __awaiter(this, void 0, void 0, function () {
320
+ var parentApp;
321
+ return __generator(this, function (_a) {
322
+ switch (_a.label) {
323
+ case 0:
324
+ parentApp = getParentApplication();
325
+ if (!(parentApp !== null)) return [3 /*break*/, 2];
326
+ return [4 /*yield*/, sendSdkRequest(parentApp, StorageAction.Clear)];
327
+ case 1:
328
+ _a.sent();
329
+ return [3 /*break*/, 3];
330
+ case 2:
331
+ storage.clear();
332
+ _a.label = 3;
333
+ case 3: return [2 /*return*/, Promise.resolve()];
334
+ }
335
+ });
336
+ });
337
+ }
338
+ registerEmbedHook(listenStorageMessages);
339
+
340
+ export { StorageAction, listenStorageMessages, setStoragePersistent, storage, storageClear, storageRead, storageRemove, storageWrite };
341
+ //# sourceMappingURL=index.esm5.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm5.js","sources":["../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * storage_impl.ts\n * storage-kit\n *\n * Copyright © 2025 Monterosa Productions Limited. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport {\n checkAvailability,\n getItem,\n setItem,\n removeItem,\n clear,\n} from '@monterosa/sdk-util';\n\nexport class StorageImpl {\n private memoryStore: { [key: string]: string } = {};\n private accessible: boolean = checkAvailability();\n private _persistent: boolean = true;\n\n set persistent(newValue: boolean) {\n const oldValue = this._persistent;\n\n if (oldValue === newValue) {\n return;\n }\n\n const swapToStorage = newValue === true && this.accessible;\n const swapToMemory = newValue === false && this.accessible;\n\n if (swapToStorage) {\n for (const [key, value] of Object.entries(this.memoryStore)) {\n setItem(key, value);\n }\n }\n\n if (swapToMemory) {\n clear();\n }\n\n this._persistent = newValue;\n }\n\n get persistent(): boolean {\n return this._persistent;\n }\n\n getItem(key: string): string | null {\n if (this.persistent && this.accessible) {\n return getItem(key);\n }\n\n if (Object.prototype.hasOwnProperty.call(this.memoryStore, key)) {\n return this.memoryStore[key];\n }\n\n return null;\n }\n\n setItem(key: string, value: string): void {\n if (this.persistent && this.accessible) {\n setItem(key, value);\n }\n\n this.memoryStore[key] = value;\n }\n\n removeItem(key: string): void {\n if (this.persistent && this.accessible) {\n removeItem(key);\n }\n\n delete this.memoryStore[key];\n }\n\n clear(): void {\n if (this.persistent && this.accessible) {\n clear();\n }\n\n this.memoryStore = {};\n }\n}\n","/**\n * @license\n * api.ts\n * storage-kit\n *\n * Copyright © 2025 Monterosa Productions Limited. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n} from '@monterosa/sdk-launcher-kit';\n\nimport { StorageImpl } from './storage_impl';\n\n/**\n * @internal\n */\nexport enum StorageAction {\n Read = 'storageRead',\n Write = 'storageWrite',\n Remove = 'storageRemove',\n Clear = 'storageClear',\n}\n\n/**\n * @internal\n */\nexport const storage = new StorageImpl();\n\n/**\n * @internal\n */\nexport function listenStorageMessages(experience: Experience): Unsubscribe {\n return onSdkMessage(experience, (message) => {\n let payload: Payload = {};\n\n if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n storage.removeItem(message.payload.key);\n break;\n case StorageAction.Clear:\n storage.clear();\n break;\n }\n\n respondToSdkMessage(experience, message, payload);\n });\n}\n\n/**\n * The `setStoragePersistent` function is a simple function that allows to\n * control the persistence of the SDK storage. If the argument `persistent` is\n * set to `true`, then the storage will be persistent across browser sessions\n * and if it is set to `false`, then the storage will save to memory.\n * It is important to note that the use of persistent storage may be subject\n * to laws and regulations, such as those related to data privacy and protection.\n *\n * @remarks\n * - We transition from persistent to memory and memory to persistent in\n * a seamless manner for you\n *\n * - By default we store in memory\n *\n * - The value of storage persistent persists across session (aka put it to true,\n * it will remain so, put it back to false, it will remain so)\n *\n * - You have the responsibility to comply with any laws and regulations, and\n * store only the data if you know it's valid to do so\n *\n * @param persistent - Determines whether or not SDK storage should persist\n * across browser sessions.\n */\nexport function setStoragePersistent(persistent: boolean): void {\n storage.persistent = persistent;\n}\n\n/**\n * The function allows to read data from the SDK storage.\n *\n * @param key - The name of the item to be read from storage.\n *\n * @throws\n * The function throws an error if there is a timeout reading the data\n * from the parent application storage.\n *\n * @returns A promise that resolves to the value\n * of the item in storage or `null` if the item doesn't exist.\n */\nexport async function storageRead(key: string): Promise<string | null> {\n const parentApp = getParentApplication();\n\n let value: string | null = null;\n\n if (parentApp !== null) {\n const result = await sendSdkRequest(parentApp, StorageAction.Read, {\n key,\n });\n\n value = result.payload.value as string;\n } else {\n value = storage.getItem(key);\n }\n\n return Promise.resolve(value);\n}\n\n/**\n * The function allows to write data to the SDK storage.\n *\n * @param key - A name of the item to be stored.\n * @param value - A value to be stored.\n *\n * @throws\n * The function throws an error if there is a timeout writing the data\n * to the parent's application storage.\n *\n * @returns A promise that resolves to `void` once the data has\n * been successfully stored.\n */\nexport async function storageWrite(key: string, value: string): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n\n return Promise.resolve();\n}\n\n/**\n * The function allows to remove an item from the SDK storage.\n *\n * @param key - A name of the item to be removed.\n *\n * @throws\n * The function throws an error if there is a timeout removing the data\n * from the parent's application storage.\n *\n * @returns A promise that resolves to `void` once the data has\n * been successfully removed.\n */\nexport async function storageRemove(key: string): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n\n return Promise.resolve();\n}\n\n/**\n * The function allows to clear all data from the SDK storage.\n *\n * @throws\n * The function throws an error if there is a timeout clearing the parent's\n * application storage.\n *\n * @returns A promise that resolves to `void` once the data has been\n * successfully cleared.\n */\nexport async function storageClear(): Promise<void> {\n const parentApp = getParentApplication();\n\n if (parentApp !== null) {\n await sendSdkRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n\n return Promise.resolve();\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;AAkBA;IAAA;QACU,gBAAW,GAA8B,EAAE,CAAC;QAC5C,eAAU,GAAY,iBAAiB,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AO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;;wBAG9B,sBAAO,OAAO,CAAC,OAAO,EAAE,EAAC;;;;CAC1B;AAED;;;;;;;;;;;;SAYsB,aAAa,CAAC,GAAW;;;;;;oBACvC,SAAS,GAAG,oBAAoB,EAAE,CAAC;0BAErC,SAAS,KAAK,IAAI,CAAA,EAAlB,wBAAkB;oBACpB,qBAAM,cAAc,CAAC,SAAS,EAAE,aAAa,CAAC,MAAM,EAAE,EAAE,GAAG,KAAA,EAAE,CAAC,EAAA;;oBAA9D,SAA8D,CAAC;;;oBAE/D,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;;wBAG1B,sBAAO,OAAO,CAAC,OAAO,EAAE,EAAC;;;;CAC1B;AAED;;;;;;;;;;SAUsB,YAAY;;;;;;oBAC1B,SAAS,GAAG,oBAAoB,EAAE,CAAC;0BAErC,SAAS,KAAK,IAAI,CAAA,EAAlB,wBAAkB;oBACpB,qBAAM,cAAc,CAAC,SAAS,EAAE,aAAa,CAAC,KAAK,CAAC,EAAA;;oBAApD,SAAoD,CAAC;;;oBAErD,OAAO,CAAC,KAAK,EAAE,CAAC;;wBAGlB,sBAAO,OAAO,CAAC,OAAO,EAAE,EAAC;;;;CAC1B;AAED,iBAAiB,CAAC,qBAAqB,CAAC;;;;"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @license
3
+ * storage_impl.ts
4
+ * storage-kit
5
+ *
6
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
7
+ *
8
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
9
+ */
10
+ export declare class StorageImpl {
11
+ private memoryStore;
12
+ private accessible;
13
+ private _persistent;
14
+ set persistent(newValue: boolean);
15
+ get persistent(): boolean;
16
+ getItem(key: string): string | null;
17
+ setItem(key: string, value: string): void;
18
+ removeItem(key: string): void;
19
+ clear(): void;
20
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@monterosa/sdk-storage-kit",
3
+ "version": "0.18.2-rc.1",
4
+ "description": "Storage Kit for the Monterosa JS SDK",
5
+ "author": "Monterosa <hello@monterosa.co.uk> (https://www.monterosa.co/)",
6
+ "main": "dist/index.cjs.js",
7
+ "browser": "dist/index.esm2017.js",
8
+ "module": "dist/index.esm2017.js",
9
+ "esm5": "dist/index.esm5.js",
10
+ "types": "./dist/index.d.ts",
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "scripts": {
15
+ "dev": "rollup -c -w",
16
+ "build": "rollup -c",
17
+ "test": "jest --color",
18
+ "test:watch": "jest --color --watchAll",
19
+ "test:ci": "jest --color --ci --reporters=jest-junit --coverage",
20
+ "api-report": "api-extractor run --local --verbose"
21
+ },
22
+ "keywords": [],
23
+ "license": "MIT",
24
+ "peerDependencies": {
25
+ "@monterosa/sdk-core": "0.x",
26
+ "@monterosa/sdk-launcher-kit": "0.x",
27
+ "@monterosa/sdk-util": "0.x"
28
+ },
29
+ "dependencies": {
30
+ "@monterosa/sdk-core": "^0.18.2-rc.1",
31
+ "@monterosa/sdk-launcher-kit": "^0.18.2-rc.1",
32
+ "@monterosa/sdk-util": "^0.18.2-rc.1"
33
+ },
34
+ "devDependencies": {
35
+ "@rollup/plugin-json": "^4.1.0",
36
+ "@types/jest": "^27.0.1",
37
+ "jest": "^27.1.0",
38
+ "jest-junit": "^13.0.0",
39
+ "rollup": "^2.57.0",
40
+ "rollup-plugin-typescript2": "^0.30.0",
41
+ "ts-jest": "^27.0.5",
42
+ "tslib": "^2.3.0",
43
+ "typescript": "^4.4.2"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "gitHead": "d2903789e355e14e4c119dcc14c1a25cc4e6b940"
49
+ }