@dynamic-labs/utils 3.0.0-alpha.62 → 3.0.0-alpha.63
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/CHANGELOG.md +17 -0
- package/package.json +4 -3
- package/src/cloneObjectWithOverrides/cloneObjectWithOverrides.cjs +37 -0
- package/src/cloneObjectWithOverrides/cloneObjectWithOverrides.d.ts +11 -0
- package/src/cloneObjectWithOverrides/cloneObjectWithOverrides.js +33 -0
- package/src/cloneObjectWithOverrides/index.d.ts +1 -0
- package/src/index.cjs +6 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -0
- package/src/services/StorageService/StorageService.cjs +59 -0
- package/src/services/StorageService/StorageService.d.ts +30 -0
- package/src/services/StorageService/StorageService.js +51 -0
- package/src/services/StorageService/createStorageService/createStorageService.cjs +46 -0
- package/src/services/StorageService/createStorageService/createStorageService.d.ts +7 -0
- package/src/services/StorageService/createStorageService/createStorageService.js +42 -0
- package/src/services/StorageService/createStorageService/index.d.ts +1 -0
- package/src/services/StorageService/index.d.ts +3 -0
- package/src/services/StorageService/types.d.ts +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
|
|
2
|
+
## [3.0.0-alpha.63](https://github.com/dynamic-labs/DynamicAuth/compare/v3.0.0-alpha.62...v3.0.0-alpha.63) (2024-09-10)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
### ⚠ BREAKING CHANGES
|
|
6
|
+
|
|
7
|
+
* remove signWithEmailWalletName (#6824)
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* remove signWithEmailWalletName ([#6824](https://github.com/dynamic-labs/DynamicAuth/issues/6824)) ([9da06ce](https://github.com/dynamic-labs/DynamicAuth/commit/9da06ce6f83813e00827295a2952a805e0ef5ec0))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
* omit body when creating 204 response data ([#6458](https://github.com/dynamic-labs/DynamicAuth/issues/6458)) ([06aab01](https://github.com/dynamic-labs/DynamicAuth/commit/06aab016eed0fa5ac41e43ece73780844d8d12ad))
|
|
17
|
+
* use a better object clone function to clone wallet signers ([#6835](https://github.com/dynamic-labs/DynamicAuth/issues/6835)) ([d88c195](https://github.com/dynamic-labs/DynamicAuth/commit/d88c195ce8e3af8e2fd8e6d76fbbdad618bc6c85))
|
|
18
|
+
|
|
2
19
|
## [3.0.0-alpha.62](https://github.com/dynamic-labs/DynamicAuth/compare/v3.0.0-alpha.61...v3.0.0-alpha.62) (2024-09-08)
|
|
3
20
|
|
|
4
21
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/utils",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.63",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/dynamic-labs/dynamic-auth.git",
|
|
@@ -28,9 +28,10 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@dynamic-labs/sdk-api-core": "0.0.526",
|
|
30
30
|
"tldts": "6.0.16",
|
|
31
|
-
"@dynamic-labs/logger": "3.0.0-alpha.
|
|
32
|
-
"@dynamic-labs/types": "3.0.0-alpha.
|
|
31
|
+
"@dynamic-labs/logger": "3.0.0-alpha.63",
|
|
32
|
+
"@dynamic-labs/types": "3.0.0-alpha.63",
|
|
33
33
|
"buffer": "6.0.3",
|
|
34
|
+
"eventemitter3": "5.0.1",
|
|
34
35
|
"stream": "0.0.2"
|
|
35
36
|
},
|
|
36
37
|
"peerDependencies": {}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Clones an object and includes properties with `enumerable: false`, unlike the spread
|
|
8
|
+
* operator which will leave these properties out.
|
|
9
|
+
*
|
|
10
|
+
* Optionally pass in overrides to apply to the cloned object.
|
|
11
|
+
*
|
|
12
|
+
* @param original The object to clone
|
|
13
|
+
* @param overrides An object containing properties to override on the cloned object
|
|
14
|
+
* @returns A new object that is a clone of the original object with the specified overrides
|
|
15
|
+
*/
|
|
16
|
+
const cloneObjectWithOverrides = (original, overrides = {}) => {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
// Get the property descriptors from the original object
|
|
19
|
+
const descriptors = Object.getOwnPropertyDescriptors(original);
|
|
20
|
+
// Create a new object using the same prototype as the original
|
|
21
|
+
const clone = Object.create(Object.getPrototypeOf(original), descriptors);
|
|
22
|
+
// // Loop through the overrides to redefine any read-only methods
|
|
23
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
24
|
+
// Redefine the method even if it was read-only on the original object
|
|
25
|
+
Object.defineProperty(clone, key, {
|
|
26
|
+
// Make it writable if necessary
|
|
27
|
+
configurable: true,
|
|
28
|
+
// Allow future modifications
|
|
29
|
+
enumerable: (_b = (_a = descriptors[key]) === null || _a === void 0 ? void 0 : _a.enumerable) !== null && _b !== void 0 ? _b : true,
|
|
30
|
+
value: value,
|
|
31
|
+
writable: true, // Preserve enumerability
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return clone;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
exports.cloneObjectWithOverrides = cloneObjectWithOverrides;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clones an object and includes properties with `enumerable: false`, unlike the spread
|
|
3
|
+
* operator which will leave these properties out.
|
|
4
|
+
*
|
|
5
|
+
* Optionally pass in overrides to apply to the cloned object.
|
|
6
|
+
*
|
|
7
|
+
* @param original The object to clone
|
|
8
|
+
* @param overrides An object containing properties to override on the cloned object
|
|
9
|
+
* @returns A new object that is a clone of the original object with the specified overrides
|
|
10
|
+
*/
|
|
11
|
+
export declare const cloneObjectWithOverrides: (original: object, overrides?: {}) => any;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Clones an object and includes properties with `enumerable: false`, unlike the spread
|
|
4
|
+
* operator which will leave these properties out.
|
|
5
|
+
*
|
|
6
|
+
* Optionally pass in overrides to apply to the cloned object.
|
|
7
|
+
*
|
|
8
|
+
* @param original The object to clone
|
|
9
|
+
* @param overrides An object containing properties to override on the cloned object
|
|
10
|
+
* @returns A new object that is a clone of the original object with the specified overrides
|
|
11
|
+
*/
|
|
12
|
+
const cloneObjectWithOverrides = (original, overrides = {}) => {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
// Get the property descriptors from the original object
|
|
15
|
+
const descriptors = Object.getOwnPropertyDescriptors(original);
|
|
16
|
+
// Create a new object using the same prototype as the original
|
|
17
|
+
const clone = Object.create(Object.getPrototypeOf(original), descriptors);
|
|
18
|
+
// // Loop through the overrides to redefine any read-only methods
|
|
19
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
20
|
+
// Redefine the method even if it was read-only on the original object
|
|
21
|
+
Object.defineProperty(clone, key, {
|
|
22
|
+
// Make it writable if necessary
|
|
23
|
+
configurable: true,
|
|
24
|
+
// Allow future modifications
|
|
25
|
+
enumerable: (_b = (_a = descriptors[key]) === null || _a === void 0 ? void 0 : _a.enumerable) !== null && _b !== void 0 ? _b : true,
|
|
26
|
+
value: value,
|
|
27
|
+
writable: true, // Preserve enumerability
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return clone;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { cloneObjectWithOverrides };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './cloneObjectWithOverrides';
|
package/src/index.cjs
CHANGED
|
@@ -71,6 +71,9 @@ var template = require('./template/template.cjs');
|
|
|
71
71
|
var get = require('./get/get.cjs');
|
|
72
72
|
var hexToString = require('./hexToString/hexToString.cjs');
|
|
73
73
|
var isHex = require('./isHex/isHex.cjs');
|
|
74
|
+
var StorageService = require('./services/StorageService/StorageService.cjs');
|
|
75
|
+
var createStorageService = require('./services/StorageService/createStorageService/createStorageService.cjs');
|
|
76
|
+
var cloneObjectWithOverrides = require('./cloneObjectWithOverrides/cloneObjectWithOverrides.cjs');
|
|
74
77
|
|
|
75
78
|
|
|
76
79
|
|
|
@@ -156,3 +159,6 @@ exports.template = template.template;
|
|
|
156
159
|
exports.get = get.get;
|
|
157
160
|
exports.hexToString = hexToString.hexToString;
|
|
158
161
|
exports.isHex = isHex.isHex;
|
|
162
|
+
exports.StorageService = StorageService.StorageService;
|
|
163
|
+
exports.createStorageService = createStorageService.createStorageService;
|
|
164
|
+
exports.cloneObjectWithOverrides = cloneObjectWithOverrides.cloneObjectWithOverrides;
|
package/src/index.d.ts
CHANGED
|
@@ -30,3 +30,5 @@ export { template } from './template';
|
|
|
30
30
|
export { get } from './get';
|
|
31
31
|
export { hexToString } from './hexToString';
|
|
32
32
|
export { isHex } from './isHex';
|
|
33
|
+
export { type IStorageService, type StorageOptions, StorageService, createStorageService, } from './services/StorageService';
|
|
34
|
+
export { cloneObjectWithOverrides } from './cloneObjectWithOverrides';
|
package/src/index.js
CHANGED
|
@@ -67,3 +67,6 @@ export { template } from './template/template.js';
|
|
|
67
67
|
export { get } from './get/get.js';
|
|
68
68
|
export { hexToString } from './hexToString/hexToString.js';
|
|
69
69
|
export { isHex } from './isHex/isHex.js';
|
|
70
|
+
export { StorageService } from './services/StorageService/StorageService.js';
|
|
71
|
+
export { createStorageService } from './services/StorageService/createStorageService/createStorageService.js';
|
|
72
|
+
export { cloneObjectWithOverrides } from './cloneObjectWithOverrides/cloneObjectWithOverrides.js';
|
|
@@ -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 = ({ prefix, storage, }) => {
|
|
10
|
+
const getKey = (key) => (prefix ? `${key}_${prefix}` : 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,42 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { logger } from '../../../logger/logger.js';
|
|
3
|
+
import { StorageService } from '../StorageService.js';
|
|
4
|
+
|
|
5
|
+
const createStorageService = ({ prefix, storage, }) => {
|
|
6
|
+
const getKey = (key) => (prefix ? `${key}_${prefix}` : 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';
|