@dynamic-labs/utils 0.19.0-alpha.2 → 0.19.0-alpha.21
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 +293 -0
- package/package.json +7 -4
- package/src/CancellablePromise/CancellablePromise.cjs +128 -26
- package/src/CancellablePromise/CancellablePromise.d.ts +54 -9
- package/src/CancellablePromise/CancellablePromise.js +128 -26
- package/src/CancellablePromise/index.d.ts +1 -1
- package/src/bigIntToHex/bigIntToHex.cjs +15 -0
- package/src/bigIntToHex/bigIntToHex.d.ts +1 -0
- package/src/bigIntToHex/bigIntToHex.js +11 -0
- package/src/bigIntToHex/index.d.ts +1 -0
- package/src/bufferToBase64.cjs +5 -5
- package/src/bufferToBase64.d.ts +1 -1
- package/src/bufferToBase64.js +5 -5
- package/src/errors/AccountExistsError.cjs +6 -6
- package/src/errors/AccountExistsError.d.ts +13 -13
- package/src/errors/AccountExistsError.js +6 -6
- package/src/errors/ChainalysisError.cjs +5 -5
- package/src/errors/ChainalysisError.d.ts +5 -5
- package/src/errors/ChainalysisError.js +5 -5
- package/src/errors/CustomError.cjs +16 -16
- package/src/errors/CustomError.d.ts +11 -11
- package/src/errors/CustomError.js +16 -16
- package/src/errors/DynamicError.cjs +1 -1
- package/src/errors/DynamicError.d.ts +3 -3
- package/src/errors/DynamicError.js +1 -1
- package/src/errors/EmailAlreadyExistsError.cjs +4 -4
- package/src/errors/EmailAlreadyExistsError.d.ts +4 -4
- package/src/errors/EmailAlreadyExistsError.js +4 -4
- package/src/errors/EmailProviderError.cjs +4 -4
- package/src/errors/EmailProviderError.d.ts +4 -4
- package/src/errors/EmailProviderError.js +4 -4
- package/src/errors/EmailVerificationError.cjs +4 -4
- package/src/errors/EmailVerificationError.d.ts +4 -4
- package/src/errors/EmailVerificationError.js +4 -4
- package/src/errors/GateBlockedError.cjs +5 -5
- package/src/errors/GateBlockedError.d.ts +5 -5
- package/src/errors/GateBlockedError.js +5 -5
- package/src/errors/MissingEnvironmentIdError.cjs +4 -4
- package/src/errors/MissingEnvironmentIdError.d.ts +4 -4
- package/src/errors/MissingEnvironmentIdError.js +4 -4
- package/src/errors/NoAccessError.cjs +6 -6
- package/src/errors/NoAccessError.d.ts +9 -9
- package/src/errors/NoAccessError.js +6 -6
- package/src/errors/NotSupportedError.cjs +1 -1
- package/src/errors/NotSupportedError.d.ts +3 -3
- package/src/errors/NotSupportedError.js +1 -1
- package/src/errors/SocialAccountAlreadyExistsError.cjs +13 -0
- package/src/errors/SocialAccountAlreadyExistsError.d.ts +4 -0
- package/src/errors/SocialAccountAlreadyExistsError.js +9 -0
- package/src/errors/UserHasAccountWithEmailError.cjs +5 -5
- package/src/errors/UserHasAccountWithEmailError.d.ts +5 -5
- package/src/errors/UserHasAccountWithEmailError.js +5 -5
- package/src/errors/UsernameAlreadyExistsError.cjs +4 -4
- package/src/errors/UsernameAlreadyExistsError.d.ts +4 -4
- package/src/errors/UsernameAlreadyExistsError.js +4 -4
- package/src/errors/WalletNotDeployedError.cjs +4 -4
- package/src/errors/WalletNotDeployedError.d.ts +4 -4
- package/src/errors/WalletNotDeployedError.js +4 -4
- package/src/errors/WalletUsedError.cjs +4 -4
- package/src/errors/WalletUsedError.d.ts +4 -4
- package/src/errors/WalletUsedError.js +4 -4
- package/src/errors/index.d.ts +16 -15
- package/src/getOrMapViemChain.cjs +77 -0
- package/src/getOrMapViemChain.d.ts +11 -0
- package/src/getOrMapViemChain.js +51 -0
- package/src/getProvidersFromWindow.cjs +38 -0
- package/src/getProvidersFromWindow.d.ts +7 -0
- package/src/getProvidersFromWindow.js +34 -0
- package/src/index.cjs +22 -0
- package/src/index.d.ts +15 -8
- package/src/index.js +9 -1
- package/src/isBigInt/index.d.ts +1 -0
- package/src/isBigInt/isBigInt.cjs +7 -0
- package/src/isBigInt/isBigInt.d.ts +1 -0
- package/src/isBigInt/isBigInt.js +3 -0
- package/src/isFunction/index.d.ts +1 -1
- package/src/isFunction/isFunction.cjs +1 -1
- package/src/isFunction/isFunction.d.ts +1 -1
- package/src/isFunction/isFunction.js +1 -1
- package/src/isMobile.cjs +122 -56
- package/src/isMobile.d.ts +18 -15
- package/src/isMobile.js +120 -57
- package/src/last.cjs +21 -0
- package/src/last.d.ts +15 -0
- package/src/last.js +17 -0
- package/src/localStorageAsync.cjs +17 -17
- package/src/localStorageAsync.d.ts +3 -3
- package/src/localStorageAsync.js +17 -17
- package/src/logger/index.d.ts +1 -0
- package/src/logger/logger.cjs +9 -0
- package/src/logger/logger.d.ts +2 -0
- package/src/logger/logger.js +5 -0
- package/src/parseChainId.d.ts +1 -1
- package/src/parseEvmNetworks.cjs +6 -6
- package/src/parseEvmNetworks.d.ts +2 -2
- package/src/parseEvmNetworks.js +6 -6
- package/src/retryableFn.cjs +53 -0
- package/src/retryableFn.d.ts +14 -0
- package/src/retryableFn.js +48 -0
- package/src/sleep/index.d.ts +1 -0
- package/src/sleep/sleep.cjs +12 -0
- package/src/sleep/sleep.d.ts +11 -0
- package/src/sleep/sleep.js +8 -0
package/src/localStorageAsync.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
2
|
|
|
3
|
-
const getItemAsync = (key) => __awaiter(void 0, void 0, void 0, function* () {
|
|
4
|
-
if (typeof window === 'undefined') {
|
|
5
|
-
return undefined;
|
|
6
|
-
}
|
|
7
|
-
return window.localStorage.getItem(key) || undefined;
|
|
8
|
-
});
|
|
9
|
-
const setItemAsync = (key, value) => __awaiter(void 0, void 0, void 0, function* () {
|
|
10
|
-
if (typeof window === 'undefined') {
|
|
11
|
-
return undefined;
|
|
12
|
-
}
|
|
13
|
-
return window.localStorage.setItem(key, value);
|
|
14
|
-
});
|
|
15
|
-
const removeItemAsync = (key) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
-
if (typeof window === 'undefined') {
|
|
17
|
-
return undefined;
|
|
18
|
-
}
|
|
19
|
-
return localStorage.removeItem(key);
|
|
3
|
+
const getItemAsync = (key) => __awaiter(void 0, void 0, void 0, function* () {
|
|
4
|
+
if (typeof window === 'undefined') {
|
|
5
|
+
return undefined;
|
|
6
|
+
}
|
|
7
|
+
return window.localStorage.getItem(key) || undefined;
|
|
8
|
+
});
|
|
9
|
+
const setItemAsync = (key, value) => __awaiter(void 0, void 0, void 0, function* () {
|
|
10
|
+
if (typeof window === 'undefined') {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
return window.localStorage.setItem(key, value);
|
|
14
|
+
});
|
|
15
|
+
const removeItemAsync = (key) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
if (typeof window === 'undefined') {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
return localStorage.removeItem(key);
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
export { getItemAsync, removeItemAsync, setItemAsync };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './logger';
|
package/src/parseChainId.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const parseChainId: (chainId: number | string) => number;
|
|
1
|
+
export declare const parseChainId: (chainId: number | string) => number;
|
package/src/parseEvmNetworks.cjs
CHANGED
|
@@ -4,12 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var parseChainId = require('./parseChainId.cjs');
|
|
6
6
|
|
|
7
|
-
const parseEvmNetworks = (networks) => {
|
|
8
|
-
const clone = networks.slice();
|
|
9
|
-
return clone.map((network) => {
|
|
10
|
-
network.chainId = parseChainId.parseChainId(network.chainId);
|
|
11
|
-
return network;
|
|
12
|
-
});
|
|
7
|
+
const parseEvmNetworks = (networks) => {
|
|
8
|
+
const clone = networks.slice();
|
|
9
|
+
return clone.map((network) => {
|
|
10
|
+
network.chainId = parseChainId.parseChainId(network.chainId);
|
|
11
|
+
return network;
|
|
12
|
+
});
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
exports.parseEvmNetworks = parseEvmNetworks;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { EvmNetwork, GenericNetwork } from '@dynamic-labs/types';
|
|
2
|
-
export declare const parseEvmNetworks: (networks: GenericNetwork[]) => EvmNetwork[];
|
|
1
|
+
import { EvmNetwork, GenericNetwork } from '@dynamic-labs/types';
|
|
2
|
+
export declare const parseEvmNetworks: (networks: GenericNetwork[]) => EvmNetwork[];
|
package/src/parseEvmNetworks.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { parseChainId } from './parseChainId.js';
|
|
2
2
|
|
|
3
|
-
const parseEvmNetworks = (networks) => {
|
|
4
|
-
const clone = networks.slice();
|
|
5
|
-
return clone.map((network) => {
|
|
6
|
-
network.chainId = parseChainId(network.chainId);
|
|
7
|
-
return network;
|
|
8
|
-
});
|
|
3
|
+
const parseEvmNetworks = (networks) => {
|
|
4
|
+
const clone = networks.slice();
|
|
5
|
+
return clone.map((network) => {
|
|
6
|
+
network.chainId = parseChainId(network.chainId);
|
|
7
|
+
return network;
|
|
8
|
+
});
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
export { parseEvmNetworks };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _tslib = require('../_virtual/_tslib.cjs');
|
|
6
|
+
|
|
7
|
+
const FALLBACK_UNDEFINED = 'FALLBACK_UNDEFINED';
|
|
8
|
+
const retryableFn = (fn, options = {}) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
9
|
+
const { maxRetries = 3, currentRetry = 0, timeoutMs = 100, fallbackValue = new Error('Max retries reached'), retryStrategy = 'timeout-only', } = options;
|
|
10
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
11
|
+
setTimeout(() => {
|
|
12
|
+
reject(new Error('Timeout'));
|
|
13
|
+
}, timeoutMs);
|
|
14
|
+
});
|
|
15
|
+
try {
|
|
16
|
+
const result = yield Promise.race([fn(), timeoutPromise]);
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
if (currentRetry >= maxRetries) {
|
|
21
|
+
if (fallbackValue instanceof Error) {
|
|
22
|
+
throw fallbackValue;
|
|
23
|
+
}
|
|
24
|
+
else if (fallbackValue === FALLBACK_UNDEFINED) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return fallbackValue;
|
|
28
|
+
}
|
|
29
|
+
const isTimeout = err.message === 'Timeout';
|
|
30
|
+
const shouldRetry = retryStrategy === 'timeout-and-rejection' ||
|
|
31
|
+
(retryStrategy === 'timeout-only' && isTimeout) ||
|
|
32
|
+
(retryStrategy === 'rejection-only' && !isTimeout);
|
|
33
|
+
if (!shouldRetry) {
|
|
34
|
+
if (fallbackValue instanceof Error) {
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
|
+
else if (fallbackValue === FALLBACK_UNDEFINED) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
return fallbackValue;
|
|
41
|
+
}
|
|
42
|
+
return retryableFn(fn, {
|
|
43
|
+
currentRetry: currentRetry + 1,
|
|
44
|
+
fallbackValue,
|
|
45
|
+
maxRetries,
|
|
46
|
+
retryStrategy,
|
|
47
|
+
timeoutMs,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
exports.FALLBACK_UNDEFINED = FALLBACK_UNDEFINED;
|
|
53
|
+
exports.retryableFn = retryableFn;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const FALLBACK_UNDEFINED = "FALLBACK_UNDEFINED";
|
|
2
|
+
export type RetryableFnOptions<T> = {
|
|
3
|
+
/** Maximum number of retry attempts before returning fallbackValue */
|
|
4
|
+
maxRetries?: number;
|
|
5
|
+
/** Internal only. Do not use */
|
|
6
|
+
currentRetry?: number;
|
|
7
|
+
/** Timeout in ms to wait before executing retryStrategy */
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
/** The value to fallback to when function rejects or times out */
|
|
10
|
+
fallbackValue?: T | Error | typeof FALLBACK_UNDEFINED;
|
|
11
|
+
/** Configure whether the fn should be retried only on timeout, only on rejection, or both */
|
|
12
|
+
retryStrategy?: 'timeout-only' | 'rejection-only' | 'timeout-and-rejection';
|
|
13
|
+
};
|
|
14
|
+
export declare const retryableFn: <T>(fn: () => Promise<T>, options?: RetryableFnOptions<T>) => Promise<T>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
|
+
|
|
3
|
+
const FALLBACK_UNDEFINED = 'FALLBACK_UNDEFINED';
|
|
4
|
+
const retryableFn = (fn, options = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
5
|
+
const { maxRetries = 3, currentRetry = 0, timeoutMs = 100, fallbackValue = new Error('Max retries reached'), retryStrategy = 'timeout-only', } = options;
|
|
6
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
7
|
+
setTimeout(() => {
|
|
8
|
+
reject(new Error('Timeout'));
|
|
9
|
+
}, timeoutMs);
|
|
10
|
+
});
|
|
11
|
+
try {
|
|
12
|
+
const result = yield Promise.race([fn(), timeoutPromise]);
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
catch (err) {
|
|
16
|
+
if (currentRetry >= maxRetries) {
|
|
17
|
+
if (fallbackValue instanceof Error) {
|
|
18
|
+
throw fallbackValue;
|
|
19
|
+
}
|
|
20
|
+
else if (fallbackValue === FALLBACK_UNDEFINED) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
return fallbackValue;
|
|
24
|
+
}
|
|
25
|
+
const isTimeout = err.message === 'Timeout';
|
|
26
|
+
const shouldRetry = retryStrategy === 'timeout-and-rejection' ||
|
|
27
|
+
(retryStrategy === 'timeout-only' && isTimeout) ||
|
|
28
|
+
(retryStrategy === 'rejection-only' && !isTimeout);
|
|
29
|
+
if (!shouldRetry) {
|
|
30
|
+
if (fallbackValue instanceof Error) {
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
else if (fallbackValue === FALLBACK_UNDEFINED) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
return fallbackValue;
|
|
37
|
+
}
|
|
38
|
+
return retryableFn(fn, {
|
|
39
|
+
currentRetry: currentRetry + 1,
|
|
40
|
+
fallbackValue,
|
|
41
|
+
maxRetries,
|
|
42
|
+
retryStrategy,
|
|
43
|
+
timeoutMs,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export { FALLBACK_UNDEFINED, retryableFn };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sleep } from './sleep';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
|
6
|
+
function sleep(timeoutMs, valueToResolve) {
|
|
7
|
+
return new Promise((resolve) => {
|
|
8
|
+
setTimeout(() => resolve(valueToResolve), timeoutMs);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
exports.sleep = sleep;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delays the execution of subsequent code by a specified amount of time.
|
|
3
|
+
* If a value is provided, the promise will resolve with that value after the timeout.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The type of the value to resolve with.
|
|
6
|
+
* @param {number} timeoutMs - The number of milliseconds to delay.
|
|
7
|
+
* @param {T} [valueToResolve] - Optional value to resolve the promise with after the timeout.
|
|
8
|
+
* @returns {Promise<T | void>} - Resolves after the specified timeout with valueToResolve or void.
|
|
9
|
+
*/
|
|
10
|
+
export declare function sleep(timeoutMs: number): Promise<void>;
|
|
11
|
+
export declare function sleep<T>(timeoutMs: number, valueToResolve: T): Promise<T>;
|