@oxyhq/core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/dist/cjs/AuthManager.js +361 -0
- package/dist/cjs/CrossDomainAuth.js +258 -0
- package/dist/cjs/HttpService.js +618 -0
- package/dist/cjs/OxyServices.base.js +263 -0
- package/dist/cjs/OxyServices.errors.js +22 -0
- package/dist/cjs/OxyServices.js +63 -0
- package/dist/cjs/constants/version.js +16 -0
- package/dist/cjs/crypto/index.js +20 -0
- package/dist/cjs/crypto/keyManager.js +887 -0
- package/dist/cjs/crypto/polyfill.js +64 -0
- package/dist/cjs/crypto/recoveryPhrase.js +169 -0
- package/dist/cjs/crypto/signatureService.js +296 -0
- package/dist/cjs/i18n/index.js +73 -0
- package/dist/cjs/i18n/locales/ar-SA.json +120 -0
- package/dist/cjs/i18n/locales/ca-ES.json +120 -0
- package/dist/cjs/i18n/locales/de-DE.json +120 -0
- package/dist/cjs/i18n/locales/en-US.json +956 -0
- package/dist/cjs/i18n/locales/es-ES.json +944 -0
- package/dist/cjs/i18n/locales/fr-FR.json +120 -0
- package/dist/cjs/i18n/locales/it-IT.json +120 -0
- package/dist/cjs/i18n/locales/ja-JP.json +119 -0
- package/dist/cjs/i18n/locales/ko-KR.json +120 -0
- package/dist/cjs/i18n/locales/locales/ar-SA.json +120 -0
- package/dist/cjs/i18n/locales/locales/ca-ES.json +120 -0
- package/dist/cjs/i18n/locales/locales/de-DE.json +120 -0
- package/dist/cjs/i18n/locales/locales/en-US.json +956 -0
- package/dist/cjs/i18n/locales/locales/es-ES.json +944 -0
- package/dist/cjs/i18n/locales/locales/fr-FR.json +120 -0
- package/dist/cjs/i18n/locales/locales/it-IT.json +120 -0
- package/dist/cjs/i18n/locales/locales/ja-JP.json +119 -0
- package/dist/cjs/i18n/locales/locales/ko-KR.json +120 -0
- package/dist/cjs/i18n/locales/locales/pt-PT.json +120 -0
- package/dist/cjs/i18n/locales/locales/zh-CN.json +120 -0
- package/dist/cjs/i18n/locales/pt-PT.json +120 -0
- package/dist/cjs/i18n/locales/zh-CN.json +120 -0
- package/dist/cjs/index.js +153 -0
- package/dist/cjs/mixins/OxyServices.analytics.js +49 -0
- package/dist/cjs/mixins/OxyServices.assets.js +380 -0
- package/dist/cjs/mixins/OxyServices.auth.js +259 -0
- package/dist/cjs/mixins/OxyServices.developer.js +97 -0
- package/dist/cjs/mixins/OxyServices.devices.js +116 -0
- package/dist/cjs/mixins/OxyServices.features.js +309 -0
- package/dist/cjs/mixins/OxyServices.fedcm.js +435 -0
- package/dist/cjs/mixins/OxyServices.karma.js +108 -0
- package/dist/cjs/mixins/OxyServices.language.js +154 -0
- package/dist/cjs/mixins/OxyServices.location.js +43 -0
- package/dist/cjs/mixins/OxyServices.payment.js +158 -0
- package/dist/cjs/mixins/OxyServices.popup.js +371 -0
- package/dist/cjs/mixins/OxyServices.privacy.js +162 -0
- package/dist/cjs/mixins/OxyServices.redirect.js +345 -0
- package/dist/cjs/mixins/OxyServices.security.js +81 -0
- package/dist/cjs/mixins/OxyServices.user.js +355 -0
- package/dist/cjs/mixins/OxyServices.utility.js +156 -0
- package/dist/cjs/mixins/index.js +79 -0
- package/dist/cjs/mixins/mixinHelpers.js +53 -0
- package/dist/cjs/models/interfaces.js +20 -0
- package/dist/cjs/models/session.js +2 -0
- package/dist/cjs/shared/index.js +70 -0
- package/dist/cjs/shared/utils/colorUtils.js +153 -0
- package/dist/cjs/shared/utils/debugUtils.js +73 -0
- package/dist/cjs/shared/utils/errorUtils.js +183 -0
- package/dist/cjs/shared/utils/index.js +49 -0
- package/dist/cjs/shared/utils/networkUtils.js +183 -0
- package/dist/cjs/shared/utils/themeUtils.js +106 -0
- package/dist/cjs/utils/apiUtils.js +61 -0
- package/dist/cjs/utils/asyncUtils.js +194 -0
- package/dist/cjs/utils/cache.js +226 -0
- package/dist/cjs/utils/deviceManager.js +205 -0
- package/dist/cjs/utils/errorUtils.js +154 -0
- package/dist/cjs/utils/index.js +26 -0
- package/dist/cjs/utils/languageUtils.js +165 -0
- package/dist/cjs/utils/loggerUtils.js +126 -0
- package/dist/cjs/utils/platform.js +144 -0
- package/dist/cjs/utils/requestUtils.js +209 -0
- package/dist/cjs/utils/sessionUtils.js +181 -0
- package/dist/cjs/utils/validationUtils.js +173 -0
- package/dist/esm/AuthManager.js +356 -0
- package/dist/esm/CrossDomainAuth.js +253 -0
- package/dist/esm/HttpService.js +614 -0
- package/dist/esm/OxyServices.base.js +259 -0
- package/dist/esm/OxyServices.errors.js +17 -0
- package/dist/esm/OxyServices.js +59 -0
- package/dist/esm/constants/version.js +13 -0
- package/dist/esm/crypto/index.js +13 -0
- package/dist/esm/crypto/keyManager.js +850 -0
- package/dist/esm/crypto/polyfill.js +61 -0
- package/dist/esm/crypto/recoveryPhrase.js +132 -0
- package/dist/esm/crypto/signatureService.js +259 -0
- package/dist/esm/i18n/index.js +69 -0
- package/dist/esm/i18n/locales/ar-SA.json +120 -0
- package/dist/esm/i18n/locales/ca-ES.json +120 -0
- package/dist/esm/i18n/locales/de-DE.json +120 -0
- package/dist/esm/i18n/locales/en-US.json +956 -0
- package/dist/esm/i18n/locales/es-ES.json +944 -0
- package/dist/esm/i18n/locales/fr-FR.json +120 -0
- package/dist/esm/i18n/locales/it-IT.json +120 -0
- package/dist/esm/i18n/locales/ja-JP.json +119 -0
- package/dist/esm/i18n/locales/ko-KR.json +120 -0
- package/dist/esm/i18n/locales/locales/ar-SA.json +120 -0
- package/dist/esm/i18n/locales/locales/ca-ES.json +120 -0
- package/dist/esm/i18n/locales/locales/de-DE.json +120 -0
- package/dist/esm/i18n/locales/locales/en-US.json +956 -0
- package/dist/esm/i18n/locales/locales/es-ES.json +944 -0
- package/dist/esm/i18n/locales/locales/fr-FR.json +120 -0
- package/dist/esm/i18n/locales/locales/it-IT.json +120 -0
- package/dist/esm/i18n/locales/locales/ja-JP.json +119 -0
- package/dist/esm/i18n/locales/locales/ko-KR.json +120 -0
- package/dist/esm/i18n/locales/locales/pt-PT.json +120 -0
- package/dist/esm/i18n/locales/locales/zh-CN.json +120 -0
- package/dist/esm/i18n/locales/pt-PT.json +120 -0
- package/dist/esm/i18n/locales/zh-CN.json +120 -0
- package/dist/esm/index.js +55 -0
- package/dist/esm/mixins/OxyServices.analytics.js +46 -0
- package/dist/esm/mixins/OxyServices.assets.js +377 -0
- package/dist/esm/mixins/OxyServices.auth.js +256 -0
- package/dist/esm/mixins/OxyServices.developer.js +94 -0
- package/dist/esm/mixins/OxyServices.devices.js +113 -0
- package/dist/esm/mixins/OxyServices.features.js +306 -0
- package/dist/esm/mixins/OxyServices.fedcm.js +433 -0
- package/dist/esm/mixins/OxyServices.karma.js +105 -0
- package/dist/esm/mixins/OxyServices.language.js +118 -0
- package/dist/esm/mixins/OxyServices.location.js +40 -0
- package/dist/esm/mixins/OxyServices.payment.js +155 -0
- package/dist/esm/mixins/OxyServices.popup.js +369 -0
- package/dist/esm/mixins/OxyServices.privacy.js +159 -0
- package/dist/esm/mixins/OxyServices.redirect.js +343 -0
- package/dist/esm/mixins/OxyServices.security.js +78 -0
- package/dist/esm/mixins/OxyServices.user.js +352 -0
- package/dist/esm/mixins/OxyServices.utility.js +153 -0
- package/dist/esm/mixins/index.js +76 -0
- package/dist/esm/mixins/mixinHelpers.js +48 -0
- package/dist/esm/models/interfaces.js +17 -0
- package/dist/esm/models/session.js +1 -0
- package/dist/esm/shared/index.js +31 -0
- package/dist/esm/shared/utils/colorUtils.js +143 -0
- package/dist/esm/shared/utils/debugUtils.js +65 -0
- package/dist/esm/shared/utils/errorUtils.js +170 -0
- package/dist/esm/shared/utils/index.js +15 -0
- package/dist/esm/shared/utils/networkUtils.js +173 -0
- package/dist/esm/shared/utils/themeUtils.js +98 -0
- package/dist/esm/utils/apiUtils.js +55 -0
- package/dist/esm/utils/asyncUtils.js +179 -0
- package/dist/esm/utils/cache.js +218 -0
- package/dist/esm/utils/deviceManager.js +168 -0
- package/dist/esm/utils/errorUtils.js +146 -0
- package/dist/esm/utils/index.js +7 -0
- package/dist/esm/utils/languageUtils.js +158 -0
- package/dist/esm/utils/loggerUtils.js +115 -0
- package/dist/esm/utils/platform.js +102 -0
- package/dist/esm/utils/requestUtils.js +203 -0
- package/dist/esm/utils/sessionUtils.js +171 -0
- package/dist/esm/utils/validationUtils.js +153 -0
- package/dist/types/AuthManager.d.ts +143 -0
- package/dist/types/CrossDomainAuth.d.ts +160 -0
- package/dist/types/HttpService.d.ts +163 -0
- package/dist/types/OxyServices.base.d.ts +126 -0
- package/dist/types/OxyServices.d.ts +81 -0
- package/dist/types/OxyServices.errors.d.ts +11 -0
- package/dist/types/constants/version.d.ts +13 -0
- package/dist/types/crypto/index.d.ts +11 -0
- package/dist/types/crypto/keyManager.d.ts +189 -0
- package/dist/types/crypto/polyfill.d.ts +11 -0
- package/dist/types/crypto/recoveryPhrase.d.ts +58 -0
- package/dist/types/crypto/signatureService.d.ts +86 -0
- package/dist/types/i18n/index.d.ts +3 -0
- package/dist/types/index.d.ts +50 -0
- package/dist/types/mixins/OxyServices.analytics.d.ts +66 -0
- package/dist/types/mixins/OxyServices.assets.d.ts +135 -0
- package/dist/types/mixins/OxyServices.auth.d.ts +186 -0
- package/dist/types/mixins/OxyServices.developer.d.ts +99 -0
- package/dist/types/mixins/OxyServices.devices.d.ts +96 -0
- package/dist/types/mixins/OxyServices.features.d.ts +228 -0
- package/dist/types/mixins/OxyServices.fedcm.d.ts +200 -0
- package/dist/types/mixins/OxyServices.karma.d.ts +85 -0
- package/dist/types/mixins/OxyServices.language.d.ts +81 -0
- package/dist/types/mixins/OxyServices.location.d.ts +64 -0
- package/dist/types/mixins/OxyServices.payment.d.ts +111 -0
- package/dist/types/mixins/OxyServices.popup.d.ts +205 -0
- package/dist/types/mixins/OxyServices.privacy.d.ts +122 -0
- package/dist/types/mixins/OxyServices.redirect.d.ts +245 -0
- package/dist/types/mixins/OxyServices.security.d.ts +78 -0
- package/dist/types/mixins/OxyServices.user.d.ts +182 -0
- package/dist/types/mixins/OxyServices.utility.d.ts +93 -0
- package/dist/types/mixins/index.d.ts +30 -0
- package/dist/types/mixins/mixinHelpers.d.ts +31 -0
- package/dist/types/models/interfaces.d.ts +415 -0
- package/dist/types/models/session.d.ts +27 -0
- package/dist/types/shared/index.d.ts +28 -0
- package/dist/types/shared/utils/colorUtils.d.ts +104 -0
- package/dist/types/shared/utils/debugUtils.d.ts +48 -0
- package/dist/types/shared/utils/errorUtils.d.ts +97 -0
- package/dist/types/shared/utils/index.d.ts +13 -0
- package/dist/types/shared/utils/networkUtils.d.ts +139 -0
- package/dist/types/shared/utils/themeUtils.d.ts +90 -0
- package/dist/types/utils/apiUtils.d.ts +53 -0
- package/dist/types/utils/asyncUtils.d.ts +58 -0
- package/dist/types/utils/cache.d.ts +127 -0
- package/dist/types/utils/deviceManager.d.ts +65 -0
- package/dist/types/utils/errorUtils.d.ts +46 -0
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/languageUtils.d.ts +37 -0
- package/dist/types/utils/loggerUtils.d.ts +48 -0
- package/dist/types/utils/platform.d.ts +40 -0
- package/dist/types/utils/requestUtils.d.ts +123 -0
- package/dist/types/utils/sessionUtils.d.ts +54 -0
- package/dist/types/utils/validationUtils.d.ts +85 -0
- package/package.json +84 -0
- package/src/AuthManager.ts +436 -0
- package/src/CrossDomainAuth.ts +307 -0
- package/src/HttpService.ts +752 -0
- package/src/OxyServices.base.ts +334 -0
- package/src/OxyServices.errors.ts +26 -0
- package/src/OxyServices.ts +129 -0
- package/src/constants/version.ts +15 -0
- package/src/crypto/index.ts +25 -0
- package/src/crypto/keyManager.ts +962 -0
- package/src/crypto/polyfill.ts +70 -0
- package/src/crypto/recoveryPhrase.ts +166 -0
- package/src/crypto/signatureService.ts +323 -0
- package/src/i18n/index.ts +75 -0
- package/src/i18n/locales/ar-SA.json +120 -0
- package/src/i18n/locales/ca-ES.json +120 -0
- package/src/i18n/locales/de-DE.json +120 -0
- package/src/i18n/locales/en-US.json +956 -0
- package/src/i18n/locales/es-ES.json +944 -0
- package/src/i18n/locales/fr-FR.json +120 -0
- package/src/i18n/locales/it-IT.json +120 -0
- package/src/i18n/locales/ja-JP.json +119 -0
- package/src/i18n/locales/ko-KR.json +120 -0
- package/src/i18n/locales/pt-PT.json +120 -0
- package/src/i18n/locales/zh-CN.json +120 -0
- package/src/index.ts +153 -0
- package/src/mixins/OxyServices.analytics.ts +53 -0
- package/src/mixins/OxyServices.assets.ts +412 -0
- package/src/mixins/OxyServices.auth.ts +358 -0
- package/src/mixins/OxyServices.developer.ts +114 -0
- package/src/mixins/OxyServices.devices.ts +119 -0
- package/src/mixins/OxyServices.features.ts +428 -0
- package/src/mixins/OxyServices.fedcm.ts +494 -0
- package/src/mixins/OxyServices.karma.ts +111 -0
- package/src/mixins/OxyServices.language.ts +127 -0
- package/src/mixins/OxyServices.location.ts +46 -0
- package/src/mixins/OxyServices.payment.ts +163 -0
- package/src/mixins/OxyServices.popup.ts +443 -0
- package/src/mixins/OxyServices.privacy.ts +182 -0
- package/src/mixins/OxyServices.redirect.ts +397 -0
- package/src/mixins/OxyServices.security.ts +103 -0
- package/src/mixins/OxyServices.user.ts +392 -0
- package/src/mixins/OxyServices.utility.ts +191 -0
- package/src/mixins/index.ts +91 -0
- package/src/mixins/mixinHelpers.ts +69 -0
- package/src/models/interfaces.ts +511 -0
- package/src/models/session.ts +30 -0
- package/src/shared/index.ts +82 -0
- package/src/shared/utils/colorUtils.ts +155 -0
- package/src/shared/utils/debugUtils.ts +73 -0
- package/src/shared/utils/errorUtils.ts +181 -0
- package/src/shared/utils/index.ts +59 -0
- package/src/shared/utils/networkUtils.ts +248 -0
- package/src/shared/utils/themeUtils.ts +115 -0
- package/src/types/bip39.d.ts +32 -0
- package/src/types/buffer.d.ts +97 -0
- package/src/types/color.d.ts +20 -0
- package/src/types/elliptic.d.ts +62 -0
- package/src/utils/apiUtils.ts +88 -0
- package/src/utils/asyncUtils.ts +252 -0
- package/src/utils/cache.ts +264 -0
- package/src/utils/deviceManager.ts +198 -0
- package/src/utils/errorUtils.ts +216 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/languageUtils.ts +174 -0
- package/src/utils/loggerUtils.ts +153 -0
- package/src/utils/platform.ts +117 -0
- package/src/utils/requestUtils.ts +237 -0
- package/src/utils/sessionUtils.ts +206 -0
- package/src/utils/validationUtils.ts +174 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Network Utility Functions
|
|
4
|
+
*
|
|
5
|
+
* Consolidated network utilities including circuit breaker pattern
|
|
6
|
+
* and exponential backoff for resilient API calls.
|
|
7
|
+
*
|
|
8
|
+
* @module shared/utils/networkUtils
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.withRetry = exports.delay = exports.shouldAllowRequest = exports.recordSuccess = exports.recordFailure = exports.calculateBackoffInterval = exports.createCircuitBreakerState = exports.DEFAULT_CIRCUIT_BREAKER_CONFIG = void 0;
|
|
12
|
+
/**
|
|
13
|
+
* Default circuit breaker configuration.
|
|
14
|
+
*/
|
|
15
|
+
exports.DEFAULT_CIRCUIT_BREAKER_CONFIG = {
|
|
16
|
+
baseInterval: 10000,
|
|
17
|
+
maxInterval: 60000,
|
|
18
|
+
maxFailures: 5,
|
|
19
|
+
recoveryTimeout: 30000,
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Creates initial circuit breaker state.
|
|
23
|
+
*
|
|
24
|
+
* @param config - Optional custom configuration
|
|
25
|
+
* @returns Initial circuit breaker state
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const state = createCircuitBreakerState({ maxFailures: 3 });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
const createCircuitBreakerState = (config = {}) => {
|
|
33
|
+
const { baseInterval, maxInterval, maxFailures } = {
|
|
34
|
+
...exports.DEFAULT_CIRCUIT_BREAKER_CONFIG,
|
|
35
|
+
...config,
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
consecutiveFailures: 0,
|
|
39
|
+
currentInterval: baseInterval,
|
|
40
|
+
baseInterval,
|
|
41
|
+
maxInterval,
|
|
42
|
+
maxFailures,
|
|
43
|
+
isOpen: false,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.createCircuitBreakerState = createCircuitBreakerState;
|
|
47
|
+
/**
|
|
48
|
+
* Calculates next interval using exponential backoff.
|
|
49
|
+
*
|
|
50
|
+
* @param state - Current circuit breaker state
|
|
51
|
+
* @returns Next interval in milliseconds
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const nextInterval = calculateBackoffInterval(state);
|
|
56
|
+
* await delay(nextInterval);
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
const calculateBackoffInterval = (state) => {
|
|
60
|
+
const { consecutiveFailures, baseInterval, maxInterval } = state;
|
|
61
|
+
if (consecutiveFailures === 0)
|
|
62
|
+
return baseInterval;
|
|
63
|
+
const backoffMultiplier = Math.min(Math.pow(2, consecutiveFailures - 1), maxInterval / baseInterval);
|
|
64
|
+
return Math.min(baseInterval * backoffMultiplier, maxInterval);
|
|
65
|
+
};
|
|
66
|
+
exports.calculateBackoffInterval = calculateBackoffInterval;
|
|
67
|
+
/**
|
|
68
|
+
* Records a failure and updates circuit breaker state.
|
|
69
|
+
*
|
|
70
|
+
* @param state - Current circuit breaker state
|
|
71
|
+
* @returns Updated state after recording failure
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* try {
|
|
76
|
+
* await apiCall();
|
|
77
|
+
* state = recordSuccess(state);
|
|
78
|
+
* } catch (error) {
|
|
79
|
+
* state = recordFailure(state);
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
const recordFailure = (state) => {
|
|
84
|
+
const newFailures = state.consecutiveFailures + 1;
|
|
85
|
+
const newInterval = (0, exports.calculateBackoffInterval)({
|
|
86
|
+
...state,
|
|
87
|
+
consecutiveFailures: newFailures,
|
|
88
|
+
});
|
|
89
|
+
const shouldOpenCircuit = newFailures >= state.maxFailures;
|
|
90
|
+
const finalInterval = shouldOpenCircuit ? state.maxInterval : newInterval;
|
|
91
|
+
return {
|
|
92
|
+
...state,
|
|
93
|
+
consecutiveFailures: newFailures,
|
|
94
|
+
currentInterval: finalInterval,
|
|
95
|
+
isOpen: shouldOpenCircuit,
|
|
96
|
+
openedAt: shouldOpenCircuit ? Date.now() : state.openedAt,
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
exports.recordFailure = recordFailure;
|
|
100
|
+
/**
|
|
101
|
+
* Records a success and resets circuit breaker state.
|
|
102
|
+
*
|
|
103
|
+
* @param state - Current circuit breaker state
|
|
104
|
+
* @returns Reset state after successful request
|
|
105
|
+
*/
|
|
106
|
+
const recordSuccess = (state) => {
|
|
107
|
+
return {
|
|
108
|
+
...state,
|
|
109
|
+
consecutiveFailures: 0,
|
|
110
|
+
currentInterval: state.baseInterval,
|
|
111
|
+
isOpen: false,
|
|
112
|
+
openedAt: undefined,
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
exports.recordSuccess = recordSuccess;
|
|
116
|
+
/**
|
|
117
|
+
* Checks if the circuit breaker should allow a request.
|
|
118
|
+
*
|
|
119
|
+
* When the circuit is open, it will only allow requests after
|
|
120
|
+
* the recovery timeout has passed (half-open state).
|
|
121
|
+
*
|
|
122
|
+
* @param state - Current circuit breaker state
|
|
123
|
+
* @param recoveryTimeout - Time to wait before allowing recovery attempts
|
|
124
|
+
* @returns true if request should be allowed
|
|
125
|
+
*/
|
|
126
|
+
const shouldAllowRequest = (state, recoveryTimeout = exports.DEFAULT_CIRCUIT_BREAKER_CONFIG.recoveryTimeout) => {
|
|
127
|
+
if (!state.isOpen)
|
|
128
|
+
return true;
|
|
129
|
+
if (!state.openedAt)
|
|
130
|
+
return true;
|
|
131
|
+
const timeSinceOpen = Date.now() - state.openedAt;
|
|
132
|
+
return timeSinceOpen >= recoveryTimeout;
|
|
133
|
+
};
|
|
134
|
+
exports.shouldAllowRequest = shouldAllowRequest;
|
|
135
|
+
/**
|
|
136
|
+
* Delays execution for a specified duration.
|
|
137
|
+
*
|
|
138
|
+
* @param ms - Milliseconds to delay
|
|
139
|
+
* @returns Promise that resolves after the delay
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* await delay(1000); // Wait 1 second
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
147
|
+
exports.delay = delay;
|
|
148
|
+
/**
|
|
149
|
+
* Executes a function with exponential backoff retry.
|
|
150
|
+
*
|
|
151
|
+
* @param fn - Async function to execute
|
|
152
|
+
* @param options - Retry options
|
|
153
|
+
* @returns Result of the function
|
|
154
|
+
* @throws Last error if all retries fail
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* const result = await withRetry(
|
|
159
|
+
* () => fetchData(),
|
|
160
|
+
* { maxRetries: 3, baseDelay: 1000 }
|
|
161
|
+
* );
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
const withRetry = async (fn, options = {}) => {
|
|
165
|
+
const { maxRetries = 3, baseDelay = 1000, maxDelay = 30000, shouldRetry = () => true, onRetry, } = options;
|
|
166
|
+
let lastError;
|
|
167
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
168
|
+
try {
|
|
169
|
+
return await fn();
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
lastError = error;
|
|
173
|
+
if (attempt === maxRetries || !shouldRetry(error)) {
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
const delayMs = Math.min(baseDelay * Math.pow(2, attempt), maxDelay);
|
|
177
|
+
onRetry?.(error, attempt + 1);
|
|
178
|
+
await (0, exports.delay)(delayMs);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
throw lastError;
|
|
182
|
+
};
|
|
183
|
+
exports.withRetry = withRetry;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Theme Utility Functions
|
|
4
|
+
*
|
|
5
|
+
* Consolidated theme utilities for normalizing and handling color schemes
|
|
6
|
+
* across the OxyServices ecosystem.
|
|
7
|
+
*
|
|
8
|
+
* @module shared/utils/themeUtils
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.getSystemColorScheme = exports.systemPrefersDarkMode = exports.getOppositeTheme = exports.normalizeColorScheme = exports.normalizeTheme = void 0;
|
|
12
|
+
/**
|
|
13
|
+
* Normalizes a theme value to ensure it's always 'light' or 'dark'.
|
|
14
|
+
*
|
|
15
|
+
* @param theme - Theme value (may be 'light' | 'dark' | string | undefined)
|
|
16
|
+
* @returns Normalized 'light' | 'dark' theme (defaults to 'light')
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* normalizeTheme('dark'); // 'dark'
|
|
21
|
+
* normalizeTheme('light'); // 'light'
|
|
22
|
+
* normalizeTheme('unknown'); // 'light'
|
|
23
|
+
* normalizeTheme(undefined); // 'light'
|
|
24
|
+
* normalizeTheme(null); // 'light'
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const normalizeTheme = (theme) => theme === 'light' || theme === 'dark' ? theme : 'light';
|
|
28
|
+
exports.normalizeTheme = normalizeTheme;
|
|
29
|
+
/**
|
|
30
|
+
* Normalizes a color scheme value with optional fallback chain.
|
|
31
|
+
*
|
|
32
|
+
* Handles null/undefined cases from React Native's useColorScheme() hook
|
|
33
|
+
* with a proper fallback chain.
|
|
34
|
+
*
|
|
35
|
+
* @param colorScheme - The color scheme from useColorScheme() hook (may be null/undefined)
|
|
36
|
+
* @param theme - Optional theme prop as fallback
|
|
37
|
+
* @returns Normalized 'light' | 'dark' color scheme
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* // In a React Native component:
|
|
42
|
+
* const systemScheme = useColorScheme(); // might be null
|
|
43
|
+
* const colorScheme = normalizeColorScheme(systemScheme, props.theme);
|
|
44
|
+
*
|
|
45
|
+
* normalizeColorScheme('dark', 'light'); // 'dark'
|
|
46
|
+
* normalizeColorScheme(null, 'dark'); // 'dark'
|
|
47
|
+
* normalizeColorScheme(undefined, undefined); // 'light'
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
const normalizeColorScheme = (colorScheme, theme) => {
|
|
51
|
+
if (colorScheme === 'light' || colorScheme === 'dark') {
|
|
52
|
+
return colorScheme;
|
|
53
|
+
}
|
|
54
|
+
if (theme === 'light' || theme === 'dark') {
|
|
55
|
+
return theme;
|
|
56
|
+
}
|
|
57
|
+
return 'light';
|
|
58
|
+
};
|
|
59
|
+
exports.normalizeColorScheme = normalizeColorScheme;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the opposite theme value.
|
|
62
|
+
*
|
|
63
|
+
* @param theme - Current theme value
|
|
64
|
+
* @returns The opposite theme ('light' → 'dark', 'dark' → 'light')
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* getOppositeTheme('light'); // 'dark'
|
|
69
|
+
* getOppositeTheme('dark'); // 'light'
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
const getOppositeTheme = (theme) => theme === 'light' ? 'dark' : 'light';
|
|
73
|
+
exports.getOppositeTheme = getOppositeTheme;
|
|
74
|
+
/**
|
|
75
|
+
* Checks if the system prefers dark mode.
|
|
76
|
+
*
|
|
77
|
+
* This function only works in browser environments.
|
|
78
|
+
* Returns false in non-browser environments (Node.js, React Native).
|
|
79
|
+
*
|
|
80
|
+
* @returns true if system prefers dark mode, false otherwise
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* if (systemPrefersDarkMode()) {
|
|
85
|
+
* setTheme('dark');
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
const systemPrefersDarkMode = () => {
|
|
90
|
+
if (typeof window === 'undefined')
|
|
91
|
+
return false;
|
|
92
|
+
return window.matchMedia?.('(prefers-color-scheme: dark)').matches ?? false;
|
|
93
|
+
};
|
|
94
|
+
exports.systemPrefersDarkMode = systemPrefersDarkMode;
|
|
95
|
+
/**
|
|
96
|
+
* Gets the system's preferred color scheme.
|
|
97
|
+
*
|
|
98
|
+
* @returns 'dark' if system prefers dark mode, 'light' otherwise
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const theme = getSystemColorScheme();
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
const getSystemColorScheme = () => (0, exports.systemPrefersDarkMode)() ? 'dark' : 'light';
|
|
106
|
+
exports.getSystemColorScheme = getSystemColorScheme;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utility functions for common API patterns
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildSearchParams = buildSearchParams;
|
|
7
|
+
exports.buildUrl = buildUrl;
|
|
8
|
+
exports.buildPaginationParams = buildPaginationParams;
|
|
9
|
+
exports.safeJsonParse = safeJsonParse;
|
|
10
|
+
/**
|
|
11
|
+
* Build URL search parameters from an object
|
|
12
|
+
* @param params Object with parameter key-value pairs
|
|
13
|
+
* @returns URLSearchParams instance
|
|
14
|
+
*/
|
|
15
|
+
function buildSearchParams(params) {
|
|
16
|
+
const searchParams = new URLSearchParams();
|
|
17
|
+
for (const [key, value] of Object.entries(params)) {
|
|
18
|
+
if (value !== undefined && value !== null) {
|
|
19
|
+
searchParams.append(key, value.toString());
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return searchParams;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Build URL with search parameters
|
|
26
|
+
* @param baseUrl Base URL
|
|
27
|
+
* @param params Object with parameter key-value pairs
|
|
28
|
+
* @returns Complete URL with search parameters
|
|
29
|
+
*/
|
|
30
|
+
function buildUrl(baseUrl, params) {
|
|
31
|
+
if (!params)
|
|
32
|
+
return baseUrl;
|
|
33
|
+
const searchParams = buildSearchParams(params);
|
|
34
|
+
const queryString = searchParams.toString();
|
|
35
|
+
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Build pagination search parameters
|
|
39
|
+
* @param params Pagination parameters
|
|
40
|
+
* @returns URLSearchParams with pagination
|
|
41
|
+
*/
|
|
42
|
+
function buildPaginationParams(params) {
|
|
43
|
+
return buildSearchParams(params);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Safe JSON parsing with error handling
|
|
47
|
+
* @param data Data to parse
|
|
48
|
+
* @param fallback Fallback value if parsing fails
|
|
49
|
+
* @returns Parsed data or fallback
|
|
50
|
+
*/
|
|
51
|
+
function safeJsonParse(data, fallback) {
|
|
52
|
+
if (typeof data === 'string') {
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(data);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return fallback;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return data;
|
|
61
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Async utilities for common asynchronous patterns and error handling
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.delay = void 0;
|
|
7
|
+
exports.withErrorHandling = withErrorHandling;
|
|
8
|
+
exports.parallelWithErrorHandling = parallelWithErrorHandling;
|
|
9
|
+
exports.retryAsync = retryAsync;
|
|
10
|
+
exports.debounceAsync = debounceAsync;
|
|
11
|
+
exports.throttleAsync = throttleAsync;
|
|
12
|
+
exports.sequentialWithProgress = sequentialWithProgress;
|
|
13
|
+
exports.batchAsync = batchAsync;
|
|
14
|
+
exports.createCancellableAsync = createCancellableAsync;
|
|
15
|
+
exports.withTimeout = withTimeout;
|
|
16
|
+
exports.withLoadingState = withLoadingState;
|
|
17
|
+
exports.retryOnError = retryOnError;
|
|
18
|
+
const loggerUtils_1 = require("./loggerUtils");
|
|
19
|
+
/**
|
|
20
|
+
* Wrapper for async operations with automatic error handling
|
|
21
|
+
* Returns null on error instead of throwing
|
|
22
|
+
*/
|
|
23
|
+
async function withErrorHandling(operation, errorHandler, context) {
|
|
24
|
+
try {
|
|
25
|
+
return await operation();
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (errorHandler) {
|
|
29
|
+
errorHandler(error);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
loggerUtils_1.logger.error(`Error in ${context || 'operation'}`, error instanceof Error ? error : new Error(String(error)), {
|
|
33
|
+
component: 'asyncUtils',
|
|
34
|
+
method: 'withErrorHandling',
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Execute multiple async operations in parallel with error handling
|
|
42
|
+
*/
|
|
43
|
+
async function parallelWithErrorHandling(operations, errorHandler) {
|
|
44
|
+
const results = await Promise.allSettled(operations.map((op, index) => withErrorHandling(op, error => errorHandler?.(error, index))));
|
|
45
|
+
return results.map(result => result.status === 'fulfilled' ? result.value : null);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Retry an async operation with exponential backoff
|
|
49
|
+
*
|
|
50
|
+
* By default, does not retry on 4xx errors (client errors).
|
|
51
|
+
* Use shouldRetry callback to customize retry behavior.
|
|
52
|
+
*/
|
|
53
|
+
async function retryAsync(operation, maxRetries = 3, baseDelay = 1000, shouldRetry) {
|
|
54
|
+
let lastError;
|
|
55
|
+
// Default shouldRetry: don't retry on 4xx errors
|
|
56
|
+
const defaultShouldRetry = (error) => {
|
|
57
|
+
// Don't retry on 4xx errors (client errors)
|
|
58
|
+
if (error?.response?.status >= 400 && error?.response?.status < 500) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
};
|
|
63
|
+
const retryCheck = shouldRetry || defaultShouldRetry;
|
|
64
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
65
|
+
try {
|
|
66
|
+
return await operation();
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
lastError = error;
|
|
70
|
+
if (attempt === maxRetries) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
if (!retryCheck(error)) {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
// Calculate delay with exponential backoff and jitter
|
|
77
|
+
const delay = baseDelay * Math.pow(2, attempt) + Math.random() * 1000;
|
|
78
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
throw lastError;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Debounce async function calls
|
|
85
|
+
*/
|
|
86
|
+
function debounceAsync(func, delay) {
|
|
87
|
+
let timeoutId;
|
|
88
|
+
const lastPromise = null;
|
|
89
|
+
return (...args) => {
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
clearTimeout(timeoutId);
|
|
92
|
+
timeoutId = setTimeout(async () => {
|
|
93
|
+
try {
|
|
94
|
+
const result = await func(...args);
|
|
95
|
+
resolve(result);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
reject(error);
|
|
99
|
+
}
|
|
100
|
+
}, delay);
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Throttle async function calls
|
|
106
|
+
*/
|
|
107
|
+
function throttleAsync(func, limit, interval) {
|
|
108
|
+
let inThrottle = false;
|
|
109
|
+
let lastPromise = null;
|
|
110
|
+
return (...args) => {
|
|
111
|
+
if (inThrottle) {
|
|
112
|
+
return lastPromise;
|
|
113
|
+
}
|
|
114
|
+
inThrottle = true;
|
|
115
|
+
lastPromise = func(...args);
|
|
116
|
+
setTimeout(() => {
|
|
117
|
+
inThrottle = false;
|
|
118
|
+
}, interval);
|
|
119
|
+
return lastPromise;
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Execute async operations sequentially with progress tracking
|
|
124
|
+
*/
|
|
125
|
+
async function sequentialWithProgress(operations, onProgress) {
|
|
126
|
+
const results = [];
|
|
127
|
+
for (let i = 0; i < operations.length; i++) {
|
|
128
|
+
const result = await operations[i]();
|
|
129
|
+
results.push(result);
|
|
130
|
+
onProgress?.(i + 1, operations.length);
|
|
131
|
+
}
|
|
132
|
+
return results;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Batch async operations
|
|
136
|
+
*/
|
|
137
|
+
async function batchAsync(items, batchSize, processor) {
|
|
138
|
+
for (let i = 0; i < items.length; i += batchSize) {
|
|
139
|
+
const batch = items.slice(i, i + batchSize);
|
|
140
|
+
await processor(batch);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create a cancellable async operation
|
|
145
|
+
*/
|
|
146
|
+
function createCancellableAsync(operation) {
|
|
147
|
+
let abortController = null;
|
|
148
|
+
return {
|
|
149
|
+
execute: async () => {
|
|
150
|
+
abortController = new AbortController();
|
|
151
|
+
return await operation(abortController.signal);
|
|
152
|
+
},
|
|
153
|
+
cancel: () => {
|
|
154
|
+
abortController?.abort();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Timeout wrapper for async operations
|
|
160
|
+
*/
|
|
161
|
+
async function withTimeout(operation, timeoutMs, timeoutMessage) {
|
|
162
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
163
|
+
setTimeout(() => {
|
|
164
|
+
reject(new Error(timeoutMessage || `Operation timed out after ${timeoutMs}ms`));
|
|
165
|
+
}, timeoutMs);
|
|
166
|
+
});
|
|
167
|
+
return Promise.race([operation, timeoutPromise]);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Execute async operation with loading state
|
|
171
|
+
*/
|
|
172
|
+
async function withLoadingState(operation, setLoading) {
|
|
173
|
+
setLoading(true);
|
|
174
|
+
try {
|
|
175
|
+
return await operation();
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
setLoading(false);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Create a promise that resolves after a delay
|
|
183
|
+
*/
|
|
184
|
+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
185
|
+
exports.delay = delay;
|
|
186
|
+
/**
|
|
187
|
+
* Execute async operation with retry on specific errors
|
|
188
|
+
*/
|
|
189
|
+
async function retryOnError(operation, retryableErrors, maxRetries = 3) {
|
|
190
|
+
return retryAsync(operation, maxRetries, 1000, (error) => {
|
|
191
|
+
const errorCode = error?.code || error?.status || error?.message;
|
|
192
|
+
return retryableErrors.includes(errorCode);
|
|
193
|
+
});
|
|
194
|
+
}
|