@oxyhq/services 5.13.1 → 5.13.2
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 +71 -0
- package/lib/commonjs/core/HttpClient.js +238 -0
- package/lib/commonjs/core/HttpClient.js.map +1 -0
- package/lib/commonjs/core/OxyServices.js +530 -332
- package/lib/commonjs/core/OxyServices.js.map +1 -1
- package/lib/commonjs/core/RequestManager.js +199 -0
- package/lib/commonjs/core/RequestManager.js.map +1 -0
- package/lib/commonjs/core/index.js +38 -1
- package/lib/commonjs/core/index.js.map +1 -1
- package/lib/commonjs/index.js +36 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/ui/components/Avatar.js +94 -27
- package/lib/commonjs/ui/components/Avatar.js.map +1 -1
- package/lib/commonjs/ui/components/FollowButton.js +1 -0
- package/lib/commonjs/ui/components/FollowButton.js.map +1 -1
- package/lib/commonjs/ui/components/internal/TextField.js +13 -8
- package/lib/commonjs/ui/components/internal/TextField.js.map +1 -1
- package/lib/commonjs/ui/context/OxyContext.js +183 -224
- package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
- package/lib/commonjs/ui/hooks/useSessionSocket.js +80 -22
- package/lib/commonjs/ui/hooks/useSessionSocket.js.map +1 -1
- package/lib/commonjs/ui/index.js +4 -1
- package/lib/commonjs/ui/index.js.map +1 -1
- package/lib/commonjs/ui/screens/AccountSettingsScreen.js +32 -2
- package/lib/commonjs/ui/screens/AccountSettingsScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/AccountSwitcherScreen.js +101 -59
- package/lib/commonjs/ui/screens/AccountSwitcherScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/FileManagementScreen.js +3 -2
- package/lib/commonjs/ui/screens/FileManagementScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/LanguageSelectorScreen.js +75 -117
- package/lib/commonjs/ui/screens/LanguageSelectorScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/SignInScreen.js +0 -11
- package/lib/commonjs/ui/screens/SignInScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/SignUpScreen.js +14 -16
- package/lib/commonjs/ui/screens/SignUpScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/WelcomeNewUserScreen.js +50 -18
- package/lib/commonjs/ui/screens/WelcomeNewUserScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/internal/SignInPasswordStep.js +10 -10
- package/lib/commonjs/ui/screens/internal/SignInPasswordStep.js.map +1 -1
- package/lib/commonjs/ui/screens/steps/SignInPasswordStep.js +16 -26
- package/lib/commonjs/ui/screens/steps/SignInPasswordStep.js.map +1 -1
- package/lib/commonjs/ui/screens/steps/SignInUsernameStep.js +104 -212
- package/lib/commonjs/ui/screens/steps/SignInUsernameStep.js.map +1 -1
- package/lib/commonjs/ui/stores/accountStore.js +237 -0
- package/lib/commonjs/ui/stores/accountStore.js.map +1 -0
- package/lib/commonjs/ui/stores/authStore.js +2 -1
- package/lib/commonjs/ui/stores/authStore.js.map +1 -1
- package/lib/commonjs/ui/styles/authStyles.js +14 -7
- package/lib/commonjs/ui/styles/authStyles.js.map +1 -1
- package/lib/commonjs/utils/asyncUtils.js +9 -22
- package/lib/commonjs/utils/asyncUtils.js.map +1 -1
- package/lib/commonjs/utils/cache.js +259 -0
- package/lib/commonjs/utils/cache.js.map +1 -0
- package/lib/commonjs/utils/index.js +99 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/languageUtils.js +159 -0
- package/lib/commonjs/utils/languageUtils.js.map +1 -0
- package/lib/commonjs/utils/requestUtils.js +217 -0
- package/lib/commonjs/utils/requestUtils.js.map +1 -0
- package/lib/commonjs/utils/sessionUtils.js +191 -0
- package/lib/commonjs/utils/sessionUtils.js.map +1 -0
- package/lib/module/core/HttpClient.js +232 -0
- package/lib/module/core/HttpClient.js.map +1 -0
- package/lib/module/core/OxyServices.js +528 -326
- package/lib/module/core/OxyServices.js.map +1 -1
- package/lib/module/core/RequestManager.js +194 -0
- package/lib/module/core/RequestManager.js.map +1 -0
- package/lib/module/core/index.js +2 -0
- package/lib/module/core/index.js.map +1 -1
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/ui/components/Avatar.js +94 -27
- package/lib/module/ui/components/Avatar.js.map +1 -1
- package/lib/module/ui/components/FollowButton.js +1 -0
- package/lib/module/ui/components/FollowButton.js.map +1 -1
- package/lib/module/ui/components/internal/TextField.js +13 -8
- package/lib/module/ui/components/internal/TextField.js.map +1 -1
- package/lib/module/ui/context/OxyContext.js +182 -223
- package/lib/module/ui/context/OxyContext.js.map +1 -1
- package/lib/module/ui/hooks/useSessionSocket.js +80 -22
- package/lib/module/ui/hooks/useSessionSocket.js.map +1 -1
- package/lib/module/ui/index.js +4 -2
- package/lib/module/ui/index.js.map +1 -1
- package/lib/module/ui/screens/AccountSettingsScreen.js +33 -2
- package/lib/module/ui/screens/AccountSettingsScreen.js.map +1 -1
- package/lib/module/ui/screens/AccountSwitcherScreen.js +102 -60
- package/lib/module/ui/screens/AccountSwitcherScreen.js.map +1 -1
- package/lib/module/ui/screens/FileManagementScreen.js +3 -2
- package/lib/module/ui/screens/FileManagementScreen.js.map +1 -1
- package/lib/module/ui/screens/LanguageSelectorScreen.js +73 -117
- package/lib/module/ui/screens/LanguageSelectorScreen.js.map +1 -1
- package/lib/module/ui/screens/SignInScreen.js +0 -11
- package/lib/module/ui/screens/SignInScreen.js.map +1 -1
- package/lib/module/ui/screens/SignUpScreen.js +14 -16
- package/lib/module/ui/screens/SignUpScreen.js.map +1 -1
- package/lib/module/ui/screens/WelcomeNewUserScreen.js +50 -18
- package/lib/module/ui/screens/WelcomeNewUserScreen.js.map +1 -1
- package/lib/module/ui/screens/internal/SignInPasswordStep.js +10 -10
- package/lib/module/ui/screens/internal/SignInPasswordStep.js.map +1 -1
- package/lib/module/ui/screens/steps/SignInPasswordStep.js +16 -26
- package/lib/module/ui/screens/steps/SignInPasswordStep.js.map +1 -1
- package/lib/module/ui/screens/steps/SignInUsernameStep.js +105 -214
- package/lib/module/ui/screens/steps/SignInUsernameStep.js.map +1 -1
- package/lib/module/ui/stores/accountStore.js +229 -0
- package/lib/module/ui/stores/accountStore.js.map +1 -0
- package/lib/module/ui/stores/authStore.js +2 -1
- package/lib/module/ui/stores/authStore.js.map +1 -1
- package/lib/module/ui/styles/authStyles.js +14 -7
- package/lib/module/ui/styles/authStyles.js.map +1 -1
- package/lib/module/utils/asyncUtils.js +10 -22
- package/lib/module/utils/asyncUtils.js.map +1 -1
- package/lib/module/utils/cache.js +250 -0
- package/lib/module/utils/cache.js.map +1 -0
- package/lib/module/utils/index.js +7 -0
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/languageUtils.js +151 -0
- package/lib/module/utils/languageUtils.js.map +1 -0
- package/lib/module/utils/requestUtils.js +210 -0
- package/lib/module/utils/requestUtils.js.map +1 -0
- package/lib/module/utils/sessionUtils.js +180 -0
- package/lib/module/utils/sessionUtils.js.map +1 -0
- package/lib/typescript/core/HttpClient.d.ts +64 -0
- package/lib/typescript/core/HttpClient.d.ts.map +1 -0
- package/lib/typescript/core/OxyServices.d.ts +82 -71
- package/lib/typescript/core/OxyServices.d.ts.map +1 -1
- package/lib/typescript/core/RequestManager.d.ts +67 -0
- package/lib/typescript/core/RequestManager.d.ts.map +1 -0
- package/lib/typescript/core/index.d.ts +2 -0
- package/lib/typescript/core/index.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +2 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/models/interfaces.d.ts +15 -0
- package/lib/typescript/models/interfaces.d.ts.map +1 -1
- package/lib/typescript/models/session.d.ts +1 -0
- package/lib/typescript/models/session.d.ts.map +1 -1
- package/lib/typescript/ui/components/Avatar.d.ts +6 -7
- package/lib/typescript/ui/components/Avatar.d.ts.map +1 -1
- package/lib/typescript/ui/components/FollowButton.d.ts.map +1 -1
- package/lib/typescript/ui/components/internal/TextField.d.ts.map +1 -1
- package/lib/typescript/ui/context/OxyContext.d.ts +4 -0
- package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/useSessionSocket.d.ts.map +1 -1
- package/lib/typescript/ui/index.d.ts +2 -2
- package/lib/typescript/ui/index.d.ts.map +1 -1
- package/lib/typescript/ui/screens/AccountSettingsScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/AccountSwitcherScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/LanguageSelectorScreen.d.ts +3 -3
- package/lib/typescript/ui/screens/LanguageSelectorScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/SignInScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/SignUpScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/WelcomeNewUserScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/internal/SignInPasswordStep.d.ts.map +1 -1
- package/lib/typescript/ui/screens/steps/SignInPasswordStep.d.ts.map +1 -1
- package/lib/typescript/ui/screens/steps/SignInUsernameStep.d.ts.map +1 -1
- package/lib/typescript/ui/stores/accountStore.d.ts +34 -0
- package/lib/typescript/ui/stores/accountStore.d.ts.map +1 -0
- package/lib/typescript/ui/stores/authStore.d.ts.map +1 -1
- package/lib/typescript/ui/styles/authStyles.d.ts +18 -2
- package/lib/typescript/ui/styles/authStyles.d.ts.map +1 -1
- package/lib/typescript/utils/asyncUtils.d.ts +2 -0
- package/lib/typescript/utils/asyncUtils.d.ts.map +1 -1
- package/lib/typescript/utils/cache.d.ts +128 -0
- package/lib/typescript/utils/cache.d.ts.map +1 -0
- package/lib/typescript/utils/index.d.ts +4 -0
- package/lib/typescript/utils/index.d.ts.map +1 -1
- package/lib/typescript/utils/languageUtils.d.ts +38 -0
- package/lib/typescript/utils/languageUtils.d.ts.map +1 -0
- package/lib/typescript/utils/requestUtils.d.ts +122 -0
- package/lib/typescript/utils/requestUtils.d.ts.map +1 -0
- package/lib/typescript/utils/sessionUtils.d.ts +55 -0
- package/lib/typescript/utils/sessionUtils.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/core/HttpClient.ts +277 -0
- package/src/core/OxyServices.ts +458 -351
- package/src/core/RequestManager.ts +240 -0
- package/src/core/index.ts +10 -0
- package/src/index.ts +10 -0
- package/src/models/interfaces.ts +19 -0
- package/src/models/session.ts +1 -1
- package/src/ui/components/Avatar.tsx +151 -35
- package/src/ui/components/FollowButton.tsx +1 -0
- package/src/ui/components/internal/TextField.tsx +7 -6
- package/src/ui/context/OxyContext.tsx +213 -217
- package/src/ui/hooks/useSessionSocket.ts +72 -18
- package/src/ui/index.ts +4 -1
- package/src/ui/screens/AccountSettingsScreen.tsx +34 -2
- package/src/ui/screens/AccountSwitcherScreen.tsx +102 -68
- package/src/ui/screens/FileManagementScreen.tsx +1 -1
- package/src/ui/screens/LanguageSelectorScreen.tsx +86 -143
- package/src/ui/screens/SignInScreen.tsx +0 -7
- package/src/ui/screens/SignUpScreen.tsx +14 -15
- package/src/ui/screens/WelcomeNewUserScreen.tsx +52 -15
- package/src/ui/screens/internal/SignInPasswordStep.tsx +4 -6
- package/src/ui/screens/steps/SignInPasswordStep.tsx +4 -8
- package/src/ui/screens/steps/SignInUsernameStep.tsx +110 -256
- package/src/ui/stores/accountStore.ts +285 -0
- package/src/ui/stores/authStore.ts +2 -1
- package/src/ui/styles/authStyles.ts +14 -7
- package/src/utils/asyncUtils.ts +10 -24
- package/src/utils/cache.ts +264 -0
- package/src/utils/index.ts +19 -0
- package/src/utils/languageUtils.ts +174 -0
- package/src/utils/requestUtils.ts +234 -0
- package/src/utils/sessionUtils.ts +206 -0
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ A comprehensive TypeScript client library for the Oxy API providing authenticati
|
|
|
15
15
|
- [Configuration](#configuration)
|
|
16
16
|
- [Authentication](#authentication)
|
|
17
17
|
- [UI Components](#ui-components)
|
|
18
|
+
- [Internationalization (i18n)](#internationalization-i18n)
|
|
18
19
|
- [Troubleshooting](#troubleshooting)
|
|
19
20
|
- [Requirements](#requirements)
|
|
20
21
|
|
|
@@ -569,6 +570,76 @@ function MyComponent() {
|
|
|
569
570
|
}
|
|
570
571
|
```
|
|
571
572
|
|
|
573
|
+
## 🌍 Internationalization (i18n)
|
|
574
|
+
|
|
575
|
+
OxyHQ Services includes built-in language selection and storage. The selected language can be accessed and synced with your app's i18n system.
|
|
576
|
+
|
|
577
|
+
### Getting Current Language
|
|
578
|
+
|
|
579
|
+
```typescript
|
|
580
|
+
import { useOxy } from '@oxyhq/services';
|
|
581
|
+
|
|
582
|
+
function MyComponent() {
|
|
583
|
+
const {
|
|
584
|
+
currentLanguage, // 'en-US'
|
|
585
|
+
currentLanguageName, // 'English'
|
|
586
|
+
currentNativeLanguageName, // 'Español' (if Spanish is selected)
|
|
587
|
+
currentLanguageMetadata // Full metadata object
|
|
588
|
+
} = useOxy();
|
|
589
|
+
|
|
590
|
+
return <Text>Current language: {currentLanguageName}</Text>;
|
|
591
|
+
}
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
### Syncing with Your i18n Library
|
|
595
|
+
|
|
596
|
+
To integrate with react-i18next, i18n-js, next-intl, or other i18n libraries, see the comprehensive guide:
|
|
597
|
+
|
|
598
|
+
📖 **[Complete i18n Integration Guide](./I18N_INTEGRATION.md)**
|
|
599
|
+
|
|
600
|
+
This guide includes:
|
|
601
|
+
- Step-by-step integration with popular i18n libraries
|
|
602
|
+
- Bidirectional sync between services and your i18n system
|
|
603
|
+
- Language code format conversion utilities
|
|
604
|
+
- Complete working examples
|
|
605
|
+
|
|
606
|
+
### Using OxyServices (Non-React)
|
|
607
|
+
|
|
608
|
+
```typescript
|
|
609
|
+
import { OxyServices } from '@oxyhq/services/core';
|
|
610
|
+
|
|
611
|
+
const oxy = new OxyServices({ baseURL: 'https://api.oxy.so' });
|
|
612
|
+
|
|
613
|
+
// Get current language code
|
|
614
|
+
const languageCode = await oxy.getCurrentLanguage();
|
|
615
|
+
|
|
616
|
+
// Get language name
|
|
617
|
+
const languageName = await oxy.getCurrentLanguageName();
|
|
618
|
+
|
|
619
|
+
// Get full metadata
|
|
620
|
+
const metadata = await oxy.getCurrentLanguageMetadata();
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
### Language Utilities
|
|
624
|
+
|
|
625
|
+
```typescript
|
|
626
|
+
import {
|
|
627
|
+
SUPPORTED_LANGUAGES,
|
|
628
|
+
getLanguageName,
|
|
629
|
+
getLanguageMetadata
|
|
630
|
+
} from '@oxyhq/services';
|
|
631
|
+
|
|
632
|
+
// Get all supported languages
|
|
633
|
+
const languages = SUPPORTED_LANGUAGES;
|
|
634
|
+
|
|
635
|
+
// Get language name from code
|
|
636
|
+
const name = getLanguageName('es-ES'); // 'Spanish'
|
|
637
|
+
|
|
638
|
+
// Get full metadata
|
|
639
|
+
const metadata = getLanguageMetadata('es-ES');
|
|
640
|
+
// { id: 'es-ES', name: 'Spanish', nativeName: 'Español', flag: '🇪🇸', ... }
|
|
641
|
+
```
|
|
642
|
+
|
|
572
643
|
## 🛠️ Troubleshooting
|
|
573
644
|
|
|
574
645
|
### Common Issues
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.HttpClient = void 0;
|
|
7
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
8
|
+
var _jwtDecode = require("jwt-decode");
|
|
9
|
+
var _errorUtils = require("../utils/errorUtils");
|
|
10
|
+
var _requestUtils = require("../utils/requestUtils");
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
/**
|
|
13
|
+
* HTTP Client Service
|
|
14
|
+
*
|
|
15
|
+
* Handles all HTTP communication with authentication, interceptors, and error handling.
|
|
16
|
+
* This is the single source of truth for making authenticated HTTP requests.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Token store for authentication
|
|
21
|
+
*/
|
|
22
|
+
class TokenStore {
|
|
23
|
+
accessToken = null;
|
|
24
|
+
refreshToken = null;
|
|
25
|
+
constructor() {}
|
|
26
|
+
static getInstance() {
|
|
27
|
+
if (!TokenStore.instance) {
|
|
28
|
+
TokenStore.instance = new TokenStore();
|
|
29
|
+
}
|
|
30
|
+
return TokenStore.instance;
|
|
31
|
+
}
|
|
32
|
+
setTokens(accessToken, refreshToken = '') {
|
|
33
|
+
this.accessToken = accessToken;
|
|
34
|
+
this.refreshToken = refreshToken;
|
|
35
|
+
}
|
|
36
|
+
getAccessToken() {
|
|
37
|
+
return this.accessToken;
|
|
38
|
+
}
|
|
39
|
+
getRefreshToken() {
|
|
40
|
+
return this.refreshToken;
|
|
41
|
+
}
|
|
42
|
+
clearTokens() {
|
|
43
|
+
this.accessToken = null;
|
|
44
|
+
this.refreshToken = null;
|
|
45
|
+
}
|
|
46
|
+
hasAccessToken() {
|
|
47
|
+
return !!this.accessToken;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* HTTP Client Service
|
|
53
|
+
*
|
|
54
|
+
* Manages Axios instance with authentication interceptors.
|
|
55
|
+
* All HTTP requests should go through this service to ensure authentication.
|
|
56
|
+
*/
|
|
57
|
+
class HttpClient {
|
|
58
|
+
constructor(config) {
|
|
59
|
+
this.baseURL = config.baseURL;
|
|
60
|
+
this.tokenStore = TokenStore.getInstance();
|
|
61
|
+
this.logger = new _requestUtils.SimpleLogger(config.enableLogging || false, config.logLevel || 'error', 'HttpClient');
|
|
62
|
+
const timeout = config.requestTimeout || 5000;
|
|
63
|
+
|
|
64
|
+
// Create Axios instance with optimized configuration
|
|
65
|
+
this.client = _axios.default.create({
|
|
66
|
+
baseURL: config.baseURL,
|
|
67
|
+
timeout,
|
|
68
|
+
headers: {
|
|
69
|
+
'Accept': 'application/json'
|
|
70
|
+
},
|
|
71
|
+
// Enable HTTP keep-alive for connection reuse (Node.js only)
|
|
72
|
+
...(typeof process !== 'undefined' && process.env && typeof window === 'undefined' && typeof require !== 'undefined' ? {
|
|
73
|
+
httpAgent: new (require('http').Agent)({
|
|
74
|
+
keepAlive: true,
|
|
75
|
+
keepAliveMsecs: 1000,
|
|
76
|
+
maxSockets: 50
|
|
77
|
+
}),
|
|
78
|
+
httpsAgent: new (require('https').Agent)({
|
|
79
|
+
keepAlive: true,
|
|
80
|
+
keepAliveMsecs: 1000,
|
|
81
|
+
maxSockets: 50
|
|
82
|
+
})
|
|
83
|
+
} : {})
|
|
84
|
+
});
|
|
85
|
+
this.setupInterceptors();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Setup axios interceptors for authentication and error handling
|
|
90
|
+
*/
|
|
91
|
+
setupInterceptors() {
|
|
92
|
+
// Request interceptor: Add authentication header
|
|
93
|
+
this.client.interceptors.request.use(async req => {
|
|
94
|
+
const accessToken = this.tokenStore.getAccessToken();
|
|
95
|
+
if (!accessToken) {
|
|
96
|
+
return req;
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
const decoded = (0, _jwtDecode.jwtDecode)(accessToken);
|
|
100
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
|
101
|
+
|
|
102
|
+
// If token expires in less than 60 seconds, refresh it
|
|
103
|
+
if (decoded.exp && decoded.exp - currentTime < 60) {
|
|
104
|
+
if (decoded.sessionId) {
|
|
105
|
+
try {
|
|
106
|
+
// Create a new axios instance to avoid interceptor recursion
|
|
107
|
+
const refreshClient = _axios.default.create({
|
|
108
|
+
baseURL: this.client.defaults.baseURL,
|
|
109
|
+
timeout: this.client.defaults.timeout
|
|
110
|
+
});
|
|
111
|
+
const res = await refreshClient.get(`/api/session/token/${decoded.sessionId}`);
|
|
112
|
+
this.tokenStore.setTokens(res.data.accessToken);
|
|
113
|
+
req.headers.Authorization = `Bearer ${res.data.accessToken}`;
|
|
114
|
+
this.logger.debug('Token refreshed');
|
|
115
|
+
} catch (refreshError) {
|
|
116
|
+
// If refresh fails, use current token anyway
|
|
117
|
+
req.headers.Authorization = `Bearer ${accessToken}`;
|
|
118
|
+
this.logger.warn('Token refresh failed, using current token');
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
req.headers.Authorization = `Bearer ${accessToken}`;
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
req.headers.Authorization = `Bearer ${accessToken}`;
|
|
125
|
+
}
|
|
126
|
+
} catch (error) {
|
|
127
|
+
this.logger.error('Error processing token:', error);
|
|
128
|
+
// Even if there's an error, still try to use the token
|
|
129
|
+
req.headers.Authorization = `Bearer ${accessToken}`;
|
|
130
|
+
}
|
|
131
|
+
return req;
|
|
132
|
+
}, error => {
|
|
133
|
+
this.logger.error('Request interceptor error:', error);
|
|
134
|
+
return Promise.reject(error);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Response interceptor: Handle auth errors
|
|
138
|
+
this.client.interceptors.response.use(response => response, error => {
|
|
139
|
+
if (error.response?.status === 401) {
|
|
140
|
+
this.logger.warn('401 Unauthorized, clearing tokens');
|
|
141
|
+
this.tokenStore.clearTokens();
|
|
142
|
+
}
|
|
143
|
+
return Promise.reject(error);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Get the underlying Axios instance
|
|
149
|
+
* Use this only when you need direct access to Axios features
|
|
150
|
+
*/
|
|
151
|
+
getAxiosInstance() {
|
|
152
|
+
return this.client;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Make a raw HTTP request (no caching, deduplication, etc.)
|
|
157
|
+
* Use this for requests that need to bypass performance features
|
|
158
|
+
*/
|
|
159
|
+
async request(config) {
|
|
160
|
+
try {
|
|
161
|
+
const response = await this.client.request({
|
|
162
|
+
method: config.method,
|
|
163
|
+
url: config.url,
|
|
164
|
+
data: config.data,
|
|
165
|
+
params: config.params,
|
|
166
|
+
timeout: config.timeout,
|
|
167
|
+
signal: config.signal
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Unwrap standardized API response format: { data: ... }
|
|
171
|
+
// This handles responses from sendSuccess() and sendPaginated() helpers
|
|
172
|
+
const responseData = response.data;
|
|
173
|
+
|
|
174
|
+
// Handle paginated responses: { data: [...], pagination: {...} }
|
|
175
|
+
// Return the data array directly - the calling method will wrap it appropriately
|
|
176
|
+
if (responseData && typeof responseData === 'object' && 'data' in responseData && 'pagination' in responseData) {
|
|
177
|
+
// For paginated responses, return the data array directly
|
|
178
|
+
// The calling methods like getUserFollowers/getUserFollowing will handle wrapping
|
|
179
|
+
// We return the whole response so methods can access both data and pagination
|
|
180
|
+
return responseData;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Handle regular success responses: { data: ... }
|
|
184
|
+
if (responseData && typeof responseData === 'object' && 'data' in responseData) {
|
|
185
|
+
return responseData.data;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Return as-is for responses that don't use sendSuccess wrapper
|
|
189
|
+
return responseData;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw (0, _errorUtils.handleHttpError)(error);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Get base URL
|
|
197
|
+
*/
|
|
198
|
+
getBaseURL() {
|
|
199
|
+
return this.baseURL;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Set authentication tokens
|
|
204
|
+
*/
|
|
205
|
+
setTokens(accessToken, refreshToken = '') {
|
|
206
|
+
this.tokenStore.setTokens(accessToken, refreshToken);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Clear authentication tokens
|
|
211
|
+
*/
|
|
212
|
+
clearTokens() {
|
|
213
|
+
this.tokenStore.clearTokens();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Get access token
|
|
218
|
+
*/
|
|
219
|
+
getAccessToken() {
|
|
220
|
+
return this.tokenStore.getAccessToken();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Check if has access token
|
|
225
|
+
*/
|
|
226
|
+
hasAccessToken() {
|
|
227
|
+
return this.tokenStore.hasAccessToken();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Test-only utility to reset global tokens between jest tests
|
|
231
|
+
static __resetTokensForTests() {
|
|
232
|
+
try {
|
|
233
|
+
TokenStore.getInstance().clearTokens();
|
|
234
|
+
} catch {}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
exports.HttpClient = HttpClient;
|
|
238
|
+
//# sourceMappingURL=HttpClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_axios","_interopRequireDefault","require","_jwtDecode","_errorUtils","_requestUtils","e","__esModule","default","TokenStore","accessToken","refreshToken","constructor","getInstance","instance","setTokens","getAccessToken","getRefreshToken","clearTokens","hasAccessToken","HttpClient","config","baseURL","tokenStore","logger","SimpleLogger","enableLogging","logLevel","timeout","requestTimeout","client","axios","create","headers","process","env","window","httpAgent","Agent","keepAlive","keepAliveMsecs","maxSockets","httpsAgent","setupInterceptors","interceptors","request","use","req","decoded","jwtDecode","currentTime","Math","floor","Date","now","exp","sessionId","refreshClient","defaults","res","get","data","Authorization","debug","refreshError","warn","error","Promise","reject","response","status","getAxiosInstance","method","url","params","signal","responseData","handleHttpError","getBaseURL","__resetTokensForTests","exports"],"sourceRoot":"../../../src","sources":["core/HttpClient.ts"],"mappings":";;;;;;AAOA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,UAAA,GAAAD,OAAA;AAEA,IAAAE,WAAA,GAAAF,OAAA;AACA,IAAAG,aAAA,GAAAH,OAAA;AAAqD,SAAAD,uBAAAK,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAXrD;AACA;A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