@oxyhq/services 0.1.0 → 0.1.1
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 +144 -250
- package/dist/backend/auth.d.ts +37 -0
- package/dist/backend/auth.js +81 -0
- package/dist/backend/index.d.ts +12 -0
- package/dist/backend/index.js +20 -0
- package/dist/backend/karma.d.ts +45 -0
- package/dist/backend/karma.js +83 -0
- package/dist/backend/users.d.ts +39 -0
- package/dist/backend/users.js +87 -0
- package/dist/backend/wallet.d.ts +29 -0
- package/dist/backend/wallet.js +68 -0
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +1 -0
- package/dist/frontend/context/OxyContext.d.ts +20 -0
- package/dist/frontend/context/OxyContext.js +97 -0
- package/dist/frontend/hooks/useAuth.d.ts +19 -0
- package/dist/frontend/hooks/useAuth.js +234 -0
- package/dist/frontend/hooks/useKarma.d.ts +27 -0
- package/dist/frontend/hooks/useKarma.js +187 -0
- package/dist/frontend/hooks/useUser.d.ts +13 -0
- package/dist/frontend/hooks/useUser.js +136 -0
- package/dist/frontend/hooks/useWallet.d.ts +14 -0
- package/dist/frontend/hooks/useWallet.js +174 -0
- package/dist/frontend/index.d.ts +6 -0
- package/dist/frontend/index.js +14 -0
- package/dist/index.d.ts +3 -28
- package/dist/index.js +17 -66
- package/dist/shared/api-client.d.ts +15 -0
- package/dist/shared/api-client.js +197 -0
- package/dist/shared/types.d.ts +135 -0
- package/package.json +45 -71
- package/dist/components/AuthBottomSheet/types.d.ts +0 -24
- package/dist/components/FileSelectorModal/types.d.ts +0 -35
- package/dist/components/FileSelectorModal/types.js +0 -2
- package/dist/components/SessionProvider.d.ts +0 -26
- package/dist/components/SessionProvider.js +0 -367
- package/dist/config.d.ts +0 -169
- package/dist/config.js +0 -115
- package/dist/constants.d.ts +0 -54
- package/dist/constants.js +0 -61
- package/dist/hooks/index.d.ts +0 -11
- package/dist/hooks/index.js +0 -23
- package/dist/hooks/useAuth.d.ts +0 -6
- package/dist/hooks/useAuth.js +0 -17
- package/dist/hooks/useFiles.d.ts +0 -10
- package/dist/hooks/useFiles.js +0 -329
- package/dist/hooks/useProfile.d.ts +0 -13
- package/dist/hooks/useProfile.js +0 -154
- package/dist/hooks/useSession.d.ts +0 -2
- package/dist/hooks/useSession.js +0 -12
- package/dist/hooks/useSubscription.d.ts +0 -15
- package/dist/hooks/useSubscription.js +0 -85
- package/dist/package.json +0 -71
- package/dist/reducers/index.d.ts +0 -8
- package/dist/reducers/index.js +0 -29
- package/dist/reducers/profileReducer.d.ts +0 -55
- package/dist/reducers/profileReducer.js +0 -136
- package/dist/services/OxyClient.d.ts +0 -84
- package/dist/services/OxyClient.js +0 -309
- package/dist/services/api.service.d.ts +0 -61
- package/dist/services/api.service.js +0 -289
- package/dist/services/auth.service.d.ts +0 -87
- package/dist/services/auth.service.js +0 -266
- package/dist/services/index.d.ts +0 -30
- package/dist/services/index.js +0 -88
- package/dist/services/payment.service.d.ts +0 -20
- package/dist/services/payment.service.js +0 -56
- package/dist/services/privacy.service.d.ts +0 -16
- package/dist/services/privacy.service.js +0 -69
- package/dist/services/profile.service.d.ts +0 -20
- package/dist/services/profile.service.js +0 -158
- package/dist/services/subscription.service.d.ts +0 -7
- package/dist/services/subscription.service.js +0 -46
- package/dist/services/user.service.d.ts +0 -25
- package/dist/services/user.service.js +0 -109
- package/dist/styles/colors.d.ts +0 -24
- package/dist/styles/colors.js +0 -31
- package/dist/styles/shared.d.ts +0 -168
- package/dist/styles/shared.js +0 -177
- package/dist/types/index.d.ts +0 -136
- package/dist/types/index.js +0 -7
- package/dist/utils/api.d.ts +0 -20
- package/dist/utils/api.js +0 -546
- package/dist/utils/authEvents.d.ts +0 -44
- package/dist/utils/authEvents.js +0 -73
- package/dist/utils/errorHandler.d.ts +0 -68
- package/dist/utils/errorHandler.js +0 -166
- package/dist/utils/index.d.ts +0 -12
- package/dist/utils/index.js +0 -41
- package/dist/utils/logger.d.ts +0 -100
- package/dist/utils/logger.js +0 -298
- package/dist/utils/socket.d.ts +0 -6
- package/dist/utils/socket.js +0 -179
- package/dist/utils/socketConfig.d.ts +0 -32
- package/dist/utils/socketConfig.js +0 -67
- package/dist/utils/storage.d.ts +0 -54
- package/dist/utils/storage.js +0 -253
- /package/dist/{components/AuthBottomSheet → shared}/types.js +0 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useWallet = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const api_client_1 = require("../../shared/api-client");
|
|
6
|
+
const OxyContext_1 = require("../context/OxyContext");
|
|
7
|
+
const useWallet = (options) => {
|
|
8
|
+
const context = (0, OxyContext_1.useOxyContext)();
|
|
9
|
+
const apiUrl = (options === null || options === void 0 ? void 0 : options.apiUrl) || context.apiUrl;
|
|
10
|
+
const getToken = (options === null || options === void 0 ? void 0 : options.getToken) || context.getToken;
|
|
11
|
+
if (!apiUrl) {
|
|
12
|
+
throw new Error('API URL is required. Provide it either through options or OxyProvider.');
|
|
13
|
+
}
|
|
14
|
+
const [apiClient] = (0, react_1.useState)(() => new api_client_1.ApiClient({
|
|
15
|
+
apiUrl,
|
|
16
|
+
}));
|
|
17
|
+
// Get wallet information for the current user
|
|
18
|
+
const getWallet = (0, react_1.useCallback)(async (userId) => {
|
|
19
|
+
try {
|
|
20
|
+
if (!getToken) {
|
|
21
|
+
return {
|
|
22
|
+
success: false,
|
|
23
|
+
error: 'Authentication token is required'
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return await apiClient.request({
|
|
27
|
+
method: 'GET',
|
|
28
|
+
url: `/wallet/${userId}`,
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: `Bearer ${getToken() || ''}`
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
return {
|
|
36
|
+
success: false,
|
|
37
|
+
error: error.message || 'Failed to get wallet information'
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}, [apiClient, getToken]);
|
|
41
|
+
// Get transaction history
|
|
42
|
+
const getTransactionHistory = (0, react_1.useCallback)(async (userId, limit = 10, offset = 0) => {
|
|
43
|
+
try {
|
|
44
|
+
if (!getToken) {
|
|
45
|
+
return {
|
|
46
|
+
success: false,
|
|
47
|
+
error: 'Authentication token is required'
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return await apiClient.request({
|
|
51
|
+
method: 'GET',
|
|
52
|
+
url: `/wallet/transactions/${userId}`,
|
|
53
|
+
params: { limit, offset },
|
|
54
|
+
headers: {
|
|
55
|
+
Authorization: `Bearer ${getToken() || ''}`
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
return {
|
|
61
|
+
success: false,
|
|
62
|
+
error: error.message || 'Failed to get transaction history'
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}, [apiClient, getToken]);
|
|
66
|
+
// Transfer funds to another user
|
|
67
|
+
const transferFunds = (0, react_1.useCallback)(async (transferRequest) => {
|
|
68
|
+
try {
|
|
69
|
+
if (!getToken) {
|
|
70
|
+
return {
|
|
71
|
+
success: false,
|
|
72
|
+
error: 'Authentication token is required'
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return await apiClient.request({
|
|
76
|
+
method: 'POST',
|
|
77
|
+
url: '/wallet/transfer',
|
|
78
|
+
data: transferRequest,
|
|
79
|
+
headers: {
|
|
80
|
+
Authorization: `Bearer ${getToken() || ''}`
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
return {
|
|
86
|
+
success: false,
|
|
87
|
+
error: error.message || 'Failed to transfer funds'
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}, [apiClient, getToken]);
|
|
91
|
+
// Process a purchase
|
|
92
|
+
const processPurchase = (0, react_1.useCallback)(async (purchaseRequest) => {
|
|
93
|
+
try {
|
|
94
|
+
if (!getToken) {
|
|
95
|
+
return {
|
|
96
|
+
success: false,
|
|
97
|
+
error: 'Authentication token is required'
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return await apiClient.request({
|
|
101
|
+
method: 'POST',
|
|
102
|
+
url: '/wallet/purchase',
|
|
103
|
+
data: purchaseRequest,
|
|
104
|
+
headers: {
|
|
105
|
+
Authorization: `Bearer ${getToken() || ''}`
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
return {
|
|
111
|
+
success: false,
|
|
112
|
+
error: error.message || 'Failed to process purchase'
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}, [apiClient, getToken]);
|
|
116
|
+
// Request a withdrawal
|
|
117
|
+
const requestWithdrawal = (0, react_1.useCallback)(async (withdrawalRequest) => {
|
|
118
|
+
try {
|
|
119
|
+
if (!getToken) {
|
|
120
|
+
return {
|
|
121
|
+
success: false,
|
|
122
|
+
error: 'Authentication token is required'
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return await apiClient.request({
|
|
126
|
+
method: 'POST',
|
|
127
|
+
url: '/wallet/withdraw',
|
|
128
|
+
data: withdrawalRequest,
|
|
129
|
+
headers: {
|
|
130
|
+
Authorization: `Bearer ${getToken() || ''}`
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
return {
|
|
136
|
+
success: false,
|
|
137
|
+
error: error.message || 'Failed to request withdrawal'
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}, [apiClient, getToken]);
|
|
141
|
+
// Get a specific transaction
|
|
142
|
+
const getTransaction = (0, react_1.useCallback)(async (transactionId) => {
|
|
143
|
+
try {
|
|
144
|
+
if (!getToken) {
|
|
145
|
+
return {
|
|
146
|
+
success: false,
|
|
147
|
+
error: 'Authentication token is required'
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return await apiClient.request({
|
|
151
|
+
method: 'GET',
|
|
152
|
+
url: `/wallet/transaction/${transactionId}`,
|
|
153
|
+
headers: {
|
|
154
|
+
Authorization: `Bearer ${getToken() || ''}`
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
return {
|
|
160
|
+
success: false,
|
|
161
|
+
error: error.message || 'Failed to get transaction'
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}, [apiClient, getToken]);
|
|
165
|
+
return {
|
|
166
|
+
getWallet,
|
|
167
|
+
getTransactionHistory,
|
|
168
|
+
transferFunds,
|
|
169
|
+
processPurchase,
|
|
170
|
+
requestWithdrawal,
|
|
171
|
+
getTransaction
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
exports.useWallet = useWallet;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useAuth } from './hooks/useAuth';
|
|
2
|
+
import { useUser } from './hooks/useUser';
|
|
3
|
+
import { useKarma } from './hooks/useKarma';
|
|
4
|
+
import { useWallet } from './hooks/useWallet';
|
|
5
|
+
import { OxyProvider, useOxyContext } from './context/OxyContext';
|
|
6
|
+
export { useAuth, useUser, useKarma, useWallet, OxyProvider, useOxyContext };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOxyContext = exports.OxyProvider = exports.useWallet = exports.useKarma = exports.useUser = exports.useAuth = void 0;
|
|
4
|
+
const useAuth_1 = require("./hooks/useAuth");
|
|
5
|
+
Object.defineProperty(exports, "useAuth", { enumerable: true, get: function () { return useAuth_1.useAuth; } });
|
|
6
|
+
const useUser_1 = require("./hooks/useUser");
|
|
7
|
+
Object.defineProperty(exports, "useUser", { enumerable: true, get: function () { return useUser_1.useUser; } });
|
|
8
|
+
const useKarma_1 = require("./hooks/useKarma");
|
|
9
|
+
Object.defineProperty(exports, "useKarma", { enumerable: true, get: function () { return useKarma_1.useKarma; } });
|
|
10
|
+
const useWallet_1 = require("./hooks/useWallet");
|
|
11
|
+
Object.defineProperty(exports, "useWallet", { enumerable: true, get: function () { return useWallet_1.useWallet; } });
|
|
12
|
+
const OxyContext_1 = require("./context/OxyContext");
|
|
13
|
+
Object.defineProperty(exports, "OxyProvider", { enumerable: true, get: function () { return OxyContext_1.OxyProvider; } });
|
|
14
|
+
Object.defineProperty(exports, "useOxyContext", { enumerable: true, get: function () { return OxyContext_1.useOxyContext; } });
|
package/dist/index.d.ts
CHANGED
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* This module provides authentication, profile management, file handling, and other
|
|
5
|
-
* services for interacting with the OxyHQ API.
|
|
6
|
-
*
|
|
7
|
-
* @module oxyhqservices
|
|
8
|
-
*/
|
|
9
|
-
export * from './config';
|
|
10
|
-
export * from './constants';
|
|
11
|
-
export * from './services';
|
|
12
|
-
export * from './hooks';
|
|
13
|
-
export * from './types';
|
|
14
|
-
export * from './utils';
|
|
15
|
-
export * from './reducers';
|
|
16
|
-
export * from './styles/shared';
|
|
17
|
-
export * from './utils';
|
|
18
|
-
/**
|
|
19
|
-
* Initialize the OxyHQ services module
|
|
20
|
-
* @param options Configuration options
|
|
21
|
-
*/
|
|
22
|
-
export declare function initialize(options?: {
|
|
23
|
-
apiUrl?: string;
|
|
24
|
-
cloudUrl?: string;
|
|
25
|
-
enableLogging?: boolean;
|
|
26
|
-
logLevel?: string;
|
|
27
|
-
enableOfflineMode?: boolean;
|
|
28
|
-
}): void;
|
|
1
|
+
export * from './shared/types';
|
|
2
|
+
export { OxyAuthBackend, OxyUsersBackend, OxyKarmaBackend, OxyWalletBackend } from './backend';
|
|
3
|
+
export { useAuth, useUser, useKarma, useWallet, OxyProvider, useOxyContext } from './frontend';
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* OxyHQ Services Module
|
|
4
|
-
*
|
|
5
|
-
* This module provides authentication, profile management, file handling, and other
|
|
6
|
-
* services for interacting with the OxyHQ API.
|
|
7
|
-
*
|
|
8
|
-
* @module oxyhqservices
|
|
9
|
-
*/
|
|
10
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
3
|
if (k2 === undefined) k2 = k;
|
|
12
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -22,61 +14,20 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
22
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
23
15
|
};
|
|
24
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.
|
|
26
|
-
//
|
|
27
|
-
__exportStar(require("./
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// Utilities
|
|
43
|
-
__exportStar(require("./utils"), exports);
|
|
44
|
-
// Import package version
|
|
45
|
-
const package_json_1 = require("./package.json");
|
|
46
|
-
/**
|
|
47
|
-
* Initialize the OxyHQ services module
|
|
48
|
-
* @param options Configuration options
|
|
49
|
-
*/
|
|
50
|
-
function initialize(options) {
|
|
51
|
-
// Import configuration and services
|
|
52
|
-
const { OXY_CONFIG } = require('./config');
|
|
53
|
-
const { logger } = require('./utils/logger');
|
|
54
|
-
// Override configuration with provided options
|
|
55
|
-
if (options) {
|
|
56
|
-
if (options.apiUrl) {
|
|
57
|
-
OXY_CONFIG.API.BASE_URL = options.apiUrl;
|
|
58
|
-
}
|
|
59
|
-
if (options.cloudUrl) {
|
|
60
|
-
OXY_CONFIG.API.CLOUD_URL = options.cloudUrl;
|
|
61
|
-
}
|
|
62
|
-
if (options.enableLogging !== undefined) {
|
|
63
|
-
OXY_CONFIG.LOGGING.ENABLE_REMOTE_LOGGING = options.enableLogging;
|
|
64
|
-
}
|
|
65
|
-
if (options.logLevel) {
|
|
66
|
-
OXY_CONFIG.LOGGING.LEVEL = options.logLevel;
|
|
67
|
-
logger.setLogLevel(options.logLevel);
|
|
68
|
-
}
|
|
69
|
-
if (options.enableOfflineMode !== undefined) {
|
|
70
|
-
OXY_CONFIG.FEATURES.ENABLE_OFFLINE_MODE = options.enableOfflineMode;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
// Log initialization
|
|
74
|
-
logger.info('OxyHQ Services initialized', 'Initialization', {
|
|
75
|
-
version: package_json_1.version,
|
|
76
|
-
config: {
|
|
77
|
-
apiUrl: OXY_CONFIG.API.BASE_URL,
|
|
78
|
-
cloudUrl: OXY_CONFIG.API.CLOUD_URL,
|
|
79
|
-
environment: OXY_CONFIG.ENV.IS_PROD ? 'production' : 'development'
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
}
|
|
17
|
+
exports.useOxyContext = exports.OxyProvider = exports.useWallet = exports.useKarma = exports.useUser = exports.useAuth = exports.OxyWalletBackend = exports.OxyKarmaBackend = exports.OxyUsersBackend = exports.OxyAuthBackend = void 0;
|
|
18
|
+
// Export types
|
|
19
|
+
__exportStar(require("./shared/types"), exports);
|
|
20
|
+
// Export backend services
|
|
21
|
+
var backend_1 = require("./backend");
|
|
22
|
+
Object.defineProperty(exports, "OxyAuthBackend", { enumerable: true, get: function () { return backend_1.OxyAuthBackend; } });
|
|
23
|
+
Object.defineProperty(exports, "OxyUsersBackend", { enumerable: true, get: function () { return backend_1.OxyUsersBackend; } });
|
|
24
|
+
Object.defineProperty(exports, "OxyKarmaBackend", { enumerable: true, get: function () { return backend_1.OxyKarmaBackend; } });
|
|
25
|
+
Object.defineProperty(exports, "OxyWalletBackend", { enumerable: true, get: function () { return backend_1.OxyWalletBackend; } });
|
|
26
|
+
// Export frontend hooks and context
|
|
27
|
+
var frontend_1 = require("./frontend");
|
|
28
|
+
Object.defineProperty(exports, "useAuth", { enumerable: true, get: function () { return frontend_1.useAuth; } });
|
|
29
|
+
Object.defineProperty(exports, "useUser", { enumerable: true, get: function () { return frontend_1.useUser; } });
|
|
30
|
+
Object.defineProperty(exports, "useKarma", { enumerable: true, get: function () { return frontend_1.useKarma; } });
|
|
31
|
+
Object.defineProperty(exports, "useWallet", { enumerable: true, get: function () { return frontend_1.useWallet; } });
|
|
32
|
+
Object.defineProperty(exports, "OxyProvider", { enumerable: true, get: function () { return frontend_1.OxyProvider; } });
|
|
33
|
+
Object.defineProperty(exports, "useOxyContext", { enumerable: true, get: function () { return frontend_1.useOxyContext; } });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { ApiResponse, AuthTokens, OxyAuthConfig } from './types';
|
|
3
|
+
export declare class ApiClient {
|
|
4
|
+
private axiosInstance;
|
|
5
|
+
private config;
|
|
6
|
+
private tokenRefreshTimeout?;
|
|
7
|
+
constructor(config: OxyAuthConfig);
|
|
8
|
+
request<T>(config: AxiosRequestConfig): Promise<ApiResponse<T>>;
|
|
9
|
+
private getAccessToken;
|
|
10
|
+
private getRefreshToken;
|
|
11
|
+
storeTokens(tokens: AuthTokens): void;
|
|
12
|
+
clearTokens(): void;
|
|
13
|
+
private refreshToken;
|
|
14
|
+
private setupTokenRefresh;
|
|
15
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ApiClient = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
class ApiClient {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.axiosInstance = axios_1.default.create({
|
|
12
|
+
baseURL: config.apiUrl,
|
|
13
|
+
headers: {
|
|
14
|
+
'Content-Type': 'application/json',
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
// Add request interceptor to attach token
|
|
18
|
+
this.axiosInstance.interceptors.request.use((config) => {
|
|
19
|
+
const token = this.getAccessToken();
|
|
20
|
+
if (token) {
|
|
21
|
+
config.headers['Authorization'] = `Bearer ${token}`;
|
|
22
|
+
}
|
|
23
|
+
return config;
|
|
24
|
+
}, (error) => Promise.reject(error));
|
|
25
|
+
// Add response interceptor to handle token refresh
|
|
26
|
+
this.axiosInstance.interceptors.response.use((response) => response, async (error) => {
|
|
27
|
+
var _a;
|
|
28
|
+
const originalRequest = error.config;
|
|
29
|
+
// If error is 401 and we haven't tried to refresh the token yet
|
|
30
|
+
if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 && !originalRequest._retry) {
|
|
31
|
+
originalRequest._retry = true;
|
|
32
|
+
try {
|
|
33
|
+
// Try to refresh the token
|
|
34
|
+
const newTokens = await this.refreshToken();
|
|
35
|
+
if (newTokens) {
|
|
36
|
+
// Update the token in storage
|
|
37
|
+
this.storeTokens(newTokens);
|
|
38
|
+
// Update the Authorization header
|
|
39
|
+
originalRequest.headers['Authorization'] = `Bearer ${newTokens.accessToken}`;
|
|
40
|
+
// Retry the original request
|
|
41
|
+
return this.axiosInstance(originalRequest);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (refreshError) {
|
|
45
|
+
// If refresh fails, clear tokens and reject
|
|
46
|
+
this.clearTokens();
|
|
47
|
+
return Promise.reject(refreshError);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return Promise.reject(error);
|
|
51
|
+
});
|
|
52
|
+
// Set up token refresh if configured
|
|
53
|
+
if (config.tokenRefreshInterval) {
|
|
54
|
+
this.setupTokenRefresh(config.tokenRefreshInterval);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Generic request method with type support
|
|
58
|
+
async request(config) {
|
|
59
|
+
var _a;
|
|
60
|
+
try {
|
|
61
|
+
const response = await this.axiosInstance.request(config);
|
|
62
|
+
return response.data;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) {
|
|
66
|
+
return error.response.data;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
success: false,
|
|
70
|
+
error: error.message || 'Network error',
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Get the stored access token based on storage config
|
|
75
|
+
getAccessToken() {
|
|
76
|
+
if (typeof window === 'undefined' || this.config.storage === 'none') {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
switch (this.config.storage) {
|
|
80
|
+
case 'local':
|
|
81
|
+
return localStorage.getItem('oxy_access_token');
|
|
82
|
+
case 'session':
|
|
83
|
+
return sessionStorage.getItem('oxy_access_token');
|
|
84
|
+
case 'cookie':
|
|
85
|
+
// Simple cookie parser (would implement more robust version in production)
|
|
86
|
+
const match = document.cookie.match(/(^|;)\s*oxy_access_token=([^;]+)/);
|
|
87
|
+
return match ? match[2] : null;
|
|
88
|
+
default:
|
|
89
|
+
return localStorage.getItem('oxy_access_token');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Get the stored refresh token based on storage config
|
|
93
|
+
getRefreshToken() {
|
|
94
|
+
if (typeof window === 'undefined' || this.config.storage === 'none') {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
switch (this.config.storage) {
|
|
98
|
+
case 'local':
|
|
99
|
+
return localStorage.getItem('oxy_refresh_token');
|
|
100
|
+
case 'session':
|
|
101
|
+
return sessionStorage.getItem('oxy_refresh_token');
|
|
102
|
+
case 'cookie':
|
|
103
|
+
const match = document.cookie.match(/(^|;)\s*oxy_refresh_token=([^;]+)/);
|
|
104
|
+
return match ? match[2] : null;
|
|
105
|
+
default:
|
|
106
|
+
return localStorage.getItem('oxy_refresh_token');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Store tokens based on storage config
|
|
110
|
+
storeTokens(tokens) {
|
|
111
|
+
if (typeof window === 'undefined' || this.config.storage === 'none') {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
switch (this.config.storage) {
|
|
115
|
+
case 'local':
|
|
116
|
+
localStorage.setItem('oxy_access_token', tokens.accessToken);
|
|
117
|
+
localStorage.setItem('oxy_refresh_token', tokens.refreshToken);
|
|
118
|
+
break;
|
|
119
|
+
case 'session':
|
|
120
|
+
sessionStorage.setItem('oxy_access_token', tokens.accessToken);
|
|
121
|
+
sessionStorage.setItem('oxy_refresh_token', tokens.refreshToken);
|
|
122
|
+
break;
|
|
123
|
+
case 'cookie':
|
|
124
|
+
// Set cookies with HttpOnly and secure flags in production
|
|
125
|
+
document.cookie = `oxy_access_token=${tokens.accessToken}; path=/;`;
|
|
126
|
+
document.cookie = `oxy_refresh_token=${tokens.refreshToken}; path=/;`;
|
|
127
|
+
break;
|
|
128
|
+
default:
|
|
129
|
+
localStorage.setItem('oxy_access_token', tokens.accessToken);
|
|
130
|
+
localStorage.setItem('oxy_refresh_token', tokens.refreshToken);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// Clear stored tokens based on storage config
|
|
134
|
+
clearTokens() {
|
|
135
|
+
if (typeof window === 'undefined' || this.config.storage === 'none') {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
switch (this.config.storage) {
|
|
139
|
+
case 'local':
|
|
140
|
+
localStorage.removeItem('oxy_access_token');
|
|
141
|
+
localStorage.removeItem('oxy_refresh_token');
|
|
142
|
+
break;
|
|
143
|
+
case 'session':
|
|
144
|
+
sessionStorage.removeItem('oxy_access_token');
|
|
145
|
+
sessionStorage.removeItem('oxy_refresh_token');
|
|
146
|
+
break;
|
|
147
|
+
case 'cookie':
|
|
148
|
+
document.cookie = 'oxy_access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
|
|
149
|
+
document.cookie = 'oxy_refresh_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
|
|
150
|
+
break;
|
|
151
|
+
default:
|
|
152
|
+
localStorage.removeItem('oxy_access_token');
|
|
153
|
+
localStorage.removeItem('oxy_refresh_token');
|
|
154
|
+
}
|
|
155
|
+
// Clear the token refresh timeout
|
|
156
|
+
if (this.tokenRefreshTimeout) {
|
|
157
|
+
clearTimeout(this.tokenRefreshTimeout);
|
|
158
|
+
this.tokenRefreshTimeout = undefined;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// Refresh the access token using the refresh token
|
|
162
|
+
async refreshToken() {
|
|
163
|
+
const refreshToken = this.getRefreshToken();
|
|
164
|
+
if (!refreshToken) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
const response = await axios_1.default.post(`${this.config.apiUrl}/auth/refresh-token`, { refreshToken });
|
|
169
|
+
if (response.data.success && response.data.data) {
|
|
170
|
+
return response.data.data;
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Set up automatic token refresh at the specified interval
|
|
179
|
+
setupTokenRefresh(interval) {
|
|
180
|
+
if (typeof window === 'undefined') {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
this.tokenRefreshTimeout = setTimeout(async () => {
|
|
184
|
+
try {
|
|
185
|
+
const newTokens = await this.refreshToken();
|
|
186
|
+
if (newTokens) {
|
|
187
|
+
this.storeTokens(newTokens);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
// Set up the next refresh regardless of success/failure
|
|
192
|
+
this.setupTokenRefresh(interval);
|
|
193
|
+
}
|
|
194
|
+
}, interval);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
exports.ApiClient = ApiClient;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
export interface UserCredentials {
|
|
2
|
+
email?: string;
|
|
3
|
+
username?: string;
|
|
4
|
+
password: string;
|
|
5
|
+
}
|
|
6
|
+
export interface UserRegistration {
|
|
7
|
+
username: string;
|
|
8
|
+
email: string;
|
|
9
|
+
password: string;
|
|
10
|
+
}
|
|
11
|
+
export interface AuthTokens {
|
|
12
|
+
accessToken: string;
|
|
13
|
+
refreshToken: string;
|
|
14
|
+
}
|
|
15
|
+
export interface UserProfile {
|
|
16
|
+
id: string;
|
|
17
|
+
username: string;
|
|
18
|
+
email: string;
|
|
19
|
+
name?: {
|
|
20
|
+
first?: string;
|
|
21
|
+
last?: string;
|
|
22
|
+
};
|
|
23
|
+
avatar?: string;
|
|
24
|
+
description?: string;
|
|
25
|
+
coverPhoto?: string;
|
|
26
|
+
location?: string;
|
|
27
|
+
website?: string;
|
|
28
|
+
stats?: {
|
|
29
|
+
followers: number;
|
|
30
|
+
following: number;
|
|
31
|
+
posts: number;
|
|
32
|
+
karma: number;
|
|
33
|
+
};
|
|
34
|
+
createdAt: string;
|
|
35
|
+
}
|
|
36
|
+
export interface PrivacySettings {
|
|
37
|
+
isPrivateAccount: boolean;
|
|
38
|
+
hideOnlineStatus: boolean;
|
|
39
|
+
hideLastSeen: boolean;
|
|
40
|
+
profileVisibility: boolean;
|
|
41
|
+
postVisibility: boolean;
|
|
42
|
+
twoFactorEnabled: boolean;
|
|
43
|
+
loginAlerts: boolean;
|
|
44
|
+
blockScreenshots: boolean;
|
|
45
|
+
secureLogin: boolean;
|
|
46
|
+
biometricLogin: boolean;
|
|
47
|
+
showActivity: boolean;
|
|
48
|
+
allowTagging: boolean;
|
|
49
|
+
allowMentions: boolean;
|
|
50
|
+
hideReadReceipts: boolean;
|
|
51
|
+
allowComments: boolean;
|
|
52
|
+
allowDirectMessages: boolean;
|
|
53
|
+
dataSharing: boolean;
|
|
54
|
+
locationSharing: boolean;
|
|
55
|
+
analyticsSharing: boolean;
|
|
56
|
+
sensitiveContent: boolean;
|
|
57
|
+
autoFilter: boolean;
|
|
58
|
+
muteKeywords: boolean;
|
|
59
|
+
}
|
|
60
|
+
export interface KarmaAction {
|
|
61
|
+
userId: string;
|
|
62
|
+
action: string;
|
|
63
|
+
points?: number;
|
|
64
|
+
description?: string;
|
|
65
|
+
targetContentId?: string;
|
|
66
|
+
}
|
|
67
|
+
export interface KarmaHistoryItem {
|
|
68
|
+
action: string;
|
|
69
|
+
points: number;
|
|
70
|
+
timestamp: string;
|
|
71
|
+
description?: string;
|
|
72
|
+
sourceUserId?: string;
|
|
73
|
+
targetContentId?: string;
|
|
74
|
+
}
|
|
75
|
+
export interface KarmaHistory {
|
|
76
|
+
userId: string;
|
|
77
|
+
actions: KarmaHistoryItem[];
|
|
78
|
+
totalKarma: number;
|
|
79
|
+
}
|
|
80
|
+
export interface KarmaRule {
|
|
81
|
+
action: string;
|
|
82
|
+
points: number;
|
|
83
|
+
description: string;
|
|
84
|
+
category: string;
|
|
85
|
+
cooldownInMinutes: number;
|
|
86
|
+
}
|
|
87
|
+
export interface WalletInfo {
|
|
88
|
+
userId: string;
|
|
89
|
+
balance: number;
|
|
90
|
+
address?: string;
|
|
91
|
+
}
|
|
92
|
+
export type TransactionType = 'deposit' | 'withdrawal' | 'transfer' | 'purchase';
|
|
93
|
+
export type TransactionStatus = 'pending' | 'completed' | 'failed' | 'cancelled';
|
|
94
|
+
export interface TransactionInfo {
|
|
95
|
+
id: string;
|
|
96
|
+
userId: string;
|
|
97
|
+
type: TransactionType;
|
|
98
|
+
amount: number;
|
|
99
|
+
status: TransactionStatus;
|
|
100
|
+
description?: string;
|
|
101
|
+
recipientId?: string;
|
|
102
|
+
itemId?: string;
|
|
103
|
+
itemType?: string;
|
|
104
|
+
timestamp: string;
|
|
105
|
+
completedAt?: string;
|
|
106
|
+
}
|
|
107
|
+
export interface TransferRequest {
|
|
108
|
+
fromUserId: string;
|
|
109
|
+
toUserId: string;
|
|
110
|
+
amount: number;
|
|
111
|
+
description?: string;
|
|
112
|
+
}
|
|
113
|
+
export interface PurchaseRequest {
|
|
114
|
+
userId: string;
|
|
115
|
+
amount: number;
|
|
116
|
+
itemId: string;
|
|
117
|
+
itemType: string;
|
|
118
|
+
description?: string;
|
|
119
|
+
}
|
|
120
|
+
export interface WithdrawalRequest {
|
|
121
|
+
userId: string;
|
|
122
|
+
amount: number;
|
|
123
|
+
address: string;
|
|
124
|
+
}
|
|
125
|
+
export interface ApiResponse<T> {
|
|
126
|
+
success: boolean;
|
|
127
|
+
data?: T;
|
|
128
|
+
error?: string;
|
|
129
|
+
message?: string;
|
|
130
|
+
}
|
|
131
|
+
export interface OxyAuthConfig {
|
|
132
|
+
apiUrl: string;
|
|
133
|
+
storage?: 'local' | 'session' | 'cookie' | 'none';
|
|
134
|
+
tokenRefreshInterval?: number;
|
|
135
|
+
}
|