@explorins/pers-sdk-react-native 1.3.2 → 1.5.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 +276 -123
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +7 -0
- package/dist/hooks/useAnalytics.d.ts +96 -0
- package/dist/hooks/useAnalytics.d.ts.map +1 -0
- package/dist/hooks/useAnalytics.js +73 -0
- package/dist/hooks/useAuth.d.ts +52 -7
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +250 -3
- package/dist/hooks/useBusiness.d.ts +45 -3
- package/dist/hooks/useBusiness.d.ts.map +1 -1
- package/dist/hooks/useBusiness.js +182 -28
- package/dist/hooks/useCampaigns.d.ts +11 -4
- package/dist/hooks/useCampaigns.d.ts.map +1 -1
- package/dist/hooks/useCampaigns.js +101 -39
- package/dist/hooks/useDonations.d.ts +33 -0
- package/dist/hooks/useDonations.d.ts.map +1 -0
- package/dist/hooks/useDonations.js +62 -0
- package/dist/hooks/useFiles.d.ts +50 -0
- package/dist/hooks/useFiles.d.ts.map +1 -0
- package/dist/hooks/useFiles.js +140 -0
- package/dist/hooks/usePurchases.d.ts +9 -0
- package/dist/hooks/usePurchases.d.ts.map +1 -0
- package/dist/hooks/usePurchases.js +56 -0
- package/dist/hooks/useRedemptions.d.ts +10 -5
- package/dist/hooks/useRedemptions.d.ts.map +1 -1
- package/dist/hooks/useRedemptions.js +94 -66
- package/dist/hooks/useTenants.d.ts +10 -0
- package/dist/hooks/useTenants.d.ts.map +1 -0
- package/dist/hooks/useTenants.js +68 -0
- package/dist/hooks/useTokens.d.ts +36 -2
- package/dist/hooks/useTokens.d.ts.map +1 -1
- package/dist/hooks/useTokens.js +180 -19
- package/dist/hooks/useTransactions.d.ts +45 -3
- package/dist/hooks/useTransactions.d.ts.map +1 -1
- package/dist/hooks/useTransactions.js +152 -31
- package/dist/hooks/useUserStatus.d.ts +9 -0
- package/dist/hooks/useUserStatus.d.ts.map +1 -0
- package/dist/hooks/useUserStatus.js +57 -0
- package/dist/hooks/useUsers.d.ts +17 -0
- package/dist/hooks/useUsers.d.ts.map +1 -0
- package/dist/hooks/useUsers.js +120 -0
- package/dist/hooks/useWeb3.d.ts +69 -6
- package/dist/hooks/useWeb3.d.ts.map +1 -1
- package/dist/hooks/useWeb3.js +177 -17
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12199 -796
- package/dist/index.js.map +1 -1
- package/dist/polyfills/index.d.ts +1 -1
- package/dist/polyfills/index.d.ts.map +1 -1
- package/dist/polyfills/index.js +155 -9
- package/dist/polyfills/web3-polyfills.d.ts +7 -0
- package/dist/polyfills/web3-polyfills.d.ts.map +1 -0
- package/dist/polyfills/web3-polyfills.js +85 -0
- package/dist/providers/PersSDKProvider.d.ts +28 -18
- package/dist/providers/PersSDKProvider.d.ts.map +1 -1
- package/dist/providers/PersSDKProvider.js +63 -129
- package/dist/providers/react-native-auth-provider.d.ts +19 -64
- package/dist/providers/react-native-auth-provider.d.ts.map +1 -1
- package/dist/providers/react-native-auth-provider.js +57 -175
- package/package.json +171 -157
- package/src/hooks/index.ts +11 -1
- package/src/hooks/useAnalytics.ts +150 -0
- package/src/hooks/useAuth.ts +286 -27
- package/src/hooks/useBusiness.ts +193 -30
- package/src/hooks/useCampaigns.ts +121 -43
- package/src/hooks/useDonations.ts +68 -0
- package/src/hooks/useFiles.ts +160 -0
- package/src/hooks/usePurchases.ts +69 -0
- package/src/hooks/useRedemptions.ts +109 -68
- package/src/hooks/useTenants.ts +77 -0
- package/src/hooks/useTokens.ts +189 -21
- package/src/hooks/useTransactions.ts +170 -34
- package/src/hooks/useUserStatus.ts +65 -0
- package/src/hooks/useUsers.ts +133 -0
- package/src/hooks/useWeb3.ts +201 -21
- package/src/index.ts +21 -17
- package/src/polyfills/index.ts +163 -10
- package/src/polyfills/web3-polyfills.ts +98 -0
- package/src/providers/PersSDKProvider.tsx +114 -178
- package/src/providers/react-native-auth-provider.ts +65 -208
- package/dist/index.esm.js +0 -1057
- package/dist/index.esm.js.map +0 -1
- package/dist/polyfills/index.simple.d.ts +0 -2
- package/dist/polyfills/index.simple.d.ts.map +0 -1
- package/dist/polyfills/index.simple.js +0 -17
- package/src/polyfills/index.simple.ts +0 -22
- package/src/types/external-modules.d.ts +0 -13
|
@@ -1,71 +1,83 @@
|
|
|
1
|
-
import React, { createContext, useContext,
|
|
2
|
-
import {
|
|
3
|
-
import { TokenSDK } from '@explorins/pers-sdk/token';
|
|
4
|
-
import { createTransactionSDK } from '@explorins/pers-sdk/transaction';
|
|
5
|
-
import { createBusinessSDK } from '@explorins/pers-sdk/business';
|
|
6
|
-
import { createCampaignSDK } from '@explorins/pers-sdk/campaign';
|
|
7
|
-
import { createRedemptionSDK } from '@explorins/pers-sdk/redemption';
|
|
8
|
-
import { createWeb3SDK } from '@explorins/pers-sdk/web3';
|
|
1
|
+
import React, { createContext, useContext, useState, ReactNode, useCallback, useRef } from 'react';
|
|
2
|
+
import { PersSDK, PersConfig, createAuthProvider } from '@explorins/pers-sdk/core';
|
|
9
3
|
import { ReactNativeHttpClient } from './react-native-http-client';
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
import { ReactNativeAuthProvider } from './react-native-auth-provider';
|
|
5
|
+
import { UserDTO, AdminDTO } from '@explorins/pers-shared';
|
|
6
|
+
|
|
7
|
+
// Import manager types for TypeScript
|
|
8
|
+
import type {
|
|
9
|
+
AuthManager,
|
|
10
|
+
UserManager,
|
|
11
|
+
TokenManager,
|
|
12
|
+
BusinessManager,
|
|
13
|
+
CampaignManager,
|
|
14
|
+
RedemptionManager,
|
|
15
|
+
TransactionManager,
|
|
16
|
+
PurchaseManager,
|
|
17
|
+
TenantManager,
|
|
18
|
+
AnalyticsManager,
|
|
19
|
+
DonationManager
|
|
20
|
+
} from '@explorins/pers-sdk/core';
|
|
21
|
+
|
|
22
|
+
// Re-export PersConfig for external use
|
|
23
|
+
export type { PersConfig } from '@explorins/pers-sdk/core';
|
|
18
24
|
|
|
19
25
|
export interface PersSDKContext {
|
|
20
|
-
// SDK
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
// Main SDK instance
|
|
27
|
+
sdk: PersSDK | null;
|
|
28
|
+
|
|
29
|
+
// Manager shortcuts for convenience
|
|
30
|
+
auth: AuthManager | null;
|
|
31
|
+
users: UserManager | null;
|
|
32
|
+
tokens: TokenManager | null;
|
|
33
|
+
businesses: BusinessManager | null;
|
|
34
|
+
campaigns: CampaignManager | null;
|
|
35
|
+
redemptions: RedemptionManager | null;
|
|
36
|
+
transactions: TransactionManager | null;
|
|
37
|
+
purchases: PurchaseManager | null;
|
|
38
|
+
tenants: TenantManager | null;
|
|
39
|
+
analytics: AnalyticsManager | null;
|
|
40
|
+
donations: DonationManager | null;
|
|
41
|
+
|
|
42
|
+
// Legacy support (deprecated but kept for backward compatibility)
|
|
43
|
+
business: BusinessManager | null;
|
|
44
|
+
|
|
45
|
+
// Platform-specific providers
|
|
46
|
+
authProvider: ReactNativeSDKAuthProvider | null;
|
|
28
47
|
|
|
29
48
|
// State
|
|
30
49
|
isInitialized: boolean;
|
|
31
50
|
isAuthenticated: boolean;
|
|
32
|
-
user:
|
|
51
|
+
user: UserDTO | AdminDTO | null;
|
|
33
52
|
accountAddress: string | null;
|
|
34
53
|
|
|
35
54
|
// Methods
|
|
36
|
-
initialize: (config:
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
logout: () => Promise<void>;
|
|
55
|
+
initialize: (config: PersConfig) => Promise<void>;
|
|
56
|
+
setAuthenticationState: (user: UserDTO | AdminDTO | null, accountAddress: string | null, isAuthenticated: boolean) => void;
|
|
57
|
+
refreshUserData: () => Promise<void>;
|
|
40
58
|
}
|
|
41
59
|
|
|
42
60
|
// Create the context
|
|
43
61
|
const SDKContext = createContext<PersSDKContext | null>(null);
|
|
44
62
|
|
|
45
|
-
//
|
|
46
|
-
class ReactNativeSDKAuthProvider extends
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
constructor(private projectKey: string) {
|
|
51
|
-
super(projectKey);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async getToken(): Promise<string | null> {
|
|
55
|
-
return this._token;
|
|
63
|
+
// Simple wrapper for SDK integration
|
|
64
|
+
class ReactNativeSDKAuthProvider extends ReactNativeAuthProvider {
|
|
65
|
+
constructor(projectKey: string) {
|
|
66
|
+
super(projectKey, { debug: true });
|
|
56
67
|
}
|
|
57
68
|
|
|
69
|
+
// Override setAccessToken to provide backward compatibility
|
|
58
70
|
async setToken(token: string | null): Promise<void> {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
71
|
+
if (token) {
|
|
72
|
+
await this.setAccessToken(token);
|
|
73
|
+
} else {
|
|
74
|
+
await this.clearTokens();
|
|
75
|
+
}
|
|
64
76
|
}
|
|
65
77
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
// Override getToken for backward compatibility
|
|
79
|
+
async getToken(): Promise<string | null> {
|
|
80
|
+
return await super.getToken();
|
|
69
81
|
}
|
|
70
82
|
}
|
|
71
83
|
|
|
@@ -74,73 +86,47 @@ export const PersSDKProvider: React.FC<{
|
|
|
74
86
|
children: ReactNode;
|
|
75
87
|
}> = ({ children }) => {
|
|
76
88
|
const initializingRef = useRef(false);
|
|
77
|
-
const [
|
|
89
|
+
const [sdk, setSdk] = useState<PersSDK | null>(null);
|
|
78
90
|
const [authProvider, setAuthProvider] = useState<ReactNativeSDKAuthProvider | null>(null);
|
|
79
|
-
const [sdks, setSdks] = useState<{
|
|
80
|
-
tokens: any | null;
|
|
81
|
-
transactions: any | null;
|
|
82
|
-
business: any | null;
|
|
83
|
-
campaigns: any | null;
|
|
84
|
-
redemptions: any | null;
|
|
85
|
-
web3: any | null;
|
|
86
|
-
}>({
|
|
87
|
-
tokens: null,
|
|
88
|
-
transactions: null,
|
|
89
|
-
business: null,
|
|
90
|
-
campaigns: null,
|
|
91
|
-
redemptions: null,
|
|
92
|
-
web3: null,
|
|
93
|
-
});
|
|
94
91
|
|
|
95
92
|
const [isInitialized, setIsInitialized] = useState(false);
|
|
96
93
|
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
97
|
-
const [user, setUser] = useState<
|
|
94
|
+
const [user, setUser] = useState<UserDTO | AdminDTO | null>(null);
|
|
98
95
|
const [accountAddress, setAccountAddress] = useState<string | null>(null);
|
|
99
96
|
|
|
100
|
-
const initialize = useCallback(async (config:
|
|
97
|
+
const initialize = useCallback(async (config: PersConfig) => {
|
|
101
98
|
// Prevent multiple initializations
|
|
102
99
|
if (isInitialized || initializingRef.current) {
|
|
103
|
-
console.log('
|
|
100
|
+
console.log('SDK already initialized or initializing, skipping...');
|
|
104
101
|
return;
|
|
105
102
|
}
|
|
106
103
|
|
|
107
104
|
initializingRef.current = true;
|
|
108
105
|
|
|
109
106
|
try {
|
|
110
|
-
console.log('
|
|
107
|
+
console.log('Initializing PERS SDK with config:', config);
|
|
111
108
|
|
|
112
|
-
// Create auth provider
|
|
113
|
-
const auth = new ReactNativeSDKAuthProvider(config.apiProjectKey);
|
|
109
|
+
// Create React Native auth provider
|
|
110
|
+
const auth = new ReactNativeSDKAuthProvider(config.apiProjectKey || 'default-project');
|
|
114
111
|
setAuthProvider(auth);
|
|
115
112
|
|
|
116
113
|
// Create HTTP client
|
|
117
114
|
const httpClient = new ReactNativeHttpClient();
|
|
118
115
|
|
|
119
|
-
// Create
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
authProvider: auth
|
|
123
|
-
environment: config.environment || 'development',
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
setApiClient(client);
|
|
127
|
-
|
|
128
|
-
// Initialize domain SDKs
|
|
129
|
-
const domainSDKs = {
|
|
130
|
-
tokens: new TokenSDK(client),
|
|
131
|
-
transactions: createTransactionSDK(client),
|
|
132
|
-
business: createBusinessSDK(client),
|
|
133
|
-
campaigns: createCampaignSDK(client),
|
|
134
|
-
redemptions: createRedemptionSDK(client),
|
|
135
|
-
web3: createWeb3SDK(client),
|
|
116
|
+
// Create config with auth provider - use the ReactNative auth provider directly
|
|
117
|
+
const sdkConfig: PersConfig = {
|
|
118
|
+
...config,
|
|
119
|
+
authProvider: auth
|
|
136
120
|
};
|
|
137
121
|
|
|
138
|
-
|
|
122
|
+
// Initialize PersSDK with manager pattern
|
|
123
|
+
const sdkInstance = new PersSDK(httpClient, sdkConfig);
|
|
124
|
+
setSdk(sdkInstance);
|
|
139
125
|
setIsInitialized(true);
|
|
140
126
|
|
|
141
|
-
console.log('
|
|
127
|
+
console.log('PERS SDK initialized successfully');
|
|
142
128
|
} catch (error) {
|
|
143
|
-
console.error('
|
|
129
|
+
console.error('Failed to initialize PERS SDK:', error);
|
|
144
130
|
initializingRef.current = false;
|
|
145
131
|
throw error;
|
|
146
132
|
} finally {
|
|
@@ -148,99 +134,50 @@ export const PersSDKProvider: React.FC<{
|
|
|
148
134
|
}
|
|
149
135
|
}, []);
|
|
150
136
|
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
try {
|
|
157
|
-
console.log(`🔐 Logging in as ${userType}...`);
|
|
158
|
-
|
|
159
|
-
// Set token in auth provider
|
|
160
|
-
await authProvider.setToken(jwtToken);
|
|
161
|
-
|
|
162
|
-
// Perform login with API client
|
|
163
|
-
const result = userType === 'admin'
|
|
164
|
-
? await apiClient.loginAdmin(jwtToken)
|
|
165
|
-
: await apiClient.loginUser(jwtToken);
|
|
166
|
-
|
|
167
|
-
const userData = result.user || result.admin;
|
|
168
|
-
const userAccountAddress = (userData as any)?.accountAddress ||
|
|
169
|
-
(userData as any)?.wallets?.[0]?.address ||
|
|
170
|
-
null;
|
|
171
|
-
|
|
172
|
-
setUser(userData);
|
|
173
|
-
setAccountAddress(userAccountAddress);
|
|
174
|
-
setIsAuthenticated(true);
|
|
175
|
-
|
|
176
|
-
console.log('✅ Login successful');
|
|
177
|
-
return result;
|
|
178
|
-
} catch (error) {
|
|
179
|
-
console.error('❌ Login failed:', error);
|
|
180
|
-
throw error;
|
|
181
|
-
}
|
|
182
|
-
}, [apiClient, authProvider]);
|
|
183
|
-
|
|
184
|
-
const loginWithRawData = useCallback(async (rawUserData: any) => {
|
|
185
|
-
if (!apiClient || !authProvider) {
|
|
186
|
-
throw new Error('SDK not initialized. Call initialize() first.');
|
|
187
|
-
}
|
|
137
|
+
const setAuthenticationState = useCallback((user: UserDTO | AdminDTO | null, accountAddress: string | null, isAuthenticated: boolean) => {
|
|
138
|
+
setUser(user);
|
|
139
|
+
setAccountAddress(accountAddress);
|
|
140
|
+
setIsAuthenticated(isAuthenticated);
|
|
141
|
+
}, []);
|
|
188
142
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const result = await apiClient.loginUserWithRawData(rawUserData);
|
|
193
|
-
|
|
194
|
-
// Set token from result
|
|
195
|
-
if (result.accessToken) {
|
|
196
|
-
await authProvider.setToken(result.accessToken);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const userData = result.user;
|
|
200
|
-
const userAccountAddress = (userData as any)?.accountAddress ||
|
|
201
|
-
(userData as any)?.wallets?.[0]?.address ||
|
|
202
|
-
null;
|
|
203
|
-
|
|
204
|
-
setUser(userData);
|
|
205
|
-
setAccountAddress(userAccountAddress);
|
|
206
|
-
setIsAuthenticated(true);
|
|
207
|
-
|
|
208
|
-
console.log('✅ Raw data login successful');
|
|
209
|
-
return result;
|
|
210
|
-
} catch (error) {
|
|
211
|
-
console.error('❌ Raw data login failed:', error);
|
|
212
|
-
throw error;
|
|
143
|
+
const refreshUserData = useCallback(async (): Promise<void> => {
|
|
144
|
+
if (!sdk || !isAuthenticated || !isInitialized) {
|
|
145
|
+
throw new Error('SDK not initialized or not authenticated. Cannot refresh user data.');
|
|
213
146
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const logout = useCallback(async () => {
|
|
147
|
+
|
|
217
148
|
try {
|
|
218
|
-
console.log('
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
setUser(null);
|
|
225
|
-
setAccountAddress(null);
|
|
226
|
-
setIsAuthenticated(false);
|
|
227
|
-
|
|
228
|
-
console.log('✅ Logout successful');
|
|
149
|
+
console.log('Refreshing user data from remote server...');
|
|
150
|
+
const freshUserData = await sdk.users.getCurrentUser();
|
|
151
|
+
setUser(freshUserData);
|
|
152
|
+
console.log('User data refreshed successfully:', freshUserData);
|
|
229
153
|
} catch (error) {
|
|
230
|
-
console.error('
|
|
154
|
+
console.error('Failed to refresh user data:', error);
|
|
231
155
|
throw error;
|
|
232
156
|
}
|
|
233
|
-
}, [
|
|
157
|
+
}, [sdk, isAuthenticated, isInitialized]);
|
|
234
158
|
|
|
235
159
|
const contextValue: PersSDKContext = {
|
|
236
|
-
// SDK
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
160
|
+
// Main SDK instance
|
|
161
|
+
sdk,
|
|
162
|
+
|
|
163
|
+
// Manager shortcuts for convenience
|
|
164
|
+
auth: sdk?.auth || null,
|
|
165
|
+
users: sdk?.users || null,
|
|
166
|
+
tokens: sdk?.tokens || null,
|
|
167
|
+
businesses: sdk?.businesses || null,
|
|
168
|
+
campaigns: sdk?.campaigns || null,
|
|
169
|
+
redemptions: sdk?.redemptions || null,
|
|
170
|
+
transactions: sdk?.transactions || null,
|
|
171
|
+
purchases: sdk?.purchases || null,
|
|
172
|
+
tenants: sdk?.tenants || null,
|
|
173
|
+
analytics: sdk?.analytics || null,
|
|
174
|
+
donations: sdk?.donations || null,
|
|
175
|
+
|
|
176
|
+
// Legacy support (deprecated but kept for backward compatibility)
|
|
177
|
+
business: sdk?.businesses || null,
|
|
178
|
+
|
|
179
|
+
// Platform-specific providers
|
|
180
|
+
authProvider,
|
|
244
181
|
|
|
245
182
|
// State
|
|
246
183
|
isInitialized,
|
|
@@ -250,9 +187,8 @@ export const PersSDKProvider: React.FC<{
|
|
|
250
187
|
|
|
251
188
|
// Methods
|
|
252
189
|
initialize,
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
logout,
|
|
190
|
+
setAuthenticationState,
|
|
191
|
+
refreshUserData,
|
|
256
192
|
};
|
|
257
193
|
|
|
258
194
|
return (
|