@oxyhq/services 5.7.2 → 5.7.4

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.
Files changed (56) hide show
  1. package/README.md +287 -80
  2. package/lib/commonjs/core/index.js +0 -57
  3. package/lib/commonjs/core/index.js.map +1 -1
  4. package/lib/commonjs/node/index.js +49 -2
  5. package/lib/commonjs/node/index.js.map +1 -1
  6. package/lib/commonjs/node/middleware.js +227 -0
  7. package/lib/commonjs/node/middleware.js.map +1 -0
  8. package/lib/commonjs/ui/context/OxyContext.js +9 -29
  9. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  10. package/lib/commonjs/ui/hooks/useSessionSocket.js +1 -4
  11. package/lib/commonjs/ui/hooks/useSessionSocket.js.map +1 -1
  12. package/lib/commonjs/ui/index.js +16 -1
  13. package/lib/commonjs/ui/index.js.map +1 -1
  14. package/lib/commonjs/ui/zero-config/index.js +25 -0
  15. package/lib/commonjs/ui/zero-config/index.js.map +1 -0
  16. package/lib/commonjs/ui/zero-config/provider.js +278 -0
  17. package/lib/commonjs/ui/zero-config/provider.js.map +1 -0
  18. package/lib/module/core/index.js +0 -57
  19. package/lib/module/core/index.js.map +1 -1
  20. package/lib/module/node/index.js +11 -1
  21. package/lib/module/node/index.js.map +1 -1
  22. package/lib/module/node/middleware.js +199 -0
  23. package/lib/module/node/middleware.js.map +1 -0
  24. package/lib/module/ui/context/OxyContext.js +9 -29
  25. package/lib/module/ui/context/OxyContext.js.map +1 -1
  26. package/lib/module/ui/hooks/useSessionSocket.js +1 -4
  27. package/lib/module/ui/hooks/useSessionSocket.js.map +1 -1
  28. package/lib/module/ui/index.js +16 -1
  29. package/lib/module/ui/index.js.map +1 -1
  30. package/lib/module/ui/zero-config/index.js +8 -0
  31. package/lib/module/ui/zero-config/index.js.map +1 -0
  32. package/lib/module/ui/zero-config/provider.js +270 -0
  33. package/lib/module/ui/zero-config/provider.js.map +1 -0
  34. package/lib/typescript/core/index.d.ts +0 -6
  35. package/lib/typescript/core/index.d.ts.map +1 -1
  36. package/lib/typescript/node/index.d.ts +7 -1
  37. package/lib/typescript/node/index.d.ts.map +1 -1
  38. package/lib/typescript/node/middleware.d.ts +92 -0
  39. package/lib/typescript/node/middleware.d.ts.map +1 -0
  40. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  41. package/lib/typescript/ui/hooks/useSessionSocket.d.ts.map +1 -1
  42. package/lib/typescript/ui/index.d.ts +2 -1
  43. package/lib/typescript/ui/index.d.ts.map +1 -1
  44. package/lib/typescript/ui/zero-config/index.d.ts +5 -0
  45. package/lib/typescript/ui/zero-config/index.d.ts.map +1 -0
  46. package/lib/typescript/ui/zero-config/provider.d.ts +84 -0
  47. package/lib/typescript/ui/zero-config/provider.d.ts.map +1 -0
  48. package/package.json +6 -1
  49. package/src/core/index.ts +0 -60
  50. package/src/node/index.ts +17 -1
  51. package/src/node/middleware.ts +234 -0
  52. package/src/ui/context/OxyContext.tsx +9 -29
  53. package/src/ui/hooks/useSessionSocket.ts +1 -4
  54. package/src/ui/index.ts +19 -1
  55. package/src/ui/zero-config/index.ts +11 -0
  56. package/src/ui/zero-config/provider.tsx +310 -0
@@ -0,0 +1,310 @@
1
+ /**
2
+ * Zero-config OxyHQ Provider and Hook for React/React Native
3
+ *
4
+ * This provides a simplified, one-line setup for frontend authentication
5
+ * with automatic token management and API integration.
6
+ */
7
+
8
+ import React, { createContext, useContext, useEffect, useState, ReactNode, useCallback } from 'react';
9
+ import { OxyServices } from '../../core';
10
+ import { User, LoginResponse } from '../../models/interfaces';
11
+
12
+ export interface OxyZeroConfigState {
13
+ // Authentication state
14
+ user: User | null;
15
+ isAuthenticated: boolean;
16
+ isLoading: boolean;
17
+ error: string | null;
18
+
19
+ // Simple auth methods
20
+ login: (username: string, password: string) => Promise<User>;
21
+ logout: () => Promise<void>;
22
+ register: (username: string, email: string, password: string) => Promise<User>;
23
+
24
+ // Access to the OxyServices client
25
+ api: OxyServices;
26
+ }
27
+
28
+ const OxyZeroConfigContext = createContext<OxyZeroConfigState | null>(null);
29
+
30
+ export interface OxyZeroConfigProviderProps {
31
+ children: ReactNode;
32
+ /** Base URL of your Oxy API server (defaults to process.env.REACT_APP_OXY_API_URL or http://localhost:3001) */
33
+ apiUrl?: string;
34
+ /** Called when authentication state changes */
35
+ onAuthChange?: (user: User | null) => void;
36
+ /** Storage key prefix (default: 'oxy_zero') */
37
+ storagePrefix?: string;
38
+ }
39
+
40
+ /**
41
+ * Zero-config provider for OxyHQ Services
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * import { OxyZeroConfigProvider } from '@oxyhq/services/ui';
46
+ *
47
+ * function App() {
48
+ * return (
49
+ * <OxyZeroConfigProvider>
50
+ * <MyApp />
51
+ * </OxyZeroConfigProvider>
52
+ * );
53
+ * }
54
+ * ```
55
+ */
56
+ export const OxyZeroConfigProvider: React.FC<OxyZeroConfigProviderProps> = ({
57
+ children,
58
+ apiUrl,
59
+ onAuthChange,
60
+ storagePrefix = 'oxy_zero'
61
+ }) => {
62
+ // Determine API URL with fallbacks
63
+ const finalApiUrl = apiUrl ||
64
+ (typeof process !== 'undefined' && process.env?.REACT_APP_OXY_API_URL) ||
65
+ 'http://localhost:3001';
66
+
67
+ // Initialize OxyServices
68
+ const [api] = useState(() => new OxyServices({ baseURL: finalApiUrl }));
69
+
70
+ // State
71
+ const [user, setUser] = useState<User | null>(null);
72
+ const [isLoading, setIsLoading] = useState(true);
73
+ const [error, setError] = useState<string | null>(null);
74
+
75
+ const isAuthenticated = user !== null;
76
+
77
+ // Storage helpers
78
+ const getStorageKey = (key: string) => `${storagePrefix}_${key}`;
79
+
80
+ const saveToStorage = useCallback((key: string, value: string) => {
81
+ try {
82
+ if (typeof localStorage !== 'undefined') {
83
+ localStorage.setItem(getStorageKey(key), value);
84
+ }
85
+ } catch (error) {
86
+ console.warn('Failed to save to storage:', error);
87
+ }
88
+ }, [storagePrefix]);
89
+
90
+ const getFromStorage = useCallback((key: string): string | null => {
91
+ try {
92
+ if (typeof localStorage !== 'undefined') {
93
+ return localStorage.getItem(getStorageKey(key));
94
+ }
95
+ } catch (error) {
96
+ console.warn('Failed to read from storage:', error);
97
+ }
98
+ return null;
99
+ }, [storagePrefix]);
100
+
101
+ const removeFromStorage = useCallback((key: string) => {
102
+ try {
103
+ if (typeof localStorage !== 'undefined') {
104
+ localStorage.removeItem(getStorageKey(key));
105
+ }
106
+ } catch (error) {
107
+ console.warn('Failed to remove from storage:', error);
108
+ }
109
+ }, [storagePrefix]);
110
+
111
+ // Initialize authentication state
112
+ useEffect(() => {
113
+ const initAuth = async () => {
114
+ try {
115
+ setIsLoading(true);
116
+ setError(null);
117
+
118
+ // Try to restore tokens from storage
119
+ const savedAccessToken = getFromStorage('accessToken');
120
+ const savedRefreshToken = getFromStorage('refreshToken');
121
+
122
+ if (savedAccessToken && savedRefreshToken) {
123
+ // Set tokens in the API client
124
+ api.setTokens(savedAccessToken, savedRefreshToken);
125
+
126
+ // Validate the token
127
+ const isValid = await api.validate();
128
+ if (isValid) {
129
+ // Load user data
130
+ const currentUser = await api.getCurrentUser();
131
+ setUser(currentUser);
132
+
133
+ if (onAuthChange) {
134
+ onAuthChange(currentUser);
135
+ }
136
+ } else {
137
+ // Invalid token, clear storage
138
+ removeFromStorage('accessToken');
139
+ removeFromStorage('refreshToken');
140
+ api.clearTokens();
141
+ }
142
+ }
143
+ } catch (error: any) {
144
+ console.warn('Failed to restore authentication:', error);
145
+ setError('Failed to restore authentication');
146
+
147
+ // Clear invalid tokens
148
+ removeFromStorage('accessToken');
149
+ removeFromStorage('refreshToken');
150
+ api.clearTokens();
151
+ } finally {
152
+ setIsLoading(false);
153
+ }
154
+ };
155
+
156
+ initAuth();
157
+ }, [api, getFromStorage, removeFromStorage, onAuthChange]);
158
+
159
+ // Login method
160
+ const login = useCallback(async (username: string, password: string): Promise<User> => {
161
+ try {
162
+ setIsLoading(true);
163
+ setError(null);
164
+
165
+ const response: LoginResponse = await api.login(username, password);
166
+
167
+ // Save tokens to storage
168
+ if (response.accessToken) {
169
+ saveToStorage('accessToken', response.accessToken);
170
+ }
171
+ if (response.refreshToken) {
172
+ saveToStorage('refreshToken', response.refreshToken);
173
+ }
174
+
175
+ // Update state
176
+ setUser(response.user);
177
+
178
+ if (onAuthChange) {
179
+ onAuthChange(response.user);
180
+ }
181
+
182
+ return response.user;
183
+ } catch (error: any) {
184
+ const errorMessage = error.message || 'Login failed';
185
+ setError(errorMessage);
186
+ throw new Error(errorMessage);
187
+ } finally {
188
+ setIsLoading(false);
189
+ }
190
+ }, [api, saveToStorage, onAuthChange]);
191
+
192
+ // Logout method
193
+ const logout = useCallback(async (): Promise<void> => {
194
+ try {
195
+ setIsLoading(true);
196
+ await api.logout();
197
+ } catch (error) {
198
+ console.warn('Logout API call failed:', error);
199
+ } finally {
200
+ // Always clean up local state and storage
201
+ removeFromStorage('accessToken');
202
+ removeFromStorage('refreshToken');
203
+ api.clearTokens();
204
+ setUser(null);
205
+ setError(null);
206
+ setIsLoading(false);
207
+
208
+ if (onAuthChange) {
209
+ onAuthChange(null);
210
+ }
211
+ }
212
+ }, [api, removeFromStorage, onAuthChange]);
213
+
214
+ // Register method
215
+ const register = useCallback(async (username: string, email: string, password: string): Promise<User> => {
216
+ try {
217
+ setIsLoading(true);
218
+ setError(null);
219
+
220
+ const response = await api.signUp(username, email, password);
221
+
222
+ // Save token from registration
223
+ if (response.token) {
224
+ saveToStorage('accessToken', response.token);
225
+ // Note: signUp doesn't return refreshToken in current API
226
+ }
227
+
228
+ // Update state
229
+ setUser(response.user);
230
+
231
+ if (onAuthChange) {
232
+ onAuthChange(response.user);
233
+ }
234
+
235
+ return response.user;
236
+ } catch (error: any) {
237
+ const errorMessage = error.message || 'Registration failed';
238
+ setError(errorMessage);
239
+ throw new Error(errorMessage);
240
+ } finally {
241
+ setIsLoading(false);
242
+ }
243
+ }, [api, saveToStorage, onAuthChange]);
244
+
245
+ const contextValue: OxyZeroConfigState = {
246
+ user,
247
+ isAuthenticated,
248
+ isLoading,
249
+ error,
250
+ login,
251
+ logout,
252
+ register,
253
+ api
254
+ };
255
+
256
+ return (
257
+ <OxyZeroConfigContext.Provider value={contextValue}>
258
+ {children}
259
+ </OxyZeroConfigContext.Provider>
260
+ );
261
+ };
262
+
263
+ /**
264
+ * Zero-config hook for OxyHQ Services
265
+ *
266
+ * @example
267
+ * ```tsx
268
+ * function MyComponent() {
269
+ * const { user, login, logout, isAuthenticated } = useOxyZeroConfig();
270
+ *
271
+ * const handleLogin = () => {
272
+ * login('username', 'password');
273
+ * };
274
+ *
275
+ * if (isAuthenticated) {
276
+ * return <div>Welcome, {user?.username}!</div>;
277
+ * }
278
+ *
279
+ * return <button onClick={handleLogin}>Login</button>;
280
+ * }
281
+ * ```
282
+ */
283
+ export const useOxyZeroConfig = (): OxyZeroConfigState => {
284
+ const context = useContext(OxyZeroConfigContext);
285
+ if (!context) {
286
+ throw new Error('useOxyZeroConfig must be used within an OxyZeroConfigProvider');
287
+ }
288
+ return context;
289
+ };
290
+
291
+ /**
292
+ * Hook for automatic API client with authentication
293
+ * This automatically includes the auth token in requests
294
+ *
295
+ * @example
296
+ * ```tsx
297
+ * function ProfileComponent() {
298
+ * const api = useOxyApi();
299
+ *
300
+ * const updateProfile = async (data) => {
301
+ * const user = await api.updateProfile(data);
302
+ * // Token is automatically included
303
+ * };
304
+ * }
305
+ * ```
306
+ */
307
+ export const useOxyApi = (): OxyServices => {
308
+ const { api } = useOxyZeroConfig();
309
+ return api;
310
+ };