@dynamic-labs/utils 3.0.0-alpha.9 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +628 -0
  2. package/_virtual/_tslib.cjs +15 -0
  3. package/_virtual/_tslib.js +14 -1
  4. package/package.json +5 -4
  5. package/src/cloneObjectWithOverrides/cloneObjectWithOverrides.cjs +37 -0
  6. package/src/cloneObjectWithOverrides/cloneObjectWithOverrides.d.ts +11 -0
  7. package/src/cloneObjectWithOverrides/cloneObjectWithOverrides.js +33 -0
  8. package/src/cloneObjectWithOverrides/index.d.ts +1 -0
  9. package/src/errors/MfaInvalidOtpError.cjs +1 -1
  10. package/src/errors/MfaInvalidOtpError.js +1 -1
  11. package/src/errors/MfaRateLimitedError.cjs +14 -0
  12. package/src/errors/MfaRateLimitedError.d.ts +4 -0
  13. package/src/errors/MfaRateLimitedError.js +10 -0
  14. package/src/errors/NoAccessError.cjs +3 -1
  15. package/src/errors/NoAccessError.d.ts +5 -1
  16. package/src/errors/NoAccessError.js +3 -1
  17. package/src/errors/SandboxMaximumThresholdReachedError.cjs +15 -0
  18. package/src/errors/SandboxMaximumThresholdReachedError.d.ts +5 -0
  19. package/src/errors/SandboxMaximumThresholdReachedError.js +11 -0
  20. package/src/errors/UserRejectedRequestError.cjs +14 -0
  21. package/src/errors/UserRejectedRequestError.d.ts +4 -0
  22. package/src/errors/UserRejectedRequestError.js +10 -0
  23. package/src/errors/WalletAddressMismatchError.cjs +17 -0
  24. package/src/errors/WalletAddressMismatchError.d.ts +11 -0
  25. package/src/errors/WalletAddressMismatchError.js +13 -0
  26. package/src/errors/index.d.ts +4 -0
  27. package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.cjs +0 -6
  28. package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.js +0 -6
  29. package/src/index.cjs +21 -0
  30. package/src/index.d.ts +4 -0
  31. package/src/index.js +11 -1
  32. package/src/isMobile.cjs +11 -0
  33. package/src/isMobile.d.ts +1 -0
  34. package/src/isMobile.js +11 -1
  35. package/src/nativeMobileOauthStateParam.cjs +13 -0
  36. package/src/nativeMobileOauthStateParam.d.ts +14 -0
  37. package/src/nativeMobileOauthStateParam.js +9 -0
  38. package/src/retryableFn.cjs +1 -5
  39. package/src/retryableFn.js +1 -5
  40. package/src/services/FetchService/FetchService.cjs +10 -5
  41. package/src/services/FetchService/FetchService.d.ts +2 -2
  42. package/src/services/FetchService/FetchService.js +10 -5
  43. package/src/services/Oauth2Service/Oauth2Service.cjs +38 -0
  44. package/src/services/Oauth2Service/Oauth2Service.d.ts +41 -0
  45. package/src/services/Oauth2Service/Oauth2Service.js +34 -0
  46. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.cjs +176 -0
  47. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.d.ts +2 -0
  48. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.js +172 -0
  49. package/src/services/Oauth2Service/createWindowOauth2Service/index.d.ts +1 -0
  50. package/src/services/Oauth2Service/index.d.ts +2 -0
  51. package/src/services/Oauth2Service/utils/connectWithAppleId/connectWithAppleId.cjs +26 -0
  52. package/src/services/Oauth2Service/utils/connectWithAppleId/connectWithAppleId.d.ts +7 -0
  53. package/src/services/Oauth2Service/utils/connectWithAppleId/connectWithAppleId.js +22 -0
  54. package/src/services/Oauth2Service/utils/connectWithAppleId/index.d.ts +1 -0
  55. package/src/services/Oauth2Service/utils/loadAppleId/index.d.ts +1 -0
  56. package/src/services/Oauth2Service/utils/loadAppleId/loadAppleId.cjs +34 -0
  57. package/src/services/Oauth2Service/utils/loadAppleId/loadAppleId.d.ts +1 -0
  58. package/src/services/Oauth2Service/utils/loadAppleId/loadAppleId.js +30 -0
  59. package/src/services/PlatformService/PlatformService.cjs +46 -13
  60. package/src/services/PlatformService/PlatformService.d.ts +31 -3
  61. package/src/services/PlatformService/PlatformService.js +46 -13
  62. package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.cjs +2 -3
  63. package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.js +2 -3
  64. package/src/services/PlatformService/types.d.ts +11 -4
  65. package/src/services/StorageService/StorageService.cjs +59 -0
  66. package/src/services/StorageService/StorageService.d.ts +30 -0
  67. package/src/services/StorageService/StorageService.js +51 -0
  68. package/src/services/StorageService/createStorageService/createStorageService.cjs +46 -0
  69. package/src/services/StorageService/createStorageService/createStorageService.d.ts +7 -0
  70. package/src/services/StorageService/createStorageService/createStorageService.js +42 -0
  71. package/src/services/StorageService/createStorageService/index.d.ts +1 -0
  72. package/src/services/StorageService/index.d.ts +3 -0
  73. package/src/services/StorageService/types.d.ts +6 -0
@@ -0,0 +1,22 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../../../_virtual/_tslib.js';
3
+ import { loadAppleId } from '../loadAppleId/loadAppleId.js';
4
+
5
+ const connectWithAppleId = (_a) => __awaiter(void 0, [_a], void 0, function* ({ clientId, oauthLoginUrl, state, }) {
6
+ yield loadAppleId();
7
+ // Because the AppleID is loaded from a script tag, there is not type for it
8
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
9
+ // @ts-ignore
10
+ AppleID.auth.init({
11
+ clientId,
12
+ redirectURI: oauthLoginUrl.searchParams.get('redirect_uri'),
13
+ scope: 'name email',
14
+ state,
15
+ usePopup: false,
16
+ });
17
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
18
+ // @ts-ignore
19
+ AppleID.auth.signIn();
20
+ });
21
+
22
+ export { connectWithAppleId };
@@ -0,0 +1 @@
1
+ export { connectWithAppleId } from './connectWithAppleId';
@@ -0,0 +1 @@
1
+ export { loadAppleId } from './loadAppleId';
@@ -0,0 +1,34 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ const appleIdScriptSrc = 'https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js';
7
+ const loadAppleId = () => new Promise((resolve, reject) => {
8
+ const script = document.querySelector('script[data-apple-sdk]');
9
+ if (script) {
10
+ // Script already exists, attach event listeners
11
+ if (script.hasAttribute('data-loaded')) {
12
+ resolve();
13
+ }
14
+ else {
15
+ script.addEventListener('load', () => resolve());
16
+ script.addEventListener('error', () => reject(new Error('Failed to load Apple SDK')));
17
+ }
18
+ }
19
+ else {
20
+ // If the script does not exist, create and add it
21
+ const script = document.createElement('script');
22
+ script.type = 'text/javascript';
23
+ script.src = appleIdScriptSrc;
24
+ script.setAttribute('data-apple-sdk', 'true');
25
+ script.onload = () => {
26
+ script === null || script === void 0 ? void 0 : script.setAttribute('data-loaded', 'true');
27
+ resolve();
28
+ };
29
+ script.onerror = () => reject(new Error('Failed to load Apple SDK'));
30
+ window.document.head.appendChild(script);
31
+ }
32
+ });
33
+
34
+ exports.loadAppleId = loadAppleId;
@@ -0,0 +1 @@
1
+ export declare const loadAppleId: () => Promise<void>;
@@ -0,0 +1,30 @@
1
+ 'use client'
2
+ const appleIdScriptSrc = 'https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js';
3
+ const loadAppleId = () => new Promise((resolve, reject) => {
4
+ const script = document.querySelector('script[data-apple-sdk]');
5
+ if (script) {
6
+ // Script already exists, attach event listeners
7
+ if (script.hasAttribute('data-loaded')) {
8
+ resolve();
9
+ }
10
+ else {
11
+ script.addEventListener('load', () => resolve());
12
+ script.addEventListener('error', () => reject(new Error('Failed to load Apple SDK')));
13
+ }
14
+ }
15
+ else {
16
+ // If the script does not exist, create and add it
17
+ const script = document.createElement('script');
18
+ script.type = 'text/javascript';
19
+ script.src = appleIdScriptSrc;
20
+ script.setAttribute('data-apple-sdk', 'true');
21
+ script.onload = () => {
22
+ script === null || script === void 0 ? void 0 : script.setAttribute('data-loaded', 'true');
23
+ resolve();
24
+ };
25
+ script.onerror = () => reject(new Error('Failed to load Apple SDK'));
26
+ window.document.head.appendChild(script);
27
+ }
28
+ });
29
+
30
+ export { loadAppleId };
@@ -3,37 +3,70 @@
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
6
7
  var createBrowserPlatformService = require('./createBrowserPlatformService/createBrowserPlatformService.cjs');
7
8
 
9
+ var _a, _PlatformService_implementation;
8
10
  class PlatformService {
9
11
  static get implementation() {
10
- if (!PlatformService._implementation) {
12
+ if (!_tslib.__classPrivateFieldGet(_a, _a, "f", _PlatformService_implementation)) {
11
13
  return createBrowserPlatformService.createBrowserPlatformService(window);
12
14
  }
13
- return PlatformService._implementation;
15
+ return _tslib.__classPrivateFieldGet(_a, _a, "f", _PlatformService_implementation);
14
16
  }
15
- static setImplementation(implementation) {
16
- PlatformService._implementation = implementation;
17
+ static set implementation(implementation) {
18
+ _tslib.__classPrivateFieldSet(_a, _a, implementation, "f", _PlatformService_implementation);
17
19
  }
20
+ /**
21
+ * Indicates if the current platform is a native mobile app
22
+ * like react-native or flutter.
23
+ */
24
+ static get isNativeMobile() {
25
+ return _a.implementation.isNativeMobile;
26
+ }
27
+ /**
28
+ * Gets the origin of the current location.
29
+ *
30
+ * @example window.location.origin
31
+ */
18
32
  static get getOrigin() {
19
- return PlatformService.implementation.getOrigin;
33
+ return _a.implementation.getOrigin;
34
+ }
35
+ /**
36
+ * Gets the origin to be displayed in the UI.
37
+ *
38
+ * @example window.location.origin
39
+ */
40
+ static get getDisplayOrigin() {
41
+ return _a.implementation.getDisplayOrigin;
20
42
  }
43
+ /**
44
+ * Gets the host of the current location.
45
+ *
46
+ * @example window.location.host
47
+ */
21
48
  static get getHost() {
22
- return PlatformService.implementation.getHost;
49
+ return _a.implementation.getHost;
23
50
  }
51
+ /**
52
+ * Gets the hostname of the current location.
53
+ *
54
+ * @example window.location.hostname
55
+ */
24
56
  static get getHostname() {
25
- return PlatformService.implementation.getHostname;
57
+ return _a.implementation.getHostname;
26
58
  }
27
59
  static get getTLD() {
28
- return PlatformService.implementation.getTLD;
60
+ return _a.implementation.getTLD;
29
61
  }
30
- // Deeplink handling
62
+ /**
63
+ * Opens a URL. If possible, should avoid new windows.
64
+ */
31
65
  static get openURL() {
32
- return PlatformService.implementation.openURL;
33
- }
34
- static get openNewWindow() {
35
- return PlatformService.implementation.openNewWindow;
66
+ return _a.implementation.openURL;
36
67
  }
37
68
  }
69
+ _a = PlatformService;
70
+ _PlatformService_implementation = { value: void 0 };
38
71
 
39
72
  exports.PlatformService = PlatformService;
@@ -1,12 +1,40 @@
1
1
  import { IPlatformService } from './types';
2
2
  export declare class PlatformService {
3
- static _implementation: IPlatformService;
3
+ #private;
4
4
  static get implementation(): IPlatformService;
5
- static setImplementation(implementation: IPlatformService): void;
5
+ static set implementation(implementation: IPlatformService);
6
+ /**
7
+ * Indicates if the current platform is a native mobile app
8
+ * like react-native or flutter.
9
+ */
10
+ static get isNativeMobile(): boolean;
11
+ /**
12
+ * Gets the origin of the current location.
13
+ *
14
+ * @example window.location.origin
15
+ */
6
16
  static get getOrigin(): () => string;
17
+ /**
18
+ * Gets the origin to be displayed in the UI.
19
+ *
20
+ * @example window.location.origin
21
+ */
22
+ static get getDisplayOrigin(): () => string | undefined;
23
+ /**
24
+ * Gets the host of the current location.
25
+ *
26
+ * @example window.location.host
27
+ */
7
28
  static get getHost(): () => string;
29
+ /**
30
+ * Gets the hostname of the current location.
31
+ *
32
+ * @example window.location.hostname
33
+ */
8
34
  static get getHostname(): () => string;
9
35
  static get getTLD(): () => string | undefined;
36
+ /**
37
+ * Opens a URL. If possible, should avoid new windows.
38
+ */
10
39
  static get openURL(): (url: string) => Promise<void>;
11
- static get openNewWindow(): (url: string) => Promise<void>;
12
40
  }
@@ -1,35 +1,68 @@
1
1
  'use client'
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from '../../../_virtual/_tslib.js';
2
3
  import { createBrowserPlatformService } from './createBrowserPlatformService/createBrowserPlatformService.js';
3
4
 
5
+ var _a, _PlatformService_implementation;
4
6
  class PlatformService {
5
7
  static get implementation() {
6
- if (!PlatformService._implementation) {
8
+ if (!__classPrivateFieldGet(_a, _a, "f", _PlatformService_implementation)) {
7
9
  return createBrowserPlatformService(window);
8
10
  }
9
- return PlatformService._implementation;
11
+ return __classPrivateFieldGet(_a, _a, "f", _PlatformService_implementation);
10
12
  }
11
- static setImplementation(implementation) {
12
- PlatformService._implementation = implementation;
13
+ static set implementation(implementation) {
14
+ __classPrivateFieldSet(_a, _a, implementation, "f", _PlatformService_implementation);
13
15
  }
16
+ /**
17
+ * Indicates if the current platform is a native mobile app
18
+ * like react-native or flutter.
19
+ */
20
+ static get isNativeMobile() {
21
+ return _a.implementation.isNativeMobile;
22
+ }
23
+ /**
24
+ * Gets the origin of the current location.
25
+ *
26
+ * @example window.location.origin
27
+ */
14
28
  static get getOrigin() {
15
- return PlatformService.implementation.getOrigin;
29
+ return _a.implementation.getOrigin;
30
+ }
31
+ /**
32
+ * Gets the origin to be displayed in the UI.
33
+ *
34
+ * @example window.location.origin
35
+ */
36
+ static get getDisplayOrigin() {
37
+ return _a.implementation.getDisplayOrigin;
16
38
  }
39
+ /**
40
+ * Gets the host of the current location.
41
+ *
42
+ * @example window.location.host
43
+ */
17
44
  static get getHost() {
18
- return PlatformService.implementation.getHost;
45
+ return _a.implementation.getHost;
19
46
  }
47
+ /**
48
+ * Gets the hostname of the current location.
49
+ *
50
+ * @example window.location.hostname
51
+ */
20
52
  static get getHostname() {
21
- return PlatformService.implementation.getHostname;
53
+ return _a.implementation.getHostname;
22
54
  }
23
55
  static get getTLD() {
24
- return PlatformService.implementation.getTLD;
56
+ return _a.implementation.getTLD;
25
57
  }
26
- // Deeplink handling
58
+ /**
59
+ * Opens a URL. If possible, should avoid new windows.
60
+ */
27
61
  static get openURL() {
28
- return PlatformService.implementation.openURL;
29
- }
30
- static get openNewWindow() {
31
- return PlatformService.implementation.openNewWindow;
62
+ return _a.implementation.openURL;
32
63
  }
33
64
  }
65
+ _a = PlatformService;
66
+ _PlatformService_implementation = { value: void 0 };
34
67
 
35
68
  export { PlatformService };
@@ -7,6 +7,7 @@ var _tslib = require('../../../../_virtual/_tslib.cjs');
7
7
  var tldts = require('tldts');
8
8
 
9
9
  const createBrowserPlatformService = (window) => ({
10
+ getDisplayOrigin: () => window.location.origin,
10
11
  getHost: () => window.location.host,
11
12
  getHostname: () => window.location.hostname,
12
13
  getOrigin: () => window.location.origin,
@@ -22,9 +23,7 @@ const createBrowserPlatformService = (window) => ({
22
23
  });
23
24
  return data.domain || undefined;
24
25
  },
25
- openNewWindow: (url) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
26
- window.open(url);
27
- }),
26
+ isNativeMobile: false,
28
27
  openURL: (url) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
29
28
  window.location.assign(url);
30
29
  }),
@@ -3,6 +3,7 @@ import { __awaiter } from '../../../../_virtual/_tslib.js';
3
3
  import { parse } from 'tldts';
4
4
 
5
5
  const createBrowserPlatformService = (window) => ({
6
+ getDisplayOrigin: () => window.location.origin,
6
7
  getHost: () => window.location.host,
7
8
  getHostname: () => window.location.hostname,
8
9
  getOrigin: () => window.location.origin,
@@ -18,9 +19,7 @@ const createBrowserPlatformService = (window) => ({
18
19
  });
19
20
  return data.domain || undefined;
20
21
  },
21
- openNewWindow: (url) => __awaiter(void 0, void 0, void 0, function* () {
22
- window.open(url);
23
- }),
22
+ isNativeMobile: false,
24
23
  openURL: (url) => __awaiter(void 0, void 0, void 0, function* () {
25
24
  window.location.assign(url);
26
25
  }),
@@ -1,4 +1,9 @@
1
1
  export interface IPlatformService {
2
+ /**
3
+ * Indicates if the current platform is a native mobile app
4
+ * like react-native or flutter.
5
+ */
6
+ isNativeMobile: boolean;
2
7
  /**
3
8
  * Gets the hostname of the current location.
4
9
  *
@@ -17,13 +22,15 @@ export interface IPlatformService {
17
22
  * @example window.location.origin
18
23
  */
19
24
  getOrigin(): string;
25
+ /**
26
+ * Gets the origin to be displayed in the UI.
27
+ *
28
+ * @example window.location.origin
29
+ */
30
+ getDisplayOrigin(): string | undefined;
20
31
  getTLD(): string | undefined;
21
32
  /**
22
33
  * Opens a URL. If possible, should avoid new windows.
23
34
  */
24
35
  openURL(url: string): Promise<void>;
25
- /**
26
- * Opens a given URL in a new window.
27
- */
28
- openNewWindow(url: string): Promise<void>;
29
36
  }
@@ -0,0 +1,59 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var EventEmitter = require('eventemitter3');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
11
+
12
+ class StorageService {
13
+ static clearRegistry() {
14
+ StorageService.registry = {
15
+ localStorage: undefined,
16
+ secureStorage: undefined,
17
+ sessionStorage: undefined,
18
+ };
19
+ }
20
+ static registerSourceStorage(source, implementation) {
21
+ StorageService.registry[source] = implementation;
22
+ }
23
+ static getSourceStorage(source) {
24
+ return StorageService.registry[source];
25
+ }
26
+ static resolveStorage(options) {
27
+ const priority = (options === null || options === void 0 ? void 0 : options.priority) || ['localStorage'];
28
+ for (const source of priority) {
29
+ const storage = StorageService.getSourceStorage(source);
30
+ if (storage) {
31
+ return storage;
32
+ }
33
+ }
34
+ throw new Error('No available storage found based on the specified priority');
35
+ }
36
+ static getItem(key, options) {
37
+ const storage = StorageService.resolveStorage(options);
38
+ return storage.getItem(key);
39
+ }
40
+ static setItem(key, value, options) {
41
+ const storage = StorageService.resolveStorage(options);
42
+ storage.setItem(key, value);
43
+ }
44
+ static removeItem(key, options) {
45
+ const storage = StorageService.resolveStorage(options);
46
+ storage.removeItem(key);
47
+ }
48
+ static getKeys(options) {
49
+ return StorageService.resolveStorage(options).getKeys();
50
+ }
51
+ }
52
+ StorageService.registry = {
53
+ localStorage: undefined,
54
+ secureStorage: undefined,
55
+ sessionStorage: undefined,
56
+ };
57
+ StorageService.events = new EventEmitter__default["default"]();
58
+
59
+ exports.StorageService = StorageService;
@@ -0,0 +1,30 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { IStorageService } from './types';
3
+ export type StorageSource = 'secureStorage' | 'localStorage' | 'sessionStorage';
4
+ export type StorageOptions = {
5
+ /**
6
+ * An ordered array of storage sources by priority.
7
+ * The service will use the first available storage source in this order.
8
+ *
9
+ * Example:
10
+ * StorageService.setItem('key', 'value', { priority: ['secureStorage', 'sessionStorage', 'localStorage'] });
11
+ * The line above will first try to store the value in secure storage,
12
+ * if the secure storage is not available, it try to store in session storage,
13
+ * and finally if both secure and session storage are not available, it will store in local storage.
14
+ */
15
+ priority: StorageSource[];
16
+ };
17
+ export declare class StorageService {
18
+ static registry: Record<StorageSource, IStorageService | undefined>;
19
+ static clearRegistry(): void;
20
+ static registerSourceStorage(source: StorageSource, implementation: IStorageService): void;
21
+ static getSourceStorage(source: StorageSource): IStorageService | undefined;
22
+ static resolveStorage(options?: StorageOptions): IStorageService;
23
+ static getItem<T = string>(key: string, options?: StorageOptions): T | undefined;
24
+ static setItem<T>(key: string, value: T, options?: StorageOptions): void;
25
+ static removeItem(key: string, options?: StorageOptions): void;
26
+ static getKeys(options?: StorageOptions): string[];
27
+ static events: EventEmitter<{
28
+ parseFailure: (error: unknown, key: string) => void;
29
+ }, any>;
30
+ }
@@ -0,0 +1,51 @@
1
+ 'use client'
2
+ import EventEmitter from 'eventemitter3';
3
+
4
+ class StorageService {
5
+ static clearRegistry() {
6
+ StorageService.registry = {
7
+ localStorage: undefined,
8
+ secureStorage: undefined,
9
+ sessionStorage: undefined,
10
+ };
11
+ }
12
+ static registerSourceStorage(source, implementation) {
13
+ StorageService.registry[source] = implementation;
14
+ }
15
+ static getSourceStorage(source) {
16
+ return StorageService.registry[source];
17
+ }
18
+ static resolveStorage(options) {
19
+ const priority = (options === null || options === void 0 ? void 0 : options.priority) || ['localStorage'];
20
+ for (const source of priority) {
21
+ const storage = StorageService.getSourceStorage(source);
22
+ if (storage) {
23
+ return storage;
24
+ }
25
+ }
26
+ throw new Error('No available storage found based on the specified priority');
27
+ }
28
+ static getItem(key, options) {
29
+ const storage = StorageService.resolveStorage(options);
30
+ return storage.getItem(key);
31
+ }
32
+ static setItem(key, value, options) {
33
+ const storage = StorageService.resolveStorage(options);
34
+ storage.setItem(key, value);
35
+ }
36
+ static removeItem(key, options) {
37
+ const storage = StorageService.resolveStorage(options);
38
+ storage.removeItem(key);
39
+ }
40
+ static getKeys(options) {
41
+ return StorageService.resolveStorage(options).getKeys();
42
+ }
43
+ }
44
+ StorageService.registry = {
45
+ localStorage: undefined,
46
+ secureStorage: undefined,
47
+ sessionStorage: undefined,
48
+ };
49
+ StorageService.events = new EventEmitter();
50
+
51
+ export { StorageService };
@@ -0,0 +1,46 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var logger = require('../../../logger/logger.cjs');
7
+ var StorageService = require('../StorageService.cjs');
8
+
9
+ const createStorageService = ({ postfix, storage, }) => {
10
+ const getKey = (key) => (postfix ? `${key}_${postfix}` : key);
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ const packValue = (value) => JSON.stringify(value);
13
+ const unpackValue = (value) => JSON.parse(value);
14
+ const getItem = (key) => {
15
+ const value = storage.getItem(getKey(key));
16
+ if (!value) {
17
+ return undefined;
18
+ }
19
+ try {
20
+ return unpackValue(value);
21
+ }
22
+ catch (error) {
23
+ logger.logger.error(`Error while parsing ${key} from local storage`, {
24
+ value,
25
+ });
26
+ removeItem(key);
27
+ StorageService.StorageService.events.emit('parseFailure', error, key);
28
+ }
29
+ return undefined;
30
+ };
31
+ const setItem = (key, value) => {
32
+ storage.setItem(getKey(key), packValue(value));
33
+ };
34
+ const removeItem = (key) => {
35
+ storage.removeItem(getKey(key));
36
+ };
37
+ const getKeys = () => Object.keys(storage);
38
+ return {
39
+ getItem,
40
+ getKeys,
41
+ removeItem,
42
+ setItem,
43
+ };
44
+ };
45
+
46
+ exports.createStorageService = createStorageService;
@@ -0,0 +1,7 @@
1
+ import { IStorageService } from '../types';
2
+ type CreateStorageServiceProps = {
3
+ postfix?: string;
4
+ storage: Storage;
5
+ };
6
+ export declare const createStorageService: ({ postfix, storage, }: CreateStorageServiceProps) => IStorageService;
7
+ export {};
@@ -0,0 +1,42 @@
1
+ 'use client'
2
+ import { logger } from '../../../logger/logger.js';
3
+ import { StorageService } from '../StorageService.js';
4
+
5
+ const createStorageService = ({ postfix, storage, }) => {
6
+ const getKey = (key) => (postfix ? `${key}_${postfix}` : key);
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ const packValue = (value) => JSON.stringify(value);
9
+ const unpackValue = (value) => JSON.parse(value);
10
+ const getItem = (key) => {
11
+ const value = storage.getItem(getKey(key));
12
+ if (!value) {
13
+ return undefined;
14
+ }
15
+ try {
16
+ return unpackValue(value);
17
+ }
18
+ catch (error) {
19
+ logger.error(`Error while parsing ${key} from local storage`, {
20
+ value,
21
+ });
22
+ removeItem(key);
23
+ StorageService.events.emit('parseFailure', error, key);
24
+ }
25
+ return undefined;
26
+ };
27
+ const setItem = (key, value) => {
28
+ storage.setItem(getKey(key), packValue(value));
29
+ };
30
+ const removeItem = (key) => {
31
+ storage.removeItem(getKey(key));
32
+ };
33
+ const getKeys = () => Object.keys(storage);
34
+ return {
35
+ getItem,
36
+ getKeys,
37
+ removeItem,
38
+ setItem,
39
+ };
40
+ };
41
+
42
+ export { createStorageService };
@@ -0,0 +1 @@
1
+ export { createStorageService } from './createStorageService';
@@ -0,0 +1,3 @@
1
+ export { StorageService, type StorageOptions } from './StorageService';
2
+ export { createStorageService } from './createStorageService';
3
+ export type { IStorageService } from './types';
@@ -0,0 +1,6 @@
1
+ export type IStorageService = {
2
+ getItem: <T = string>(key: string) => T | undefined;
3
+ setItem: <T>(key: string, value: T) => void;
4
+ removeItem: (key: string) => void;
5
+ getKeys: () => string[];
6
+ };