@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,263 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OxyServicesBase = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* OxyServices Base Class
|
|
6
|
+
*
|
|
7
|
+
* Contains core infrastructure, HTTP client, request management, and error handling
|
|
8
|
+
*/
|
|
9
|
+
const jwt_decode_1 = require("jwt-decode");
|
|
10
|
+
const errorUtils_1 = require("./utils/errorUtils");
|
|
11
|
+
const HttpService_1 = require("./HttpService");
|
|
12
|
+
const OxyServices_errors_1 = require("./OxyServices.errors");
|
|
13
|
+
/**
|
|
14
|
+
* Base class for OxyServices with core infrastructure
|
|
15
|
+
*/
|
|
16
|
+
class OxyServicesBase {
|
|
17
|
+
constructor(...args) {
|
|
18
|
+
const config = args[0];
|
|
19
|
+
if (!config || typeof config !== 'object') {
|
|
20
|
+
throw new Error('OxyConfig is required');
|
|
21
|
+
}
|
|
22
|
+
this.config = config;
|
|
23
|
+
this.cloudURL = config.cloudURL || 'https://cloud.oxy.so';
|
|
24
|
+
// Initialize unified HTTP service (handles auth, caching, deduplication, queuing, retry)
|
|
25
|
+
this.httpService = new HttpService_1.HttpService(config);
|
|
26
|
+
}
|
|
27
|
+
// Test-only utility to reset global tokens between jest tests
|
|
28
|
+
static __resetTokensForTests() {
|
|
29
|
+
HttpService_1.HttpService.__resetTokensForTests();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Make a request with all performance optimizations
|
|
33
|
+
* This is the main method for all API calls - ensures authentication and performance features
|
|
34
|
+
*/
|
|
35
|
+
async makeRequest(method, url, data, options = {}) {
|
|
36
|
+
return this.httpService.request({
|
|
37
|
+
method,
|
|
38
|
+
url,
|
|
39
|
+
data: method !== 'GET' ? data : undefined,
|
|
40
|
+
params: method === 'GET' ? data : undefined,
|
|
41
|
+
...options,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
// ============================================================================
|
|
45
|
+
// CORE METHODS (HTTP Client, Token Management, Error Handling)
|
|
46
|
+
// ============================================================================
|
|
47
|
+
/**
|
|
48
|
+
* Get the configured Oxy API base URL
|
|
49
|
+
*/
|
|
50
|
+
getBaseURL() {
|
|
51
|
+
return this.httpService.getBaseURL();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get the HTTP service instance
|
|
55
|
+
* Useful for advanced use cases where direct access to the HTTP service is needed
|
|
56
|
+
*/
|
|
57
|
+
getClient() {
|
|
58
|
+
return this.httpService;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get performance metrics
|
|
62
|
+
*/
|
|
63
|
+
getMetrics() {
|
|
64
|
+
return this.httpService.getMetrics();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Clear request cache
|
|
68
|
+
*/
|
|
69
|
+
clearCache() {
|
|
70
|
+
this.httpService.clearCache();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Clear specific cache entry
|
|
74
|
+
*/
|
|
75
|
+
clearCacheEntry(key) {
|
|
76
|
+
this.httpService.clearCacheEntry(key);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get cache statistics
|
|
80
|
+
*/
|
|
81
|
+
getCacheStats() {
|
|
82
|
+
return this.httpService.getCacheStats();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get the configured Oxy Cloud (file storage/CDN) URL
|
|
86
|
+
*/
|
|
87
|
+
getCloudURL() {
|
|
88
|
+
return this.cloudURL;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Set authentication tokens
|
|
92
|
+
*/
|
|
93
|
+
setTokens(accessToken, refreshToken = '') {
|
|
94
|
+
this.httpService.setTokens(accessToken, refreshToken);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Clear stored authentication tokens
|
|
98
|
+
*/
|
|
99
|
+
clearTokens() {
|
|
100
|
+
this.httpService.clearTokens();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the current user ID from the access token
|
|
104
|
+
*/
|
|
105
|
+
getCurrentUserId() {
|
|
106
|
+
const accessToken = this.httpService.getAccessToken();
|
|
107
|
+
if (!accessToken) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const decoded = (0, jwt_decode_1.jwtDecode)(accessToken);
|
|
112
|
+
return decoded.userId || decoded.id || null;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Check if the client has a valid access token (public method)
|
|
120
|
+
*/
|
|
121
|
+
hasValidToken() {
|
|
122
|
+
return this.httpService.hasAccessToken();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Get the raw access token (for constructing anchor URLs when needed)
|
|
126
|
+
*/
|
|
127
|
+
getAccessToken() {
|
|
128
|
+
return this.httpService.getAccessToken();
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Wait for authentication to be ready
|
|
132
|
+
*
|
|
133
|
+
* Optimized for high-scale usage with immediate synchronous check and adaptive polling.
|
|
134
|
+
* Returns immediately if token is already available (0ms delay), otherwise uses
|
|
135
|
+
* adaptive polling that starts fast (50ms) and gradually increases to reduce CPU usage.
|
|
136
|
+
*
|
|
137
|
+
* @param timeoutMs Maximum time to wait in milliseconds (default: 5000ms)
|
|
138
|
+
* @returns Promise that resolves to true if authentication is ready, false if timeout
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const isReady = await oxyServices.waitForAuth(3000);
|
|
143
|
+
* if (isReady) {
|
|
144
|
+
* // Proceed with authenticated operations
|
|
145
|
+
* }
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
async waitForAuth(timeoutMs = 5000) {
|
|
149
|
+
// Immediate synchronous check - no delay if token is ready
|
|
150
|
+
if (this.httpService.hasAccessToken()) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
const startTime = performance.now();
|
|
154
|
+
const maxTime = startTime + timeoutMs;
|
|
155
|
+
// Adaptive polling: start fast, then slow down to reduce CPU usage
|
|
156
|
+
let pollInterval = 50; // Start with 50ms
|
|
157
|
+
while (performance.now() < maxTime) {
|
|
158
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
159
|
+
if (this.httpService.hasAccessToken()) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
// Increase interval after first few checks (adaptive polling)
|
|
163
|
+
// This reduces CPU usage for long waits while maintaining responsiveness
|
|
164
|
+
if (pollInterval < 200) {
|
|
165
|
+
pollInterval = Math.min(pollInterval * 1.5, 200);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Execute a function with automatic authentication retry logic
|
|
172
|
+
* This handles the common case where API calls are made before authentication completes
|
|
173
|
+
*/
|
|
174
|
+
async withAuthRetry(operation, operationName, options = {}) {
|
|
175
|
+
const { maxRetries = 2, retryDelay = 1000, authTimeoutMs = 5000 } = options;
|
|
176
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
177
|
+
try {
|
|
178
|
+
// First attempt: check if we have a token
|
|
179
|
+
if (!this.httpService.hasAccessToken()) {
|
|
180
|
+
if (attempt === 0) {
|
|
181
|
+
// On first attempt, wait briefly for authentication to complete
|
|
182
|
+
const authReady = await this.waitForAuth(authTimeoutMs);
|
|
183
|
+
if (!authReady) {
|
|
184
|
+
throw new OxyServices_errors_1.OxyAuthenticationTimeoutError(operationName, authTimeoutMs);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
// On retry attempts, fail immediately if no token
|
|
189
|
+
throw new OxyServices_errors_1.OxyAuthenticationError(`Authentication required: ${operationName} requires a valid access token.`, 'AUTH_REQUIRED');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// Execute the operation
|
|
193
|
+
return await operation();
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
const isLastAttempt = attempt === maxRetries;
|
|
197
|
+
const errorObj = error && typeof error === 'object' ? error : null;
|
|
198
|
+
const isAuthError = errorObj?.response?.status === 401 ||
|
|
199
|
+
errorObj?.code === 'MISSING_TOKEN' ||
|
|
200
|
+
errorObj?.message?.includes('Authentication') ||
|
|
201
|
+
error instanceof OxyServices_errors_1.OxyAuthenticationError;
|
|
202
|
+
if (isAuthError && !isLastAttempt && !(error instanceof OxyServices_errors_1.OxyAuthenticationTimeoutError)) {
|
|
203
|
+
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
// If it's not an auth error, or it's the last attempt, throw the error
|
|
207
|
+
if (error instanceof OxyServices_errors_1.OxyAuthenticationError) {
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
throw this.handleError(error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// This should never be reached, but TypeScript requires it
|
|
214
|
+
throw new OxyServices_errors_1.OxyAuthenticationError(`${operationName} failed after ${maxRetries + 1} attempts`);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Validate the current access token with the server
|
|
218
|
+
*/
|
|
219
|
+
async validate() {
|
|
220
|
+
if (!this.hasValidToken()) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
const res = await this.makeRequest('GET', '/api/auth/validate', undefined, {
|
|
225
|
+
cache: false,
|
|
226
|
+
retry: false,
|
|
227
|
+
});
|
|
228
|
+
return res.valid === true;
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Centralized error handling
|
|
236
|
+
*/
|
|
237
|
+
handleError(error) {
|
|
238
|
+
const api = (0, errorUtils_1.handleHttpError)(error);
|
|
239
|
+
// Ensure we always have a non-empty message
|
|
240
|
+
const message = api.message?.trim() || 'An unexpected error occurred';
|
|
241
|
+
const err = new Error(message);
|
|
242
|
+
err.code = api.code;
|
|
243
|
+
err.status = api.status;
|
|
244
|
+
err.details = api.details;
|
|
245
|
+
return err;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Health check endpoint
|
|
249
|
+
*/
|
|
250
|
+
async healthCheck() {
|
|
251
|
+
try {
|
|
252
|
+
return await this.makeRequest('GET', '/health', undefined, {
|
|
253
|
+
cache: false,
|
|
254
|
+
retry: false,
|
|
255
|
+
timeout: 5000
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
throw this.handleError(error);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
exports.OxyServicesBase = OxyServicesBase;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OxyAuthenticationTimeoutError = exports.OxyAuthenticationError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Custom error types for better error handling
|
|
6
|
+
*/
|
|
7
|
+
class OxyAuthenticationError extends Error {
|
|
8
|
+
constructor(message, code = 'AUTH_ERROR', status = 401) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'OxyAuthenticationError';
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.status = status;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.OxyAuthenticationError = OxyAuthenticationError;
|
|
16
|
+
class OxyAuthenticationTimeoutError extends OxyAuthenticationError {
|
|
17
|
+
constructor(operationName, timeoutMs) {
|
|
18
|
+
super(`Authentication timeout (${timeoutMs}ms): ${operationName} requires user authentication. Please ensure the user is logged in before calling this method.`, 'AUTH_TIMEOUT', 408);
|
|
19
|
+
this.name = 'OxyAuthenticationTimeoutError';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.OxyAuthenticationTimeoutError = OxyAuthenticationTimeoutError;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.oxyClient = exports.OXY_API_URL = exports.OXY_CLOUD_URL = exports.OxyAuthenticationTimeoutError = exports.OxyAuthenticationError = exports.OxyServices = void 0;
|
|
4
|
+
const OxyServices_errors_1 = require("./OxyServices.errors");
|
|
5
|
+
Object.defineProperty(exports, "OxyAuthenticationError", { enumerable: true, get: function () { return OxyServices_errors_1.OxyAuthenticationError; } });
|
|
6
|
+
Object.defineProperty(exports, "OxyAuthenticationTimeoutError", { enumerable: true, get: function () { return OxyServices_errors_1.OxyAuthenticationTimeoutError; } });
|
|
7
|
+
// Import mixin composition helper
|
|
8
|
+
const mixins_1 = require("./mixins");
|
|
9
|
+
/**
|
|
10
|
+
* OxyServices - Unified client library for interacting with the Oxy API
|
|
11
|
+
*
|
|
12
|
+
* This class provides all API functionality in one simple, easy-to-use interface.
|
|
13
|
+
*
|
|
14
|
+
* ## Architecture
|
|
15
|
+
* - **HttpService**: Unified HTTP service handling authentication, caching, deduplication, queuing, and retry
|
|
16
|
+
* - **OxyServices**: Provides high-level API methods
|
|
17
|
+
*
|
|
18
|
+
* ## Mixin Composition
|
|
19
|
+
* The class is composed using TypeScript mixins for better code organization:
|
|
20
|
+
* - **Base**: Core infrastructure (HTTP client, request management, error handling)
|
|
21
|
+
* - **Auth**: Authentication and session management
|
|
22
|
+
* - **User**: User profiles, follow, notifications
|
|
23
|
+
* - **Privacy**: Blocked and restricted users
|
|
24
|
+
* - **Language**: Language detection and metadata
|
|
25
|
+
* - **Payment**: Payment processing
|
|
26
|
+
* - **Karma**: Karma system
|
|
27
|
+
* - **Assets**: File upload and asset management
|
|
28
|
+
* - **Developer**: Developer API management
|
|
29
|
+
* - **Location**: Location-based features
|
|
30
|
+
* - **Analytics**: Analytics tracking
|
|
31
|
+
* - **Devices**: Device management
|
|
32
|
+
* - **Utility**: Utility methods and Express middleware
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const oxy = new OxyServices({
|
|
37
|
+
* baseURL: 'https://api.oxy.so',
|
|
38
|
+
* cloudURL: 'https://cloud.oxy.so'
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
// Compose all mixins into the final OxyServices class
|
|
43
|
+
const OxyServicesComposed = (0, mixins_1.composeOxyServices)();
|
|
44
|
+
// Export as a named class to avoid TypeScript issues with anonymous class types
|
|
45
|
+
class OxyServices extends OxyServicesComposed {
|
|
46
|
+
constructor(config) {
|
|
47
|
+
super(config);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.OxyServices = OxyServices;
|
|
51
|
+
/**
|
|
52
|
+
* Export the default Oxy Cloud URL (for backward compatibility)
|
|
53
|
+
*/
|
|
54
|
+
exports.OXY_CLOUD_URL = 'https://cloud.oxy.so';
|
|
55
|
+
/**
|
|
56
|
+
* Export the default Oxy API URL (for documentation)
|
|
57
|
+
*/
|
|
58
|
+
exports.OXY_API_URL = (typeof process !== 'undefined' && process.env && process.env.OXY_API_URL) || 'https://api.oxy.so';
|
|
59
|
+
/**
|
|
60
|
+
* Pre-configured client instance for easy import
|
|
61
|
+
* Uses OXY_API_URL as baseURL and OXY_CLOUD_URL as cloudURL
|
|
62
|
+
*/
|
|
63
|
+
exports.oxyClient = new OxyServices({ baseURL: exports.OXY_API_URL, cloudURL: exports.OXY_CLOUD_URL });
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Package version and metadata constants
|
|
4
|
+
* This file is auto-generated to avoid runtime dependency on package.json
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.description = exports.version = exports.name = exports.packageInfo = void 0;
|
|
8
|
+
exports.packageInfo = {
|
|
9
|
+
name: "@oxyhq/services",
|
|
10
|
+
version: "5.2.1",
|
|
11
|
+
description: "Reusable OxyHQ module to handle authentication, user management, karma system and more 🚀",
|
|
12
|
+
main: "lib/commonjs/node/index.js",
|
|
13
|
+
module: "lib/module/node/index.js",
|
|
14
|
+
types: "lib/typescript/node/index.d.ts"
|
|
15
|
+
};
|
|
16
|
+
exports.name = exports.packageInfo.name, exports.version = exports.packageInfo.version, exports.description = exports.packageInfo.description;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Oxy Crypto Module
|
|
4
|
+
*
|
|
5
|
+
* Provides cryptographic identity management for the Oxy ecosystem.
|
|
6
|
+
* Handles key generation, secure storage, digital signatures, and recovery phrases.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.default = exports.RecoveryPhraseService = exports.SignatureService = exports.KeyManager = void 0;
|
|
10
|
+
// Import polyfills first - this ensures Buffer is available for bip39 and other libraries
|
|
11
|
+
require("./polyfill");
|
|
12
|
+
var keyManager_1 = require("./keyManager");
|
|
13
|
+
Object.defineProperty(exports, "KeyManager", { enumerable: true, get: function () { return keyManager_1.KeyManager; } });
|
|
14
|
+
var signatureService_1 = require("./signatureService");
|
|
15
|
+
Object.defineProperty(exports, "SignatureService", { enumerable: true, get: function () { return signatureService_1.SignatureService; } });
|
|
16
|
+
var recoveryPhrase_1 = require("./recoveryPhrase");
|
|
17
|
+
Object.defineProperty(exports, "RecoveryPhraseService", { enumerable: true, get: function () { return recoveryPhrase_1.RecoveryPhraseService; } });
|
|
18
|
+
// Re-export for convenience
|
|
19
|
+
var keyManager_2 = require("./keyManager");
|
|
20
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return keyManager_2.KeyManager; } });
|