@monterosa/sdk-storage-kit 0.18.9 → 0.18.10-local-only-storage.2

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/dist/api.d.ts CHANGED
@@ -24,6 +24,27 @@ export declare function listenStorageMessages(experience: Experience): Unsubscri
24
24
  */
25
25
  export declare function parentAppRequest(parentApp: ParentApplication, action: StorageAction.Read, payload?: Payload): Promise<string | null>;
26
26
  export declare function parentAppRequest(parentApp: ParentApplication, action: StorageAction.Write | StorageAction.Remove | StorageAction.Clear, payload?: Payload): Promise<undefined>;
27
+ /**
28
+ * The `setStorageLocalOnly` function allows to control whether the SDK storage
29
+ * should use only the local context storage, bypassing communication with the
30
+ * parent application via the bridge.
31
+ *
32
+ * @remarks
33
+ * - When set to `true`, all storage operations (`storageRead`, `storageWrite`,
34
+ * `storageRemove`, `storageClear`) will use the local `StorageImpl` directly,
35
+ * even when a parent application is detected
36
+ *
37
+ * - This is useful when the parent page runs an older version of the SDK that
38
+ * does not include the storage-kit, which would otherwise cause a 20-second
39
+ * timeout on every storage operation
40
+ *
41
+ * - By default, `localOnly` is set to `false`, meaning the bridge will be used
42
+ * when a parent application is present
43
+ *
44
+ * @param localOnly - Determines whether or not SDK storage should bypass the
45
+ * parent application bridge and use only local storage.
46
+ */
47
+ export declare function setStorageLocalOnly(localOnly: boolean): void;
27
48
  /**
28
49
  * The `setStoragePersistent` function is a simple function that allows to
29
50
  * control the persistence of the SDK storage. If the argument `persistent` is
package/dist/index.cjs.js CHANGED
@@ -68,11 +68,26 @@ function __generator(thisArg, body) {
68
68
  * More details on the license can be found at https://www.monterosa.co/sdk/license
69
69
  */
70
70
  var _a;
71
+ /**
72
+ * Storage bridge action types used internally to communicate with the parent application.
73
+ */
71
74
  var StorageAction;
72
75
  (function (StorageAction) {
76
+ /**
77
+ * Read a value from storage.
78
+ */
73
79
  StorageAction["Read"] = "storageRead";
80
+ /**
81
+ * Write a value to storage.
82
+ */
74
83
  StorageAction["Write"] = "storageWrite";
84
+ /**
85
+ * Remove a value from storage.
86
+ */
75
87
  StorageAction["Remove"] = "storageRemove";
88
+ /**
89
+ * Clear all values from storage.
90
+ */
76
91
  StorageAction["Clear"] = "storageClear";
77
92
  })(StorageAction || (StorageAction = {}));
78
93
  /**
@@ -84,7 +99,7 @@ var StorageAction;
84
99
  * try {
85
100
  * // some code that uses the StorageKit
86
101
  * } catch (err) {
87
- * if (err.code === StorageError.ParentApp) {
102
+ * if (err.code === StorageError.ParentAppError) {
88
103
  * // handle parent app error
89
104
  * } else {
90
105
  * // handle other error types
@@ -159,7 +174,18 @@ var StorageImpl = /** @class */ (function () {
159
174
  this.memoryStore = {};
160
175
  this.accessible = sdkUtil.checkAvailability();
161
176
  this._persistent = true;
177
+ this._localOnly = false;
162
178
  }
179
+ Object.defineProperty(StorageImpl.prototype, "localOnly", {
180
+ get: function () {
181
+ return this._localOnly;
182
+ },
183
+ set: function (newValue) {
184
+ this._localOnly = newValue;
185
+ },
186
+ enumerable: false,
187
+ configurable: true
188
+ });
163
189
  Object.defineProperty(StorageImpl.prototype, "persistent", {
164
190
  get: function () {
165
191
  return this._persistent;
@@ -301,6 +327,29 @@ function parentAppRequest(parentApp, action, payload) {
301
327
  });
302
328
  });
303
329
  }
330
+ /**
331
+ * The `setStorageLocalOnly` function allows to control whether the SDK storage
332
+ * should use only the local context storage, bypassing communication with the
333
+ * parent application via the bridge.
334
+ *
335
+ * @remarks
336
+ * - When set to `true`, all storage operations (`storageRead`, `storageWrite`,
337
+ * `storageRemove`, `storageClear`) will use the local `StorageImpl` directly,
338
+ * even when a parent application is detected
339
+ *
340
+ * - This is useful when the parent page runs an older version of the SDK that
341
+ * does not include the storage-kit, which would otherwise cause a 20-second
342
+ * timeout on every storage operation
343
+ *
344
+ * - By default, `localOnly` is set to `false`, meaning the bridge will be used
345
+ * when a parent application is present
346
+ *
347
+ * @param localOnly - Determines whether or not SDK storage should bypass the
348
+ * parent application bridge and use only local storage.
349
+ */
350
+ function setStorageLocalOnly(localOnly) {
351
+ storage.localOnly = localOnly;
352
+ }
304
353
  /**
305
354
  * The `setStoragePersistent` function is a simple function that allows to
306
355
  * control the persistence of the SDK storage. If the argument `persistent` is
@@ -350,7 +399,7 @@ function storageRead(key) {
350
399
  _a.label = 1;
351
400
  case 1:
352
401
  _a.trys.push([1, 5, , 6]);
353
- if (!(parentApp !== null)) return [3 /*break*/, 3];
402
+ if (!(parentApp !== null && !storage.localOnly)) return [3 /*break*/, 3];
354
403
  return [4 /*yield*/, parentAppRequest(parentApp, StorageAction.Read, {
355
404
  key: key,
356
405
  })];
@@ -394,7 +443,7 @@ function storageWrite(key, value) {
394
443
  _a.label = 1;
395
444
  case 1:
396
445
  _a.trys.push([1, 5, , 6]);
397
- if (!(parentApp !== null)) return [3 /*break*/, 3];
446
+ if (!(parentApp !== null && !storage.localOnly)) return [3 /*break*/, 3];
398
447
  return [4 /*yield*/, parentAppRequest(parentApp, StorageAction.Write, {
399
448
  key: key,
400
449
  value: value,
@@ -438,7 +487,7 @@ function storageRemove(key) {
438
487
  _a.label = 1;
439
488
  case 1:
440
489
  _a.trys.push([1, 5, , 6]);
441
- if (!(parentApp !== null)) return [3 /*break*/, 3];
490
+ if (!(parentApp !== null && !storage.localOnly)) return [3 /*break*/, 3];
442
491
  return [4 /*yield*/, parentAppRequest(parentApp, StorageAction.Remove, { key: key })];
443
492
  case 2:
444
493
  _a.sent();
@@ -477,7 +526,7 @@ function storageClear() {
477
526
  _a.label = 1;
478
527
  case 1:
479
528
  _a.trys.push([1, 5, , 6]);
480
- if (!(parentApp !== null)) return [3 /*break*/, 3];
529
+ if (!(parentApp !== null && !storage.localOnly)) return [3 /*break*/, 3];
481
530
  return [4 /*yield*/, parentAppRequest(parentApp, StorageAction.Clear)];
482
531
  case 2:
483
532
  _a.sent();
@@ -500,6 +549,7 @@ sdkLauncherKit.registerEmbedHook(listenStorageMessages);
500
549
 
501
550
  exports.listenStorageMessages = listenStorageMessages;
502
551
  exports.parentAppRequest = parentAppRequest;
552
+ exports.setStorageLocalOnly = setStorageLocalOnly;
503
553
  exports.setStoragePersistent = setStoragePersistent;
504
554
  exports.storage = storage;
505
555
  exports.storageClear = storageClear;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/types.ts","../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * types.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\nexport enum StorageAction {\n Read = 'storageRead',\n Write = 'storageWrite',\n Remove = 'storageRemove',\n Clear = 'storageClear',\n}\n\nexport type ResponsePayload = {\n key: string;\n value?: string | null;\n error?: string;\n};\n\n/**\n * Defines a set of error codes that may be encountered when using the\n * Storage kit of the Monterosa SDK.\n *\n * @example\n * ```javascript\n * try {\n * // some code that uses the StorageKit\n * } catch (err) {\n * if (err.code === StorageError.ParentApp) {\n * // handle parent app error\n * } else {\n * // handle other error types\n * }\n * }\n * ```\n *\n * @remarks\n * - The `StorageError` enum provides a convenient way to handle errors\n * encountered when using the `StorageKit` module. By checking the code\n * property of the caught error against the values of the enum, the error\n * type can be determined and appropriate action taken.\n *\n * - The `StorageError` enum is not intended to be instantiated or extended.\n */\nexport enum StorageError {\n /**\n * Indicates an error occurred in the parent app.\n */\n ParentAppError = 'parent_app_error',\n /**\n * Indicates a timeout occurred when communicating with the parent app.\n */\n ParentTimeoutError = 'parent_timeout_error',\n /**\n * Indicates an error occurred when reading from the storage.\n */\n ReadError = 'read_error',\n /**\n * Indicates an error occurred when writing to the storage.\n */\n WriteError = 'write_error',\n /**\n * Indicates an error occurred when removing from the storage.\n */\n RemoveError = 'remove_error',\n /**\n * Indicates an error occurred when clearing the storage.\n */\n ClearError = 'clear_error',\n}\n\n/**\n * @internal\n */\nexport const StorageErrorMessages = {\n [StorageError.ParentAppError]: (error: string) =>\n `Parent application error: ${error}`,\n [StorageError.ParentTimeoutError]: (error: string) =>\n `Parent application timeout: ${error}. Please check if the storage-kit is imported on the parent page.`,\n [StorageError.ReadError]: (error: string) => `Storage read error: ${error}`,\n [StorageError.WriteError]: (error: string) => `Storage write error: ${error}`,\n [StorageError.RemoveError]: (error: string) =>\n `Storage remove error: ${error}`,\n [StorageError.ClearError]: (error: string) => `Storage clear error: ${error}`,\n};\n","/**\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 {\n MonterosaError,\n Unsubscribe,\n createError,\n getErrorMessage,\n} from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n ParentApplication,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n BridgeError,\n} from '@monterosa/sdk-launcher-kit';\n\nimport {\n StorageAction,\n ResponsePayload,\n StorageError,\n StorageErrorMessages,\n} from './types';\nimport { StorageImpl } from './storage_impl';\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 if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n try {\n let payload: Payload = {};\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n key: message.payload.key,\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n payload = {\n key: message.payload.key,\n };\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n payload = {\n key: message.payload.key,\n };\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 } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to handle storage message ${message.action} with error: ${errorMessage}`,\n );\n\n respondToSdkMessage(experience, message, {\n key: message.payload.key,\n error: errorMessage,\n });\n }\n });\n}\n\n/**\n * @internal\n */\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Read,\n payload?: Payload,\n): Promise<string | null>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Write | StorageAction.Remove | StorageAction.Clear,\n payload?: Payload,\n): Promise<undefined>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction,\n payload?: Payload,\n): Promise<string | null | undefined> {\n try {\n const response = await sendSdkRequest(parentApp, action, payload);\n\n const { value, error } = response.payload as ResponsePayload;\n\n if (error !== undefined) {\n throw createError(\n StorageError.ParentAppError,\n StorageErrorMessages,\n error,\n );\n }\n\n return value;\n } catch (err) {\n if (\n err instanceof MonterosaError &&\n err.code === BridgeError.RequestTimeoutError\n ) {\n const errorMessage = getErrorMessage(err);\n\n throw createError(\n StorageError.ParentTimeoutError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n throw err;\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 try {\n if (parentApp !== null) {\n value = await parentAppRequest(parentApp, StorageAction.Read, {\n key,\n });\n } else {\n value = storage.getItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to read storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.ReadError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n return 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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to write storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.WriteError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to remove storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.RemoveError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(`Unable to clear storage with error: ${errorMessage}`);\n\n throw createError(\n StorageError.ClearError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":["StorageError","checkAvailability","setItem","clear","getItem","removeItem","onSdkMessage","respondToSdkMessage","getErrorMessage","sendSdkRequest","createError","MonterosaError","BridgeError","getParentApplication","registerEmbedHook"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;AAUA,IAAY,aAKX;AALD,WAAY,aAAa;IACvB,qCAAoB,CAAA;IACpB,uCAAsB,CAAA;IACtB,yCAAwB,CAAA;IACxB,uCAAsB,CAAA;AACxB,CAAC,EALW,aAAa,KAAb,aAAa,QAKxB;AAQD;;;;;;;;;;;;;;;;;;;;;;;;;AAyBYA;AAAZ,WAAY,YAAY;;;;IAItB,mDAAmC,CAAA;;;;IAInC,2DAA2C,CAAA;;;;IAI3C,wCAAwB,CAAA;;;;IAIxB,0CAA0B,CAAA;;;;IAI1B,4CAA4B,CAAA;;;;IAI5B,0CAA0B,CAAA;AAC5B,CAAC,EAzBWA,oBAAY,KAAZA,oBAAY,QAyBvB;AAED;;;AAGO,IAAM,oBAAoB;IAC/B,GAACA,oBAAY,CAAC,cAAc,IAAG,UAAC,KAAa;QAC3C,OAAA,+BAA6B,KAAO;KAAA;IACtC,GAACA,oBAAY,CAAC,kBAAkB,IAAG,UAAC,KAAa;QAC/C,OAAA,iCAA+B,KAAK,sEAAmE;KAAA;IACzG,GAACA,oBAAY,CAAC,SAAS,IAAG,UAAC,KAAa,IAAK,OAAA,yBAAuB,KAAO,GAAA;IAC3E,GAACA,oBAAY,CAAC,UAAU,IAAG,UAAC,KAAa,IAAK,OAAA,0BAAwB,KAAO,GAAA;IAC7E,GAACA,oBAAY,CAAC,WAAW,IAAG,UAAC,KAAa;QACxC,OAAA,2BAAyB,KAAO;KAAA;IAClC,GAACA,oBAAY,CAAC,UAAU,IAAG,UAAC,KAAa,IAAK,OAAA,0BAAwB,KAAO,GAAA;OAC9E;;ACxFD;;;;;;;;;AAkBA;IAAA;QACU,gBAAW,GAA8B,EAAE,CAAC;QAC5C,eAAU,GAAY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B,oBAAoB,EACpB,YAAY,CACb,CAAC;;;;;CAEL;AAEDc,gCAAiB,CAAC,qBAAqB,CAAC;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/types.ts","../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * types.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\n/**\n * Storage bridge action types used internally to communicate with the parent application.\n */\nexport enum StorageAction {\n /**\n * Read a value from storage.\n */\n Read = 'storageRead',\n /**\n * Write a value to storage.\n */\n Write = 'storageWrite',\n /**\n * Remove a value from storage.\n */\n Remove = 'storageRemove',\n /**\n * Clear all values from storage.\n */\n Clear = 'storageClear',\n}\n\n/**\n * Response payload returned from a storage bridge request.\n */\nexport type ResponsePayload = {\n /**\n * The storage key that was operated on.\n */\n key: string;\n /**\n * The value read from storage, or `null` if not found. Only present for read\n * operations.\n */\n value?: string | null;\n /**\n * Error message, if the operation failed.\n */\n error?: string;\n};\n\n/**\n * Defines a set of error codes that may be encountered when using the\n * Storage kit of the Monterosa SDK.\n *\n * @example\n * ```javascript\n * try {\n * // some code that uses the StorageKit\n * } catch (err) {\n * if (err.code === StorageError.ParentAppError) {\n * // handle parent app error\n * } else {\n * // handle other error types\n * }\n * }\n * ```\n *\n * @remarks\n * - The `StorageError` enum provides a convenient way to handle errors\n * encountered when using the `StorageKit` module. By checking the code\n * property of the caught error against the values of the enum, the error\n * type can be determined and appropriate action taken.\n *\n * - The `StorageError` enum is not intended to be instantiated or extended.\n */\nexport enum StorageError {\n /**\n * Indicates an error occurred in the parent app.\n */\n ParentAppError = 'parent_app_error',\n /**\n * Indicates a timeout occurred when communicating with the parent app.\n */\n ParentTimeoutError = 'parent_timeout_error',\n /**\n * Indicates an error occurred when reading from the storage.\n */\n ReadError = 'read_error',\n /**\n * Indicates an error occurred when writing to the storage.\n */\n WriteError = 'write_error',\n /**\n * Indicates an error occurred when removing from the storage.\n */\n RemoveError = 'remove_error',\n /**\n * Indicates an error occurred when clearing the storage.\n */\n ClearError = 'clear_error',\n}\n\n/**\n * @internal\n */\nexport const StorageErrorMessages = {\n [StorageError.ParentAppError]: (error: string) =>\n `Parent application error: ${error}`,\n [StorageError.ParentTimeoutError]: (error: string) =>\n `Parent application timeout: ${error}. Please check if the storage-kit is imported on the parent page.`,\n [StorageError.ReadError]: (error: string) => `Storage read error: ${error}`,\n [StorageError.WriteError]: (error: string) => `Storage write error: ${error}`,\n [StorageError.RemoveError]: (error: string) =>\n `Storage remove error: ${error}`,\n [StorageError.ClearError]: (error: string) => `Storage clear error: ${error}`,\n};\n","/**\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 private _localOnly: boolean = false;\n\n set localOnly(newValue: boolean) {\n this._localOnly = newValue;\n }\n\n get localOnly(): boolean {\n return this._localOnly;\n }\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 {\n MonterosaError,\n Unsubscribe,\n createError,\n getErrorMessage,\n} from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n ParentApplication,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n BridgeError,\n} from '@monterosa/sdk-launcher-kit';\n\nimport {\n StorageAction,\n ResponsePayload,\n StorageError,\n StorageErrorMessages,\n} from './types';\nimport { StorageImpl } from './storage_impl';\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 if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n try {\n let payload: Payload = {};\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n key: message.payload.key,\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n payload = {\n key: message.payload.key,\n };\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n payload = {\n key: message.payload.key,\n };\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 } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to handle storage message ${message.action} with error: ${errorMessage}`,\n );\n\n respondToSdkMessage(experience, message, {\n key: message.payload.key,\n error: errorMessage,\n });\n }\n });\n}\n\n/**\n * @internal\n */\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Read,\n payload?: Payload,\n): Promise<string | null>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Write | StorageAction.Remove | StorageAction.Clear,\n payload?: Payload,\n): Promise<undefined>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction,\n payload?: Payload,\n): Promise<string | null | undefined> {\n try {\n const response = await sendSdkRequest(parentApp, action, payload);\n\n const { value, error } = response.payload as ResponsePayload;\n\n if (error !== undefined) {\n throw createError(\n StorageError.ParentAppError,\n StorageErrorMessages,\n error,\n );\n }\n\n return value;\n } catch (err) {\n if (\n err instanceof MonterosaError &&\n err.code === BridgeError.RequestTimeoutError\n ) {\n const errorMessage = getErrorMessage(err);\n\n throw createError(\n StorageError.ParentTimeoutError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n throw err;\n }\n}\n\n/**\n * The `setStorageLocalOnly` function allows to control whether the SDK storage\n * should use only the local context storage, bypassing communication with the\n * parent application via the bridge.\n *\n * @remarks\n * - When set to `true`, all storage operations (`storageRead`, `storageWrite`,\n * `storageRemove`, `storageClear`) will use the local `StorageImpl` directly,\n * even when a parent application is detected\n *\n * - This is useful when the parent page runs an older version of the SDK that\n * does not include the storage-kit, which would otherwise cause a 20-second\n * timeout on every storage operation\n *\n * - By default, `localOnly` is set to `false`, meaning the bridge will be used\n * when a parent application is present\n *\n * @param localOnly - Determines whether or not SDK storage should bypass the\n * parent application bridge and use only local storage.\n */\nexport function setStorageLocalOnly(localOnly: boolean): void {\n storage.localOnly = localOnly;\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 try {\n if (parentApp !== null && !storage.localOnly) {\n value = await parentAppRequest(parentApp, StorageAction.Read, {\n key,\n });\n } else {\n value = storage.getItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to read storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.ReadError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n return 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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to write storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.WriteError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to remove storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.RemoveError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(`Unable to clear storage with error: ${errorMessage}`);\n\n throw createError(\n StorageError.ClearError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":["StorageError","checkAvailability","setItem","clear","getItem","removeItem","onSdkMessage","respondToSdkMessage","getErrorMessage","sendSdkRequest","createError","MonterosaError","BridgeError","getParentApplication","registerEmbedHook"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;AAQG;;AAEH;;AAEG;AACH,IAAY,aAiBX,CAAA;AAjBD,CAAA,UAAY,aAAa,EAAA;AACvB;;AAEG;AACH,IAAA,aAAA,CAAA,MAAA,CAAA,GAAA,aAAoB,CAAA;AACpB;;AAEG;AACH,IAAA,aAAA,CAAA,OAAA,CAAA,GAAA,cAAsB,CAAA;AACtB;;AAEG;AACH,IAAA,aAAA,CAAA,QAAA,CAAA,GAAA,eAAwB,CAAA;AACxB;;A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,YAAc,CAAC,CAAC;oBAErE,MAAME,mBAAW,CACfV,oBAAY,CAAC,UAAU,EACvB,oBAAoB,EACpB,YAAY,CACb,CAAC;;;;;AAEL,CAAA;AAEDc,gCAAiB,CAAC,qBAAqB,CAAC;;;;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Persists key-value data across sessions via the parent application.
3
+ *
4
+ * @packageDocumentation
5
+ */
1
6
  /**
2
7
  * @license
3
8
  * index.ts
@@ -7,10 +12,5 @@
7
12
  *
8
13
  * More details on the license can be found at https://www.monterosa.co/sdk/license
9
14
  */
10
- /**
11
- * Monterosa SDK / Storage Kit
12
- *
13
- * @packageDocumentation
14
- */
15
15
  export * from './api';
16
16
  export { StorageError } from './types';
@@ -10,11 +10,26 @@ import { registerEmbedHook, onSdkMessage, respondToSdkMessage, sendSdkRequest, B
10
10
  *
11
11
  * More details on the license can be found at https://www.monterosa.co/sdk/license
12
12
  */
13
+ /**
14
+ * Storage bridge action types used internally to communicate with the parent application.
15
+ */
13
16
  var StorageAction;
14
17
  (function (StorageAction) {
18
+ /**
19
+ * Read a value from storage.
20
+ */
15
21
  StorageAction["Read"] = "storageRead";
22
+ /**
23
+ * Write a value to storage.
24
+ */
16
25
  StorageAction["Write"] = "storageWrite";
26
+ /**
27
+ * Remove a value from storage.
28
+ */
17
29
  StorageAction["Remove"] = "storageRemove";
30
+ /**
31
+ * Clear all values from storage.
32
+ */
18
33
  StorageAction["Clear"] = "storageClear";
19
34
  })(StorageAction || (StorageAction = {}));
20
35
  /**
@@ -26,7 +41,7 @@ var StorageAction;
26
41
  * try {
27
42
  * // some code that uses the StorageKit
28
43
  * } catch (err) {
29
- * if (err.code === StorageError.ParentApp) {
44
+ * if (err.code === StorageError.ParentAppError) {
30
45
  * // handle parent app error
31
46
  * } else {
32
47
  * // handle other error types
@@ -95,6 +110,13 @@ class StorageImpl {
95
110
  this.memoryStore = {};
96
111
  this.accessible = checkAvailability();
97
112
  this._persistent = true;
113
+ this._localOnly = false;
114
+ }
115
+ set localOnly(newValue) {
116
+ this._localOnly = newValue;
117
+ }
118
+ get localOnly() {
119
+ return this._localOnly;
98
120
  }
99
121
  set persistent(newValue) {
100
122
  const oldValue = this._persistent;
@@ -221,6 +243,29 @@ async function parentAppRequest(parentApp, action, payload) {
221
243
  throw err;
222
244
  }
223
245
  }
246
+ /**
247
+ * The `setStorageLocalOnly` function allows to control whether the SDK storage
248
+ * should use only the local context storage, bypassing communication with the
249
+ * parent application via the bridge.
250
+ *
251
+ * @remarks
252
+ * - When set to `true`, all storage operations (`storageRead`, `storageWrite`,
253
+ * `storageRemove`, `storageClear`) will use the local `StorageImpl` directly,
254
+ * even when a parent application is detected
255
+ *
256
+ * - This is useful when the parent page runs an older version of the SDK that
257
+ * does not include the storage-kit, which would otherwise cause a 20-second
258
+ * timeout on every storage operation
259
+ *
260
+ * - By default, `localOnly` is set to `false`, meaning the bridge will be used
261
+ * when a parent application is present
262
+ *
263
+ * @param localOnly - Determines whether or not SDK storage should bypass the
264
+ * parent application bridge and use only local storage.
265
+ */
266
+ function setStorageLocalOnly(localOnly) {
267
+ storage.localOnly = localOnly;
268
+ }
224
269
  /**
225
270
  * The `setStoragePersistent` function is a simple function that allows to
226
271
  * control the persistence of the SDK storage. If the argument `persistent` is
@@ -263,7 +308,7 @@ async function storageRead(key) {
263
308
  const parentApp = getParentApplication();
264
309
  let value = null;
265
310
  try {
266
- if (parentApp !== null) {
311
+ if (parentApp !== null && !storage.localOnly) {
267
312
  value = await parentAppRequest(parentApp, StorageAction.Read, {
268
313
  key,
269
314
  });
@@ -295,7 +340,7 @@ async function storageRead(key) {
295
340
  async function storageWrite(key, value) {
296
341
  const parentApp = getParentApplication();
297
342
  try {
298
- if (parentApp !== null) {
343
+ if (parentApp !== null && !storage.localOnly) {
299
344
  await parentAppRequest(parentApp, StorageAction.Write, {
300
345
  key,
301
346
  value,
@@ -326,7 +371,7 @@ async function storageWrite(key, value) {
326
371
  async function storageRemove(key) {
327
372
  const parentApp = getParentApplication();
328
373
  try {
329
- if (parentApp !== null) {
374
+ if (parentApp !== null && !storage.localOnly) {
330
375
  await parentAppRequest(parentApp, StorageAction.Remove, { key });
331
376
  }
332
377
  else {
@@ -352,7 +397,7 @@ async function storageRemove(key) {
352
397
  async function storageClear() {
353
398
  const parentApp = getParentApplication();
354
399
  try {
355
- if (parentApp !== null) {
400
+ if (parentApp !== null && !storage.localOnly) {
356
401
  await parentAppRequest(parentApp, StorageAction.Clear);
357
402
  }
358
403
  else {
@@ -367,5 +412,5 @@ async function storageClear() {
367
412
  }
368
413
  registerEmbedHook(listenStorageMessages);
369
414
 
370
- export { StorageError, listenStorageMessages, parentAppRequest, setStoragePersistent, storage, storageClear, storageRead, storageRemove, storageWrite };
415
+ export { StorageError, listenStorageMessages, parentAppRequest, setStorageLocalOnly, setStoragePersistent, storage, storageClear, storageRead, storageRemove, storageWrite };
371
416
  //# sourceMappingURL=index.esm2017.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm2017.js","sources":["../src/types.ts","../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * types.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\nexport enum StorageAction {\n Read = 'storageRead',\n Write = 'storageWrite',\n Remove = 'storageRemove',\n Clear = 'storageClear',\n}\n\nexport type ResponsePayload = {\n key: string;\n value?: string | null;\n error?: string;\n};\n\n/**\n * Defines a set of error codes that may be encountered when using the\n * Storage kit of the Monterosa SDK.\n *\n * @example\n * ```javascript\n * try {\n * // some code that uses the StorageKit\n * } catch (err) {\n * if (err.code === StorageError.ParentApp) {\n * // handle parent app error\n * } else {\n * // handle other error types\n * }\n * }\n * ```\n *\n * @remarks\n * - The `StorageError` enum provides a convenient way to handle errors\n * encountered when using the `StorageKit` module. By checking the code\n * property of the caught error against the values of the enum, the error\n * type can be determined and appropriate action taken.\n *\n * - The `StorageError` enum is not intended to be instantiated or extended.\n */\nexport enum StorageError {\n /**\n * Indicates an error occurred in the parent app.\n */\n ParentAppError = 'parent_app_error',\n /**\n * Indicates a timeout occurred when communicating with the parent app.\n */\n ParentTimeoutError = 'parent_timeout_error',\n /**\n * Indicates an error occurred when reading from the storage.\n */\n ReadError = 'read_error',\n /**\n * Indicates an error occurred when writing to the storage.\n */\n WriteError = 'write_error',\n /**\n * Indicates an error occurred when removing from the storage.\n */\n RemoveError = 'remove_error',\n /**\n * Indicates an error occurred when clearing the storage.\n */\n ClearError = 'clear_error',\n}\n\n/**\n * @internal\n */\nexport const StorageErrorMessages = {\n [StorageError.ParentAppError]: (error: string) =>\n `Parent application error: ${error}`,\n [StorageError.ParentTimeoutError]: (error: string) =>\n `Parent application timeout: ${error}. Please check if the storage-kit is imported on the parent page.`,\n [StorageError.ReadError]: (error: string) => `Storage read error: ${error}`,\n [StorageError.WriteError]: (error: string) => `Storage write error: ${error}`,\n [StorageError.RemoveError]: (error: string) =>\n `Storage remove error: ${error}`,\n [StorageError.ClearError]: (error: string) => `Storage clear error: ${error}`,\n};\n","/**\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 {\n MonterosaError,\n Unsubscribe,\n createError,\n getErrorMessage,\n} from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n ParentApplication,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n BridgeError,\n} from '@monterosa/sdk-launcher-kit';\n\nimport {\n StorageAction,\n ResponsePayload,\n StorageError,\n StorageErrorMessages,\n} from './types';\nimport { StorageImpl } from './storage_impl';\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 if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n try {\n let payload: Payload = {};\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n key: message.payload.key,\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n payload = {\n key: message.payload.key,\n };\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n payload = {\n key: message.payload.key,\n };\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 } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to handle storage message ${message.action} with error: ${errorMessage}`,\n );\n\n respondToSdkMessage(experience, message, {\n key: message.payload.key,\n error: errorMessage,\n });\n }\n });\n}\n\n/**\n * @internal\n */\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Read,\n payload?: Payload,\n): Promise<string | null>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Write | StorageAction.Remove | StorageAction.Clear,\n payload?: Payload,\n): Promise<undefined>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction,\n payload?: Payload,\n): Promise<string | null | undefined> {\n try {\n const response = await sendSdkRequest(parentApp, action, payload);\n\n const { value, error } = response.payload as ResponsePayload;\n\n if (error !== undefined) {\n throw createError(\n StorageError.ParentAppError,\n StorageErrorMessages,\n error,\n );\n }\n\n return value;\n } catch (err) {\n if (\n err instanceof MonterosaError &&\n err.code === BridgeError.RequestTimeoutError\n ) {\n const errorMessage = getErrorMessage(err);\n\n throw createError(\n StorageError.ParentTimeoutError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n throw err;\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 try {\n if (parentApp !== null) {\n value = await parentAppRequest(parentApp, StorageAction.Read, {\n key,\n });\n } else {\n value = storage.getItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to read storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.ReadError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n return 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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to write storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.WriteError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to remove storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.RemoveError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(`Unable to clear storage with error: ${errorMessage}`);\n\n throw createError(\n StorageError.ClearError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":[],"mappings":";;;AAAA;;;;;;;;;AAUA,IAAY,aAKX;AALD,WAAY,aAAa;IACvB,qCAAoB,CAAA;IACpB,uCAAsB,CAAA;IACtB,yCAAwB,CAAA;IACxB,uCAAsB,CAAA;AACxB,CAAC,EALW,aAAa,KAAb,aAAa,QAKxB;AAQD;;;;;;;;;;;;;;;;;;;;;;;;;IAyBY;AAAZ,WAAY,YAAY;;;;IAItB,mDAAmC,CAAA;;;;IAInC,2DAA2C,CAAA;;;;IAI3C,wCAAwB,CAAA;;;;IAIxB,0CAA0B,CAAA;;;;IAI1B,4CAA4B,CAAA;;;;IAI5B,0CAA0B,CAAA;AAC5B,CAAC,EAzBW,YAAY,KAAZ,YAAY,QAyBvB;AAED;;;AAGO,MAAM,oBAAoB,GAAG;IAClC,CAAC,YAAY,CAAC,cAAc,GAAG,CAAC,KAAa,KAC3C,6BAA6B,KAAK,EAAE;IACtC,CAAC,YAAY,CAAC,kBAAkB,GAAG,CAAC,KAAa,KAC/C,+BAA+B,KAAK,mEAAmE;IACzG,CAAC,YAAY,CAAC,SAAS,GAAG,CAAC,KAAa,KAAK,uBAAuB,KAAK,EAAE;IAC3E,CAAC,YAAY,CAAC,UAAU,GAAG,CAAC,KAAa,KAAK,wBAAwB,KAAK,EAAE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AAY,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;QAE1C,OAAO,CAAC,KAAK,CAAC,uCAAuC,YAAY,EAAE,CAAC,CAAC;QAErE,MAAM,WAAW,CACf,YAAY,CAAC,UAAU,EACvB,oBAAoB,EACpB,YAAY,CACb,CAAC;KACH;AACH,CAAC;AAED,iBAAiB,CAAC,qBAAqB,CAAC;;;;"}
1
+ {"version":3,"file":"index.esm2017.js","sources":["../src/types.ts","../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * types.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\n/**\n * Storage bridge action types used internally to communicate with the parent application.\n */\nexport enum StorageAction {\n /**\n * Read a value from storage.\n */\n Read = 'storageRead',\n /**\n * Write a value to storage.\n */\n Write = 'storageWrite',\n /**\n * Remove a value from storage.\n */\n Remove = 'storageRemove',\n /**\n * Clear all values from storage.\n */\n Clear = 'storageClear',\n}\n\n/**\n * Response payload returned from a storage bridge request.\n */\nexport type ResponsePayload = {\n /**\n * The storage key that was operated on.\n */\n key: string;\n /**\n * The value read from storage, or `null` if not found. Only present for read\n * operations.\n */\n value?: string | null;\n /**\n * Error message, if the operation failed.\n */\n error?: string;\n};\n\n/**\n * Defines a set of error codes that may be encountered when using the\n * Storage kit of the Monterosa SDK.\n *\n * @example\n * ```javascript\n * try {\n * // some code that uses the StorageKit\n * } catch (err) {\n * if (err.code === StorageError.ParentAppError) {\n * // handle parent app error\n * } else {\n * // handle other error types\n * }\n * }\n * ```\n *\n * @remarks\n * - The `StorageError` enum provides a convenient way to handle errors\n * encountered when using the `StorageKit` module. By checking the code\n * property of the caught error against the values of the enum, the error\n * type can be determined and appropriate action taken.\n *\n * - The `StorageError` enum is not intended to be instantiated or extended.\n */\nexport enum StorageError {\n /**\n * Indicates an error occurred in the parent app.\n */\n ParentAppError = 'parent_app_error',\n /**\n * Indicates a timeout occurred when communicating with the parent app.\n */\n ParentTimeoutError = 'parent_timeout_error',\n /**\n * Indicates an error occurred when reading from the storage.\n */\n ReadError = 'read_error',\n /**\n * Indicates an error occurred when writing to the storage.\n */\n WriteError = 'write_error',\n /**\n * Indicates an error occurred when removing from the storage.\n */\n RemoveError = 'remove_error',\n /**\n * Indicates an error occurred when clearing the storage.\n */\n ClearError = 'clear_error',\n}\n\n/**\n * @internal\n */\nexport const StorageErrorMessages = {\n [StorageError.ParentAppError]: (error: string) =>\n `Parent application error: ${error}`,\n [StorageError.ParentTimeoutError]: (error: string) =>\n `Parent application timeout: ${error}. Please check if the storage-kit is imported on the parent page.`,\n [StorageError.ReadError]: (error: string) => `Storage read error: ${error}`,\n [StorageError.WriteError]: (error: string) => `Storage write error: ${error}`,\n [StorageError.RemoveError]: (error: string) =>\n `Storage remove error: ${error}`,\n [StorageError.ClearError]: (error: string) => `Storage clear error: ${error}`,\n};\n","/**\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 private _localOnly: boolean = false;\n\n set localOnly(newValue: boolean) {\n this._localOnly = newValue;\n }\n\n get localOnly(): boolean {\n return this._localOnly;\n }\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 {\n MonterosaError,\n Unsubscribe,\n createError,\n getErrorMessage,\n} from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n ParentApplication,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n BridgeError,\n} from '@monterosa/sdk-launcher-kit';\n\nimport {\n StorageAction,\n ResponsePayload,\n StorageError,\n StorageErrorMessages,\n} from './types';\nimport { StorageImpl } from './storage_impl';\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 if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n try {\n let payload: Payload = {};\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n key: message.payload.key,\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n payload = {\n key: message.payload.key,\n };\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n payload = {\n key: message.payload.key,\n };\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 } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to handle storage message ${message.action} with error: ${errorMessage}`,\n );\n\n respondToSdkMessage(experience, message, {\n key: message.payload.key,\n error: errorMessage,\n });\n }\n });\n}\n\n/**\n * @internal\n */\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Read,\n payload?: Payload,\n): Promise<string | null>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Write | StorageAction.Remove | StorageAction.Clear,\n payload?: Payload,\n): Promise<undefined>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction,\n payload?: Payload,\n): Promise<string | null | undefined> {\n try {\n const response = await sendSdkRequest(parentApp, action, payload);\n\n const { value, error } = response.payload as ResponsePayload;\n\n if (error !== undefined) {\n throw createError(\n StorageError.ParentAppError,\n StorageErrorMessages,\n error,\n );\n }\n\n return value;\n } catch (err) {\n if (\n err instanceof MonterosaError &&\n err.code === BridgeError.RequestTimeoutError\n ) {\n const errorMessage = getErrorMessage(err);\n\n throw createError(\n StorageError.ParentTimeoutError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n throw err;\n }\n}\n\n/**\n * The `setStorageLocalOnly` function allows to control whether the SDK storage\n * should use only the local context storage, bypassing communication with the\n * parent application via the bridge.\n *\n * @remarks\n * - When set to `true`, all storage operations (`storageRead`, `storageWrite`,\n * `storageRemove`, `storageClear`) will use the local `StorageImpl` directly,\n * even when a parent application is detected\n *\n * - This is useful when the parent page runs an older version of the SDK that\n * does not include the storage-kit, which would otherwise cause a 20-second\n * timeout on every storage operation\n *\n * - By default, `localOnly` is set to `false`, meaning the bridge will be used\n * when a parent application is present\n *\n * @param localOnly - Determines whether or not SDK storage should bypass the\n * parent application bridge and use only local storage.\n */\nexport function setStorageLocalOnly(localOnly: boolean): void {\n storage.localOnly = localOnly;\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 try {\n if (parentApp !== null && !storage.localOnly) {\n value = await parentAppRequest(parentApp, StorageAction.Read, {\n key,\n });\n } else {\n value = storage.getItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to read storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.ReadError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n return 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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to write storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.WriteError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to remove storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.RemoveError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(`Unable to clear storage with error: ${errorMessage}`);\n\n throw createError(\n StorageError.ClearError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":[],"mappings":";;;AAAA;;;;;;;;AAQG;AAEH;;AAEG;AACH,IAAY,aAiBX,CAAA;AAjBD,CAAA,UAAY,aAAa,EAAA;AACvB;;AAEG;AACH,IAAA,aAAA,CAAA,MAAA,CAAA,GAAA,aAAoB,CAAA;AACpB;;AAEG;AACH,IAAA,aAAA,CAAA,OAAA,CAAA,GAAA,cAAsB,CAAA;AACtB;;AAEG;AACH,IAAA,aAAA,CAAA,QAAA,CAAA,GAAA,eAAwB,CAAA;AACxB;;AAEG;AACH,IAAA,aAAA,CAAA,OAAA,CAAA,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AAC;AACjB,SAAA;AACF,KAAA;AAAC,IAAA,OAAO,GAAG,EAAE;AACZ,QAAA,MAAM,YAAY,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;AAE1C,QAAA,OAAO,CAAC,KAAK,CAAC,uCAAuC,YAAY,CAAA,CAAE,CAAC,CAAC;QAErE,MAAM,WAAW,CACf,YAAY,CAAC,UAAU,EACvB,oBAAoB,EACpB,YAAY,CACb,CAAC;AACH,KAAA;AACH,CAAC;AAED,iBAAiB,CAAC,qBAAqB,CAAC;;;;"}
@@ -1,4 +1,4 @@
1
- import { setItem, clear, getItem, removeItem, checkAvailability, getErrorMessage, MonterosaError, createError } from '@monterosa/sdk-util';
1
+ import { getItem, setItem, removeItem, clear, checkAvailability, getErrorMessage, MonterosaError, createError } from '@monterosa/sdk-util';
2
2
  import { registerEmbedHook, onSdkMessage, respondToSdkMessage, BridgeError, sendSdkRequest, getParentApplication } from '@monterosa/sdk-launcher-kit';
3
3
 
4
4
  /*! *****************************************************************************
@@ -64,11 +64,26 @@ function __generator(thisArg, body) {
64
64
  * More details on the license can be found at https://www.monterosa.co/sdk/license
65
65
  */
66
66
  var _a;
67
+ /**
68
+ * Storage bridge action types used internally to communicate with the parent application.
69
+ */
67
70
  var StorageAction;
68
71
  (function (StorageAction) {
72
+ /**
73
+ * Read a value from storage.
74
+ */
69
75
  StorageAction["Read"] = "storageRead";
76
+ /**
77
+ * Write a value to storage.
78
+ */
70
79
  StorageAction["Write"] = "storageWrite";
80
+ /**
81
+ * Remove a value from storage.
82
+ */
71
83
  StorageAction["Remove"] = "storageRemove";
84
+ /**
85
+ * Clear all values from storage.
86
+ */
72
87
  StorageAction["Clear"] = "storageClear";
73
88
  })(StorageAction || (StorageAction = {}));
74
89
  /**
@@ -80,7 +95,7 @@ var StorageAction;
80
95
  * try {
81
96
  * // some code that uses the StorageKit
82
97
  * } catch (err) {
83
- * if (err.code === StorageError.ParentApp) {
98
+ * if (err.code === StorageError.ParentAppError) {
84
99
  * // handle parent app error
85
100
  * } else {
86
101
  * // handle other error types
@@ -155,7 +170,18 @@ var StorageImpl = /** @class */ (function () {
155
170
  this.memoryStore = {};
156
171
  this.accessible = checkAvailability();
157
172
  this._persistent = true;
173
+ this._localOnly = false;
158
174
  }
175
+ Object.defineProperty(StorageImpl.prototype, "localOnly", {
176
+ get: function () {
177
+ return this._localOnly;
178
+ },
179
+ set: function (newValue) {
180
+ this._localOnly = newValue;
181
+ },
182
+ enumerable: false,
183
+ configurable: true
184
+ });
159
185
  Object.defineProperty(StorageImpl.prototype, "persistent", {
160
186
  get: function () {
161
187
  return this._persistent;
@@ -297,6 +323,29 @@ function parentAppRequest(parentApp, action, payload) {
297
323
  });
298
324
  });
299
325
  }
326
+ /**
327
+ * The `setStorageLocalOnly` function allows to control whether the SDK storage
328
+ * should use only the local context storage, bypassing communication with the
329
+ * parent application via the bridge.
330
+ *
331
+ * @remarks
332
+ * - When set to `true`, all storage operations (`storageRead`, `storageWrite`,
333
+ * `storageRemove`, `storageClear`) will use the local `StorageImpl` directly,
334
+ * even when a parent application is detected
335
+ *
336
+ * - This is useful when the parent page runs an older version of the SDK that
337
+ * does not include the storage-kit, which would otherwise cause a 20-second
338
+ * timeout on every storage operation
339
+ *
340
+ * - By default, `localOnly` is set to `false`, meaning the bridge will be used
341
+ * when a parent application is present
342
+ *
343
+ * @param localOnly - Determines whether or not SDK storage should bypass the
344
+ * parent application bridge and use only local storage.
345
+ */
346
+ function setStorageLocalOnly(localOnly) {
347
+ storage.localOnly = localOnly;
348
+ }
300
349
  /**
301
350
  * The `setStoragePersistent` function is a simple function that allows to
302
351
  * control the persistence of the SDK storage. If the argument `persistent` is
@@ -346,7 +395,7 @@ function storageRead(key) {
346
395
  _a.label = 1;
347
396
  case 1:
348
397
  _a.trys.push([1, 5, , 6]);
349
- if (!(parentApp !== null)) return [3 /*break*/, 3];
398
+ if (!(parentApp !== null && !storage.localOnly)) return [3 /*break*/, 3];
350
399
  return [4 /*yield*/, parentAppRequest(parentApp, StorageAction.Read, {
351
400
  key: key,
352
401
  })];
@@ -390,7 +439,7 @@ function storageWrite(key, value) {
390
439
  _a.label = 1;
391
440
  case 1:
392
441
  _a.trys.push([1, 5, , 6]);
393
- if (!(parentApp !== null)) return [3 /*break*/, 3];
442
+ if (!(parentApp !== null && !storage.localOnly)) return [3 /*break*/, 3];
394
443
  return [4 /*yield*/, parentAppRequest(parentApp, StorageAction.Write, {
395
444
  key: key,
396
445
  value: value,
@@ -434,7 +483,7 @@ function storageRemove(key) {
434
483
  _a.label = 1;
435
484
  case 1:
436
485
  _a.trys.push([1, 5, , 6]);
437
- if (!(parentApp !== null)) return [3 /*break*/, 3];
486
+ if (!(parentApp !== null && !storage.localOnly)) return [3 /*break*/, 3];
438
487
  return [4 /*yield*/, parentAppRequest(parentApp, StorageAction.Remove, { key: key })];
439
488
  case 2:
440
489
  _a.sent();
@@ -473,7 +522,7 @@ function storageClear() {
473
522
  _a.label = 1;
474
523
  case 1:
475
524
  _a.trys.push([1, 5, , 6]);
476
- if (!(parentApp !== null)) return [3 /*break*/, 3];
525
+ if (!(parentApp !== null && !storage.localOnly)) return [3 /*break*/, 3];
477
526
  return [4 /*yield*/, parentAppRequest(parentApp, StorageAction.Clear)];
478
527
  case 2:
479
528
  _a.sent();
@@ -494,5 +543,5 @@ function storageClear() {
494
543
  }
495
544
  registerEmbedHook(listenStorageMessages);
496
545
 
497
- export { StorageError, listenStorageMessages, parentAppRequest, setStoragePersistent, storage, storageClear, storageRead, storageRemove, storageWrite };
546
+ export { StorageError, listenStorageMessages, parentAppRequest, setStorageLocalOnly, setStoragePersistent, storage, storageClear, storageRead, storageRemove, storageWrite };
498
547
  //# sourceMappingURL=index.esm5.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm5.js","sources":["../src/types.ts","../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * types.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\nexport enum StorageAction {\n Read = 'storageRead',\n Write = 'storageWrite',\n Remove = 'storageRemove',\n Clear = 'storageClear',\n}\n\nexport type ResponsePayload = {\n key: string;\n value?: string | null;\n error?: string;\n};\n\n/**\n * Defines a set of error codes that may be encountered when using the\n * Storage kit of the Monterosa SDK.\n *\n * @example\n * ```javascript\n * try {\n * // some code that uses the StorageKit\n * } catch (err) {\n * if (err.code === StorageError.ParentApp) {\n * // handle parent app error\n * } else {\n * // handle other error types\n * }\n * }\n * ```\n *\n * @remarks\n * - The `StorageError` enum provides a convenient way to handle errors\n * encountered when using the `StorageKit` module. By checking the code\n * property of the caught error against the values of the enum, the error\n * type can be determined and appropriate action taken.\n *\n * - The `StorageError` enum is not intended to be instantiated or extended.\n */\nexport enum StorageError {\n /**\n * Indicates an error occurred in the parent app.\n */\n ParentAppError = 'parent_app_error',\n /**\n * Indicates a timeout occurred when communicating with the parent app.\n */\n ParentTimeoutError = 'parent_timeout_error',\n /**\n * Indicates an error occurred when reading from the storage.\n */\n ReadError = 'read_error',\n /**\n * Indicates an error occurred when writing to the storage.\n */\n WriteError = 'write_error',\n /**\n * Indicates an error occurred when removing from the storage.\n */\n RemoveError = 'remove_error',\n /**\n * Indicates an error occurred when clearing the storage.\n */\n ClearError = 'clear_error',\n}\n\n/**\n * @internal\n */\nexport const StorageErrorMessages = {\n [StorageError.ParentAppError]: (error: string) =>\n `Parent application error: ${error}`,\n [StorageError.ParentTimeoutError]: (error: string) =>\n `Parent application timeout: ${error}. Please check if the storage-kit is imported on the parent page.`,\n [StorageError.ReadError]: (error: string) => `Storage read error: ${error}`,\n [StorageError.WriteError]: (error: string) => `Storage write error: ${error}`,\n [StorageError.RemoveError]: (error: string) =>\n `Storage remove error: ${error}`,\n [StorageError.ClearError]: (error: string) => `Storage clear error: ${error}`,\n};\n","/**\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 {\n MonterosaError,\n Unsubscribe,\n createError,\n getErrorMessage,\n} from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n ParentApplication,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n BridgeError,\n} from '@monterosa/sdk-launcher-kit';\n\nimport {\n StorageAction,\n ResponsePayload,\n StorageError,\n StorageErrorMessages,\n} from './types';\nimport { StorageImpl } from './storage_impl';\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 if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n try {\n let payload: Payload = {};\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n key: message.payload.key,\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n payload = {\n key: message.payload.key,\n };\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n payload = {\n key: message.payload.key,\n };\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 } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to handle storage message ${message.action} with error: ${errorMessage}`,\n );\n\n respondToSdkMessage(experience, message, {\n key: message.payload.key,\n error: errorMessage,\n });\n }\n });\n}\n\n/**\n * @internal\n */\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Read,\n payload?: Payload,\n): Promise<string | null>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Write | StorageAction.Remove | StorageAction.Clear,\n payload?: Payload,\n): Promise<undefined>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction,\n payload?: Payload,\n): Promise<string | null | undefined> {\n try {\n const response = await sendSdkRequest(parentApp, action, payload);\n\n const { value, error } = response.payload as ResponsePayload;\n\n if (error !== undefined) {\n throw createError(\n StorageError.ParentAppError,\n StorageErrorMessages,\n error,\n );\n }\n\n return value;\n } catch (err) {\n if (\n err instanceof MonterosaError &&\n err.code === BridgeError.RequestTimeoutError\n ) {\n const errorMessage = getErrorMessage(err);\n\n throw createError(\n StorageError.ParentTimeoutError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n throw err;\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 try {\n if (parentApp !== null) {\n value = await parentAppRequest(parentApp, StorageAction.Read, {\n key,\n });\n } else {\n value = storage.getItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to read storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.ReadError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n return 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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to write storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.WriteError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to remove storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.RemoveError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null) {\n await parentAppRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(`Unable to clear storage with error: ${errorMessage}`);\n\n throw createError(\n StorageError.ClearError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;AAUA,IAAY,aAKX;AALD,WAAY,aAAa;IACvB,qCAAoB,CAAA;IACpB,uCAAsB,CAAA;IACtB,yCAAwB,CAAA;IACxB,uCAAsB,CAAA;AACxB,CAAC,EALW,aAAa,KAAb,aAAa,QAKxB;AAQD;;;;;;;;;;;;;;;;;;;;;;;;;IAyBY;AAAZ,WAAY,YAAY;;;;IAItB,mDAAmC,CAA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AAC,EAAA;;oBAAhE,SAAgE,CAAC;;;oBAEjE,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;;;;;oBAGpB,YAAY,GAAG,eAAe,CAAC,KAAG,CAAC,CAAC;oBAE1C,OAAO,CAAC,KAAK,CACX,qCAAkC,GAAG,uBAAiB,YAAc,CACrE,CAAC;oBAEF,MAAM,WAAW,CACf,YAAY,CAAC,WAAW,EACxB,oBAAoB,EACpB,YAAY,CACb,CAAC;;;;;CAEL;AAED;;;;;;;;;;SAUsB,YAAY;;;;;;oBAC1B,SAAS,GAAG,oBAAoB,EAAE,CAAC;;;;0BAGnC,SAAS,KAAK,IAAI,CAAA,EAAlB,wBAAkB;oBACpB,qBAAM,gBAAgB,CAAC,SAAS,EAAE,aAAa,CAAC,KAAK,CAAC,EAAA;;oBAAtD,SAAsD,CAAC;;;oBAEvD,OAAO,CAAC,KAAK,EAAE,CAAC;;;;;oBAGZ,YAAY,GAAG,eAAe,CAAC,KAAG,CAAC,CAAC;oBAE1C,OAAO,CAAC,KAAK,CAAC,yCAAuC,YAAc,CAAC,CAAC;oBAErE,MAAM,WAAW,CACf,YAAY,CAAC,UAAU,EACvB,oBAAoB,EACpB,YAAY,CACb,CAAC;;;;;CAEL;AAED,iBAAiB,CAAC,qBAAqB,CAAC;;;;"}
1
+ {"version":3,"file":"index.esm5.js","sources":["../src/types.ts","../src/storage_impl.ts","../src/api.ts"],"sourcesContent":["/**\n * @license\n * types.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\n/**\n * Storage bridge action types used internally to communicate with the parent application.\n */\nexport enum StorageAction {\n /**\n * Read a value from storage.\n */\n Read = 'storageRead',\n /**\n * Write a value to storage.\n */\n Write = 'storageWrite',\n /**\n * Remove a value from storage.\n */\n Remove = 'storageRemove',\n /**\n * Clear all values from storage.\n */\n Clear = 'storageClear',\n}\n\n/**\n * Response payload returned from a storage bridge request.\n */\nexport type ResponsePayload = {\n /**\n * The storage key that was operated on.\n */\n key: string;\n /**\n * The value read from storage, or `null` if not found. Only present for read\n * operations.\n */\n value?: string | null;\n /**\n * Error message, if the operation failed.\n */\n error?: string;\n};\n\n/**\n * Defines a set of error codes that may be encountered when using the\n * Storage kit of the Monterosa SDK.\n *\n * @example\n * ```javascript\n * try {\n * // some code that uses the StorageKit\n * } catch (err) {\n * if (err.code === StorageError.ParentAppError) {\n * // handle parent app error\n * } else {\n * // handle other error types\n * }\n * }\n * ```\n *\n * @remarks\n * - The `StorageError` enum provides a convenient way to handle errors\n * encountered when using the `StorageKit` module. By checking the code\n * property of the caught error against the values of the enum, the error\n * type can be determined and appropriate action taken.\n *\n * - The `StorageError` enum is not intended to be instantiated or extended.\n */\nexport enum StorageError {\n /**\n * Indicates an error occurred in the parent app.\n */\n ParentAppError = 'parent_app_error',\n /**\n * Indicates a timeout occurred when communicating with the parent app.\n */\n ParentTimeoutError = 'parent_timeout_error',\n /**\n * Indicates an error occurred when reading from the storage.\n */\n ReadError = 'read_error',\n /**\n * Indicates an error occurred when writing to the storage.\n */\n WriteError = 'write_error',\n /**\n * Indicates an error occurred when removing from the storage.\n */\n RemoveError = 'remove_error',\n /**\n * Indicates an error occurred when clearing the storage.\n */\n ClearError = 'clear_error',\n}\n\n/**\n * @internal\n */\nexport const StorageErrorMessages = {\n [StorageError.ParentAppError]: (error: string) =>\n `Parent application error: ${error}`,\n [StorageError.ParentTimeoutError]: (error: string) =>\n `Parent application timeout: ${error}. Please check if the storage-kit is imported on the parent page.`,\n [StorageError.ReadError]: (error: string) => `Storage read error: ${error}`,\n [StorageError.WriteError]: (error: string) => `Storage write error: ${error}`,\n [StorageError.RemoveError]: (error: string) =>\n `Storage remove error: ${error}`,\n [StorageError.ClearError]: (error: string) => `Storage clear error: ${error}`,\n};\n","/**\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 private _localOnly: boolean = false;\n\n set localOnly(newValue: boolean) {\n this._localOnly = newValue;\n }\n\n get localOnly(): boolean {\n return this._localOnly;\n }\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 {\n MonterosaError,\n Unsubscribe,\n createError,\n getErrorMessage,\n} from '@monterosa/sdk-util';\nimport {\n Experience,\n Payload,\n ParentApplication,\n registerEmbedHook,\n getParentApplication,\n sendSdkRequest,\n respondToSdkMessage,\n onSdkMessage,\n BridgeError,\n} from '@monterosa/sdk-launcher-kit';\n\nimport {\n StorageAction,\n ResponsePayload,\n StorageError,\n StorageErrorMessages,\n} from './types';\nimport { StorageImpl } from './storage_impl';\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 if (\n !Object.values(StorageAction).includes(message.action as StorageAction)\n ) {\n return;\n }\n\n try {\n let payload: Payload = {};\n\n switch (message.action) {\n case StorageAction.Read:\n payload = {\n key: message.payload.key,\n value: storage.getItem(message.payload.key),\n };\n break;\n case StorageAction.Write:\n payload = {\n key: message.payload.key,\n };\n storage.setItem(message.payload.key, message.payload.value);\n break;\n case StorageAction.Remove:\n payload = {\n key: message.payload.key,\n };\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 } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to handle storage message ${message.action} with error: ${errorMessage}`,\n );\n\n respondToSdkMessage(experience, message, {\n key: message.payload.key,\n error: errorMessage,\n });\n }\n });\n}\n\n/**\n * @internal\n */\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Read,\n payload?: Payload,\n): Promise<string | null>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction.Write | StorageAction.Remove | StorageAction.Clear,\n payload?: Payload,\n): Promise<undefined>;\n\nexport async function parentAppRequest(\n parentApp: ParentApplication,\n action: StorageAction,\n payload?: Payload,\n): Promise<string | null | undefined> {\n try {\n const response = await sendSdkRequest(parentApp, action, payload);\n\n const { value, error } = response.payload as ResponsePayload;\n\n if (error !== undefined) {\n throw createError(\n StorageError.ParentAppError,\n StorageErrorMessages,\n error,\n );\n }\n\n return value;\n } catch (err) {\n if (\n err instanceof MonterosaError &&\n err.code === BridgeError.RequestTimeoutError\n ) {\n const errorMessage = getErrorMessage(err);\n\n throw createError(\n StorageError.ParentTimeoutError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n throw err;\n }\n}\n\n/**\n * The `setStorageLocalOnly` function allows to control whether the SDK storage\n * should use only the local context storage, bypassing communication with the\n * parent application via the bridge.\n *\n * @remarks\n * - When set to `true`, all storage operations (`storageRead`, `storageWrite`,\n * `storageRemove`, `storageClear`) will use the local `StorageImpl` directly,\n * even when a parent application is detected\n *\n * - This is useful when the parent page runs an older version of the SDK that\n * does not include the storage-kit, which would otherwise cause a 20-second\n * timeout on every storage operation\n *\n * - By default, `localOnly` is set to `false`, meaning the bridge will be used\n * when a parent application is present\n *\n * @param localOnly - Determines whether or not SDK storage should bypass the\n * parent application bridge and use only local storage.\n */\nexport function setStorageLocalOnly(localOnly: boolean): void {\n storage.localOnly = localOnly;\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 try {\n if (parentApp !== null && !storage.localOnly) {\n value = await parentAppRequest(parentApp, StorageAction.Read, {\n key,\n });\n } else {\n value = storage.getItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to read storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.ReadError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n\n return 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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Write, {\n key,\n value,\n });\n } else {\n storage.setItem(key, value);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to write storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.WriteError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Remove, { key });\n } else {\n storage.removeItem(key);\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(\n `Unable to remove storage item \"${key}\" with error: ${errorMessage}`,\n );\n\n throw createError(\n StorageError.RemoveError,\n StorageErrorMessages,\n errorMessage,\n );\n }\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 try {\n if (parentApp !== null && !storage.localOnly) {\n await parentAppRequest(parentApp, StorageAction.Clear);\n } else {\n storage.clear();\n }\n } catch (err) {\n const errorMessage = getErrorMessage(err);\n\n console.error(`Unable to clear storage with error: ${errorMessage}`);\n\n throw createError(\n StorageError.ClearError,\n StorageErrorMessages,\n errorMessage,\n );\n }\n}\n\nregisterEmbedHook(listenStorageMessages);\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;AAQG;;AAEH;;AAEG;AACH,IAAY,aAiBX,CAAA;AAjBD,CAAA,UAAY,aAAa,EAAA;AACvB;;AAEG;AACH,IAAA,aAAA,CAAA,MAAA,CAAA,GAAA,aAAoB,CAAA;AACpB;;AAEG;AACH,IAAA,aAAA,CAAA,OAAA,CAAA,GAAA,cAAsB,CAAA;AACtB;;AAEG;AACH,IAAA,aAAA,CAAA,QAAA,CAAA,GAAA,eAAwB,CAAA;AACxB;;AAEG;AACH,IAAA,aAAA,CAAA,OAAA,CAAA,GAAA,cAAsB,CAAA;AACxB,CAAC,EAjBW,aAAa,KAAb,aAAa,GAiBxB,EAAA,CAAA,CAAA,CAAA;AAqBD;;;;;;;;;;;;;;;;;;;;;;;;AAwBG;IACS,aAyBX;AAzBD,CAAA,UAAY,YAAY,EAAA;AACtB;;AAEG;AACH,IAAA,YAAA,CAAA,gBAAA,CAAA,GAAA,kBAAmC,CAAA;AACnC;;AAEG;AACH,IAAA,YAAA,CAAA,oBAAA,CAAA,GAAA,sBAA2C,CAAA;AAC3C;;AAEG;AACH,IAAA,YAAA,CAAA,WAAA,CAAA,GAAA,YAAwB,CAAA;AACxB;;AAEG;AACH,IAAA,YAAA,CAAA,YAAA,CAAA,GAAA,aAA0B,CAAA;AAC1B;;AAEG;AACH,IAAA,YAAA,CAAA,aAAA,CAAA,GAAA,cAA4B,CAAA;AAC5B;;AAEG;AACH,IAAA,YAAA,CAAA,YAAA,CAAA,GAAA,aAA0B,CAAA;AAC5B,C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AAC;;;;;AAEL,CAAA;AAED;;;;;;;;;AASG;SACmB,YAAY,GAAA;;;;;;oBAC1B,SAAS,GAAG,oBAAoB,EAAE,CAAC;;;;0BAGnC,SAAS,KAAK,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,CAAA,EAAxC,OAAwC,CAAA,CAAA,YAAA,CAAA,CAAA,CAAA;oBAC1C,OAAM,CAAA,CAAA,YAAA,gBAAgB,CAAC,SAAS,EAAE,aAAa,CAAC,KAAK,CAAC,CAAA,CAAA;;AAAtD,oBAAA,EAAA,CAAA,IAAA,EAAsD,CAAC;;;oBAEvD,OAAO,CAAC,KAAK,EAAE,CAAC;;;;;AAGZ,oBAAA,YAAY,GAAG,eAAe,CAAC,KAAG,CAAC,CAAC;AAE1C,oBAAA,OAAO,CAAC,KAAK,CAAC,sCAAuC,GAAA,YAAc,CAAC,CAAC;oBAErE,MAAM,WAAW,CACf,YAAY,CAAC,UAAU,EACvB,oBAAoB,EACpB,YAAY,CACb,CAAC;;;;;AAEL,CAAA;AAED,iBAAiB,CAAC,qBAAqB,CAAC;;;;"}
@@ -0,0 +1,213 @@
1
+ /**
2
+ * Persists key-value data across sessions via the parent application.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ import { Experience } from '@monterosa/sdk-launcher-kit';
8
+ import { ParentApplication } from '@monterosa/sdk-launcher-kit';
9
+ import { Payload } from '@monterosa/sdk-launcher-kit';
10
+ import { Unsubscribe } from '@monterosa/sdk-util';
11
+
12
+ /**
13
+ * @internal
14
+ */
15
+ export declare function listenStorageMessages(experience: Experience): Unsubscribe;
16
+
17
+ /**
18
+ * @internal
19
+ */
20
+ export declare function parentAppRequest(parentApp: ParentApplication, action: StorageAction.Read, payload?: Payload): Promise<string | null>;
21
+
22
+ export declare function parentAppRequest(parentApp: ParentApplication, action: StorageAction.Write | StorageAction.Remove | StorageAction.Clear, payload?: Payload): Promise<undefined>;
23
+
24
+ /**
25
+ * The `setStoragePersistent` function is a simple function that allows to
26
+ * control the persistence of the SDK storage. If the argument `persistent` is
27
+ * set to `true`, then the storage will be persistent across browser sessions
28
+ * and if it is set to `false`, then the storage will save to memory.
29
+ * It is important to note that the use of persistent storage may be subject
30
+ * to laws and regulations, such as those related to data privacy and protection.
31
+ *
32
+ * @remarks
33
+ * - We transition from persistent to memory and memory to persistent in
34
+ * a seamless manner for you
35
+ *
36
+ * - By default we store in memory
37
+ *
38
+ * - The value of storage persistent persists across session (aka put it to true,
39
+ * it will remain so, put it back to false, it will remain so)
40
+ *
41
+ * - You have the responsibility to comply with any laws and regulations, and
42
+ * store only the data if you know it's valid to do so
43
+ *
44
+ * @param persistent - Determines whether or not SDK storage should persist
45
+ * across browser sessions.
46
+ */
47
+ export declare function setStoragePersistent(persistent: boolean): void;
48
+
49
+ /**
50
+ * @internal
51
+ */
52
+ export declare const storage: StorageImpl;
53
+
54
+ /**
55
+ * @license
56
+ * types.ts
57
+ * storage-kit
58
+ *
59
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
60
+ *
61
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
62
+ */
63
+ /**
64
+ * Storage bridge action types used internally to communicate with the parent application.
65
+ */
66
+ declare enum StorageAction {
67
+ /**
68
+ * Read a value from storage.
69
+ */
70
+ Read = "storageRead",
71
+ /**
72
+ * Write a value to storage.
73
+ */
74
+ Write = "storageWrite",
75
+ /**
76
+ * Remove a value from storage.
77
+ */
78
+ Remove = "storageRemove",
79
+ /**
80
+ * Clear all values from storage.
81
+ */
82
+ Clear = "storageClear"
83
+ }
84
+
85
+ /**
86
+ * The function allows to clear all data from the SDK storage.
87
+ *
88
+ * @throws
89
+ * The function throws an error if there is a timeout clearing the parent's
90
+ * application storage.
91
+ *
92
+ * @returns A promise that resolves to `void` once the data has been
93
+ * successfully cleared.
94
+ */
95
+ export declare function storageClear(): Promise<void>;
96
+
97
+ /**
98
+ * Defines a set of error codes that may be encountered when using the
99
+ * Storage kit of the Monterosa SDK.
100
+ *
101
+ * @example
102
+ * ```javascript
103
+ * try {
104
+ * // some code that uses the StorageKit
105
+ * } catch (err) {
106
+ * if (err.code === StorageError.ParentAppError) {
107
+ * // handle parent app error
108
+ * } else {
109
+ * // handle other error types
110
+ * }
111
+ * }
112
+ * ```
113
+ *
114
+ * @remarks
115
+ * - The `StorageError` enum provides a convenient way to handle errors
116
+ * encountered when using the `StorageKit` module. By checking the code
117
+ * property of the caught error against the values of the enum, the error
118
+ * type can be determined and appropriate action taken.
119
+ *
120
+ * - The `StorageError` enum is not intended to be instantiated or extended.
121
+ */
122
+ export declare enum StorageError {
123
+ /**
124
+ * Indicates an error occurred in the parent app.
125
+ */
126
+ ParentAppError = "parent_app_error",
127
+ /**
128
+ * Indicates a timeout occurred when communicating with the parent app.
129
+ */
130
+ ParentTimeoutError = "parent_timeout_error",
131
+ /**
132
+ * Indicates an error occurred when reading from the storage.
133
+ */
134
+ ReadError = "read_error",
135
+ /**
136
+ * Indicates an error occurred when writing to the storage.
137
+ */
138
+ WriteError = "write_error",
139
+ /**
140
+ * Indicates an error occurred when removing from the storage.
141
+ */
142
+ RemoveError = "remove_error",
143
+ /**
144
+ * Indicates an error occurred when clearing the storage.
145
+ */
146
+ ClearError = "clear_error"
147
+ }
148
+
149
+ /**
150
+ * @license
151
+ * storage_impl.ts
152
+ * storage-kit
153
+ *
154
+ * Copyright © 2025 Monterosa Productions Limited. All rights reserved.
155
+ *
156
+ * More details on the license can be found at https://www.monterosa.co/sdk/license
157
+ */
158
+ declare class StorageImpl {
159
+ private memoryStore;
160
+ private accessible;
161
+ private _persistent;
162
+ set persistent(newValue: boolean);
163
+ get persistent(): boolean;
164
+ getItem(key: string): string | null;
165
+ setItem(key: string, value: string): void;
166
+ removeItem(key: string): void;
167
+ clear(): void;
168
+ }
169
+
170
+ /**
171
+ * The function allows to read data from the SDK storage.
172
+ *
173
+ * @param key - The name of the item to be read from storage.
174
+ *
175
+ * @throws
176
+ * The function throws an error if there is a timeout reading the data
177
+ * from the parent application storage.
178
+ *
179
+ * @returns A promise that resolves to the value
180
+ * of the item in storage or `null` if the item doesn't exist.
181
+ */
182
+ export declare function storageRead(key: string): Promise<string | null>;
183
+
184
+ /**
185
+ * The function allows to remove an item from the SDK storage.
186
+ *
187
+ * @param key - A name of the item to be removed.
188
+ *
189
+ * @throws
190
+ * The function throws an error if there is a timeout removing the data
191
+ * from the parent's application storage.
192
+ *
193
+ * @returns A promise that resolves to `void` once the data has
194
+ * been successfully removed.
195
+ */
196
+ export declare function storageRemove(key: string): Promise<void>;
197
+
198
+ /**
199
+ * The function allows to write data to the SDK storage.
200
+ *
201
+ * @param key - A name of the item to be stored.
202
+ * @param value - A value to be stored.
203
+ *
204
+ * @throws
205
+ * The function throws an error if there is a timeout writing the data
206
+ * to the parent's application storage.
207
+ *
208
+ * @returns A promise that resolves to `void` once the data has
209
+ * been successfully stored.
210
+ */
211
+ export declare function storageWrite(key: string, value: string): Promise<void>;
212
+
213
+ export { }
@@ -11,6 +11,9 @@ export declare class StorageImpl {
11
11
  private memoryStore;
12
12
  private accessible;
13
13
  private _persistent;
14
+ private _localOnly;
15
+ set localOnly(newValue: boolean);
16
+ get localOnly(): boolean;
14
17
  set persistent(newValue: boolean);
15
18
  get persistent(): boolean;
16
19
  getItem(key: string): string | null;
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.23.0"
9
+ }
10
+ ]
11
+ }
package/dist/types.d.ts CHANGED
@@ -7,15 +7,43 @@
7
7
  *
8
8
  * More details on the license can be found at https://www.monterosa.co/sdk/license
9
9
  */
10
+ /**
11
+ * Storage bridge action types used internally to communicate with the parent application.
12
+ */
10
13
  export declare enum StorageAction {
14
+ /**
15
+ * Read a value from storage.
16
+ */
11
17
  Read = "storageRead",
18
+ /**
19
+ * Write a value to storage.
20
+ */
12
21
  Write = "storageWrite",
22
+ /**
23
+ * Remove a value from storage.
24
+ */
13
25
  Remove = "storageRemove",
26
+ /**
27
+ * Clear all values from storage.
28
+ */
14
29
  Clear = "storageClear"
15
30
  }
31
+ /**
32
+ * Response payload returned from a storage bridge request.
33
+ */
16
34
  export declare type ResponsePayload = {
35
+ /**
36
+ * The storage key that was operated on.
37
+ */
17
38
  key: string;
39
+ /**
40
+ * The value read from storage, or `null` if not found. Only present for read
41
+ * operations.
42
+ */
18
43
  value?: string | null;
44
+ /**
45
+ * Error message, if the operation failed.
46
+ */
19
47
  error?: string;
20
48
  };
21
49
  /**
@@ -27,7 +55,7 @@ export declare type ResponsePayload = {
27
55
  * try {
28
56
  * // some code that uses the StorageKit
29
57
  * } catch (err) {
30
- * if (err.code === StorageError.ParentApp) {
58
+ * if (err.code === StorageError.ParentAppError) {
31
59
  * // handle parent app error
32
60
  * } else {
33
61
  * // handle other error types
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monterosa/sdk-storage-kit",
3
- "version": "0.18.9",
3
+ "version": "0.18.10-local-only-storage.2",
4
4
  "description": "Storage Kit for the Monterosa JS SDK",
5
5
  "author": "Monterosa <hello@monterosa.co.uk> (https://www.monterosa.co/)",
6
6
  "main": "dist/index.cjs.js",
@@ -27,9 +27,9 @@
27
27
  "@monterosa/sdk-util": "0.x"
28
28
  },
29
29
  "dependencies": {
30
- "@monterosa/sdk-core": "^0.18.9",
31
- "@monterosa/sdk-launcher-kit": "^0.18.9",
32
- "@monterosa/sdk-util": "^0.18.9"
30
+ "@monterosa/sdk-core": "^0.18.10-local-only-storage.2",
31
+ "@monterosa/sdk-launcher-kit": "^0.18.10-local-only-storage.2",
32
+ "@monterosa/sdk-util": "^0.18.10-local-only-storage.2"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@rollup/plugin-json": "^4.1.0",
@@ -45,5 +45,5 @@
45
45
  "publishConfig": {
46
46
  "access": "public"
47
47
  },
48
- "gitHead": "cf8b4ea9ed5e7b9ab5b64456a07c2a78c2b568c0"
48
+ "gitHead": "e8c9eed1871359b198d7ed25b0af9ee518d376e9"
49
49
  }