@oxyhq/services 5.5.7 → 5.5.9
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/lib/commonjs/core/index.js +13 -13
- package/lib/commonjs/core/index.js.map +1 -1
- package/lib/commonjs/ui/context/OxyContext.js +480 -87
- package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
- package/lib/commonjs/ui/hooks/useAuthFetch.js +80 -45
- package/lib/commonjs/ui/hooks/useAuthFetch.js.map +1 -1
- package/lib/commonjs/ui/screens/SessionManagementScreen.js.map +1 -1
- package/lib/module/core/index.js +13 -6
- package/lib/module/core/index.js.map +1 -1
- package/lib/module/ui/context/OxyContext.js +480 -87
- package/lib/module/ui/context/OxyContext.js.map +1 -1
- package/lib/module/ui/hooks/useAuthFetch.js +80 -45
- package/lib/module/ui/hooks/useAuthFetch.js.map +1 -1
- package/lib/module/ui/screens/SessionManagementScreen.js.map +1 -1
- package/lib/typescript/core/index.d.ts +7 -6
- package/lib/typescript/core/index.d.ts.map +1 -1
- package/lib/typescript/ui/context/OxyContext.d.ts +12 -7
- package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/useAuthFetch.d.ts +10 -4
- package/lib/typescript/ui/hooks/useAuthFetch.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/backend-middleware.test.ts +209 -0
- package/src/__tests__/ui/hooks/authfetch-integration.test.ts +197 -0
- package/src/__tests__/ui/hooks/backward-compatibility.test.ts +159 -0
- package/src/__tests__/ui/hooks/real-world-scenarios.test.ts +224 -0
- package/src/__tests__/ui/hooks/url-resolution.test.ts +129 -0
- package/src/__tests__/ui/hooks/useAuthFetch-separation.test.ts +69 -0
- package/src/__tests__/ui/hooks/useAuthFetch.test.ts +70 -0
- package/src/core/index.ts +13 -7
- package/src/ui/context/OxyContext.tsx +536 -99
- package/src/ui/hooks/useAuthFetch.ts +81 -47
- package/src/ui/screens/SessionManagementScreen.tsx +9 -9
- package/lib/commonjs/core/AuthManager.js +0 -378
- package/lib/commonjs/core/AuthManager.js.map +0 -1
- package/lib/module/core/AuthManager.js +0 -373
- package/lib/module/core/AuthManager.js.map +0 -1
- package/lib/typescript/core/AuthManager.d.ts +0 -100
- package/lib/typescript/core/AuthManager.d.ts.map +0 -1
- package/src/core/AuthManager.ts +0 -389
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Real-world scenario test: Mention app and other Oxy ecosystem apps
|
|
3
|
+
* This test demonstrates the exact use case described in the issue
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
describe('Real-world Oxy Ecosystem Integration', () => {
|
|
7
|
+
describe('Mention app scenario', () => {
|
|
8
|
+
// Simulate the complete Mention app setup
|
|
9
|
+
function simulateMentionAppSetup() {
|
|
10
|
+
console.log('Setting up Mention app with Oxy authentication...');
|
|
11
|
+
|
|
12
|
+
// 1. Create OxyServices for Oxy API authentication
|
|
13
|
+
const oxyServices = {
|
|
14
|
+
baseURL: 'https://api.oxy.so',
|
|
15
|
+
getBaseURL: () => 'https://api.oxy.so',
|
|
16
|
+
setBaseURL: (url: string) => {
|
|
17
|
+
console.log(`[OxyServices] Internal URL change to: ${url}`);
|
|
18
|
+
},
|
|
19
|
+
// Internal methods that need to call Oxy API
|
|
20
|
+
authenticateToken: async (token: string) => {
|
|
21
|
+
const url = `https://api.oxy.so/auth/validate`;
|
|
22
|
+
console.log(`[Internal] Validating token at: ${url}`);
|
|
23
|
+
return { valid: true, user: { id: 'user123', username: 'mentionuser' } };
|
|
24
|
+
},
|
|
25
|
+
refreshTokens: async () => {
|
|
26
|
+
const url = `https://api.oxy.so/auth/refresh`;
|
|
27
|
+
console.log(`[Internal] Refreshing tokens at: ${url}`);
|
|
28
|
+
return { accessToken: 'new-token', refreshToken: 'new-refresh' };
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// 2. Create context with separation
|
|
33
|
+
let appBaseURL = oxyServices.baseURL; // Initially same
|
|
34
|
+
|
|
35
|
+
const oxyContext = {
|
|
36
|
+
oxyServices,
|
|
37
|
+
setApiUrl: (url: string) => {
|
|
38
|
+
console.log(`[App] Setting app API URL to: ${url}`);
|
|
39
|
+
appBaseURL = url; // Only affects public authFetch
|
|
40
|
+
},
|
|
41
|
+
getAppBaseURL: () => appBaseURL,
|
|
42
|
+
isAuthenticated: true,
|
|
43
|
+
user: { id: 'user123', username: 'mentionuser' },
|
|
44
|
+
login: async (username: string, password: string) => ({ id: 'user123' }),
|
|
45
|
+
logout: async () => {},
|
|
46
|
+
signUp: async (username: string, email: string, password: string) => ({ id: 'user123' }),
|
|
47
|
+
activeSessionId: 'session123'
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// 3. Create authFetch that uses app-specific URL
|
|
51
|
+
const authFetch = {
|
|
52
|
+
get: async (endpoint: string) => {
|
|
53
|
+
const url = `${oxyContext.getAppBaseURL()}${endpoint}`;
|
|
54
|
+
console.log(`[AuthFetch] GET ${url}`);
|
|
55
|
+
return { data: `Response from ${url}` };
|
|
56
|
+
},
|
|
57
|
+
post: async (endpoint: string, data: any) => {
|
|
58
|
+
const url = `${oxyContext.getAppBaseURL()}${endpoint}`;
|
|
59
|
+
console.log(`[AuthFetch] POST ${url}`, data);
|
|
60
|
+
return { data: `Posted to ${url}`, created: data };
|
|
61
|
+
},
|
|
62
|
+
put: async (endpoint: string, data: any) => {
|
|
63
|
+
const url = `${oxyContext.getAppBaseURL()}${endpoint}`;
|
|
64
|
+
console.log(`[AuthFetch] PUT ${url}`, data);
|
|
65
|
+
return { data: `Updated at ${url}`, updated: data };
|
|
66
|
+
},
|
|
67
|
+
delete: async (endpoint: string) => {
|
|
68
|
+
const url = `${oxyContext.getAppBaseURL()}${endpoint}`;
|
|
69
|
+
console.log(`[AuthFetch] DELETE ${url}`);
|
|
70
|
+
return { data: `Deleted from ${url}` };
|
|
71
|
+
},
|
|
72
|
+
setApiUrl: oxyContext.setApiUrl,
|
|
73
|
+
isAuthenticated: oxyContext.isAuthenticated,
|
|
74
|
+
user: oxyContext.user
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return { oxyServices, oxyContext, authFetch };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
test('should enable Mention app to use its own backend with Oxy authentication', async () => {
|
|
81
|
+
console.log('\\n=== Mention App Real-world Test ===\\n');
|
|
82
|
+
|
|
83
|
+
const { oxyServices, oxyContext, authFetch } = simulateMentionAppSetup();
|
|
84
|
+
|
|
85
|
+
console.log('1. Initial setup - both use Oxy API:');
|
|
86
|
+
// Internal authentication calls
|
|
87
|
+
await oxyServices.authenticateToken('user-token');
|
|
88
|
+
|
|
89
|
+
// Public app calls (initially to Oxy API)
|
|
90
|
+
await authFetch.get('/api/user/profile');
|
|
91
|
+
|
|
92
|
+
console.log('\\n2. Mention app configures its own backend:');
|
|
93
|
+
// This is the key change - app sets its own API URL
|
|
94
|
+
authFetch.setApiUrl('https://mention.earth/api');
|
|
95
|
+
|
|
96
|
+
console.log('\\n3. After configuration - clean separation:');
|
|
97
|
+
|
|
98
|
+
// Internal module calls STILL go to Oxy API
|
|
99
|
+
await oxyServices.authenticateToken('user-token');
|
|
100
|
+
await oxyServices.refreshTokens();
|
|
101
|
+
|
|
102
|
+
// Public authFetch calls now go to Mention's backend
|
|
103
|
+
await authFetch.get('/api/mentions');
|
|
104
|
+
await authFetch.post('/api/mentions', {
|
|
105
|
+
text: 'Hello world from Mention!',
|
|
106
|
+
userId: 'user123'
|
|
107
|
+
});
|
|
108
|
+
await authFetch.put('/api/mentions/123', {
|
|
109
|
+
text: 'Updated mention text'
|
|
110
|
+
});
|
|
111
|
+
await authFetch.delete('/api/mentions/456');
|
|
112
|
+
|
|
113
|
+
console.log('\\n4. Verification:');
|
|
114
|
+
const internalURL = oxyServices.getBaseURL();
|
|
115
|
+
const publicURL = oxyContext.getAppBaseURL();
|
|
116
|
+
|
|
117
|
+
console.log(`Internal (Oxy API): ${internalURL}`);
|
|
118
|
+
console.log(`Public (Mention API): ${publicURL}`);
|
|
119
|
+
|
|
120
|
+
// Verify the separation
|
|
121
|
+
expect(internalURL).toBe('https://api.oxy.so');
|
|
122
|
+
expect(publicURL).toBe('https://mention.earth/api');
|
|
123
|
+
|
|
124
|
+
console.log('\\n✅ Mention app can use Oxy auth with its own backend!');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('should support multiple Oxy ecosystem apps with different backends', async () => {
|
|
128
|
+
console.log('\\n=== Multiple Oxy Apps Test ===\\n');
|
|
129
|
+
|
|
130
|
+
// Simulate different apps in the Oxy ecosystem
|
|
131
|
+
const apps = [
|
|
132
|
+
{ name: 'Mention', url: 'https://mention.earth/api' },
|
|
133
|
+
{ name: 'Homiio', url: 'https://homiio.com/api' },
|
|
134
|
+
{ name: 'Custom App', url: 'https://my-custom-app.com/api' }
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
for (const app of apps) {
|
|
138
|
+
console.log(`${app.name} app setup:`);
|
|
139
|
+
|
|
140
|
+
const { oxyServices, oxyContext, authFetch } = simulateMentionAppSetup();
|
|
141
|
+
|
|
142
|
+
// Each app configures its own backend
|
|
143
|
+
authFetch.setApiUrl(app.url);
|
|
144
|
+
|
|
145
|
+
// Internal auth still works with Oxy
|
|
146
|
+
await oxyServices.authenticateToken('token');
|
|
147
|
+
|
|
148
|
+
// App-specific calls go to app backend
|
|
149
|
+
await authFetch.get('/api/data');
|
|
150
|
+
|
|
151
|
+
// Verify separation for each app
|
|
152
|
+
expect(oxyServices.getBaseURL()).toBe('https://api.oxy.so');
|
|
153
|
+
expect(oxyContext.getAppBaseURL()).toBe(app.url);
|
|
154
|
+
|
|
155
|
+
console.log(` ✅ ${app.name} configured successfully\\n`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
console.log('✅ Multiple apps can coexist with independent backends!');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('should demonstrate zero-config setup with runtime configuration', async () => {
|
|
162
|
+
console.log('\\n=== Zero-Config + Runtime Config Test ===\\n');
|
|
163
|
+
|
|
164
|
+
console.log('1. Zero-config setup (as described in issue):');
|
|
165
|
+
|
|
166
|
+
// App developer just needs to:
|
|
167
|
+
const { oxyServices, authFetch } = simulateMentionAppSetup();
|
|
168
|
+
|
|
169
|
+
console.log(' - Create OxyServices instance ✅');
|
|
170
|
+
console.log(' - Wrap app with OxyProvider ✅');
|
|
171
|
+
console.log(' - Use authFetch in components ✅');
|
|
172
|
+
|
|
173
|
+
console.log('\\n2. Runtime configuration (almost zero setup):');
|
|
174
|
+
|
|
175
|
+
// App can dynamically configure its API
|
|
176
|
+
authFetch.setApiUrl('https://production.myapp.com/api');
|
|
177
|
+
|
|
178
|
+
console.log('\\n3. Everything works seamlessly:');
|
|
179
|
+
|
|
180
|
+
// Authentication through Oxy
|
|
181
|
+
await oxyServices.authenticateToken('user-token');
|
|
182
|
+
|
|
183
|
+
// App data through app's backend
|
|
184
|
+
await authFetch.get('/api/app-specific-data');
|
|
185
|
+
await authFetch.post('/api/app-actions', { action: 'create' });
|
|
186
|
+
|
|
187
|
+
console.log('\\n✅ Zero-config setup with runtime flexibility achieved!');
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe('Backend integration scenarios', () => {
|
|
192
|
+
test('should support backend token validation while frontend uses app API', async () => {
|
|
193
|
+
console.log('\\n=== Backend Integration Test ===\\n');
|
|
194
|
+
|
|
195
|
+
// Simulate backend middleware
|
|
196
|
+
const backendOxyServices = {
|
|
197
|
+
baseURL: 'https://api.oxy.so',
|
|
198
|
+
authenticateToken: async (token: string) => {
|
|
199
|
+
const url = `https://api.oxy.so/auth/validate`;
|
|
200
|
+
console.log(`[Backend] Validating token at: ${url}`);
|
|
201
|
+
return {
|
|
202
|
+
valid: true,
|
|
203
|
+
user: { id: 'user123', username: 'testuser' },
|
|
204
|
+
userId: 'user123'
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// Simulate frontend using app API
|
|
210
|
+
const { authFetch } = simulateMentionAppSetup();
|
|
211
|
+
authFetch.setApiUrl('https://myapp.com/api');
|
|
212
|
+
|
|
213
|
+
console.log('1. Frontend makes authenticated request to app backend:');
|
|
214
|
+
await authFetch.post('/api/protected-endpoint', { data: 'sensitive' });
|
|
215
|
+
|
|
216
|
+
console.log('\\n2. Backend validates token with Oxy API:');
|
|
217
|
+
const tokenValidation = await backendOxyServices.authenticateToken('user-jwt-token');
|
|
218
|
+
|
|
219
|
+
console.log(' Token validation result:', tokenValidation);
|
|
220
|
+
|
|
221
|
+
console.log('\\n✅ Backend validates with Oxy, frontend uses app API!');
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for URL resolution in separated authFetch implementation
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
describe('URL Resolution Separation', () => {
|
|
6
|
+
describe('resolveURL function behavior', () => {
|
|
7
|
+
// Mock implementation of resolveURL logic used in useAuthFetch
|
|
8
|
+
function resolveURL(input: RequestInfo | URL, baseURL: string): string {
|
|
9
|
+
if (!baseURL) {
|
|
10
|
+
throw new Error('Base URL not configured. Please provide a baseURL in OxyServices configuration.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const url = input.toString();
|
|
14
|
+
|
|
15
|
+
// If it's already a full URL (http/https), return as is
|
|
16
|
+
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
17
|
+
return url;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Normalize base URL (remove trailing slash)
|
|
21
|
+
const normalizedBaseURL = baseURL.replace(/\/$/, '');
|
|
22
|
+
|
|
23
|
+
// If URL starts with /, it's relative to base URL
|
|
24
|
+
if (url.startsWith('/')) {
|
|
25
|
+
return `${normalizedBaseURL}${url}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Otherwise, append to base URL with /
|
|
29
|
+
return `${normalizedBaseURL}/${url}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test('should resolve relative URLs with app base URL', () => {
|
|
33
|
+
const appBaseURL = 'https://myapp.example.com';
|
|
34
|
+
|
|
35
|
+
expect(resolveURL('/api/users', appBaseURL)).toBe('https://myapp.example.com/api/users');
|
|
36
|
+
expect(resolveURL('api/users', appBaseURL)).toBe('https://myapp.example.com/api/users');
|
|
37
|
+
expect(resolveURL('/api/posts', appBaseURL)).toBe('https://myapp.example.com/api/posts');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('should preserve absolute URLs unchanged', () => {
|
|
41
|
+
const appBaseURL = 'https://myapp.example.com';
|
|
42
|
+
const absoluteURL = 'https://external-api.example.com/data';
|
|
43
|
+
|
|
44
|
+
expect(resolveURL(absoluteURL, appBaseURL)).toBe(absoluteURL);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('should handle different app base URLs independently', () => {
|
|
48
|
+
const appBaseURL1 = 'https://app1.example.com';
|
|
49
|
+
const appBaseURL2 = 'https://app2.example.com';
|
|
50
|
+
const oxyBaseURL = 'https://api.oxy.so';
|
|
51
|
+
|
|
52
|
+
// Same endpoint, different base URLs
|
|
53
|
+
expect(resolveURL('/api/users', appBaseURL1)).toBe('https://app1.example.com/api/users');
|
|
54
|
+
expect(resolveURL('/api/users', appBaseURL2)).toBe('https://app2.example.com/api/users');
|
|
55
|
+
expect(resolveURL('/api/users', oxyBaseURL)).toBe('https://api.oxy.so/api/users');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('should normalize base URLs correctly', () => {
|
|
59
|
+
const baseURLWithSlash = 'https://myapp.example.com/';
|
|
60
|
+
const baseURLWithoutSlash = 'https://myapp.example.com';
|
|
61
|
+
|
|
62
|
+
expect(resolveURL('/api/users', baseURLWithSlash)).toBe('https://myapp.example.com/api/users');
|
|
63
|
+
expect(resolveURL('/api/users', baseURLWithoutSlash)).toBe('https://myapp.example.com/api/users');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('should throw error for missing base URL', () => {
|
|
67
|
+
expect(() => resolveURL('/api/users', '')).toThrow('Base URL not configured');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('Separation scenarios', () => {
|
|
72
|
+
test('should demonstrate independent URL management', () => {
|
|
73
|
+
// Simulate the separation: internal vs public URLs
|
|
74
|
+
const internalOxyURL = 'https://api.oxy.so';
|
|
75
|
+
const publicAppURL = 'https://mention.earth/api';
|
|
76
|
+
|
|
77
|
+
// Internal module calls (would use OxyServices base URL)
|
|
78
|
+
const internalCall = resolveURL('/auth/validate', internalOxyURL);
|
|
79
|
+
expect(internalCall).toBe('https://api.oxy.so/auth/validate');
|
|
80
|
+
|
|
81
|
+
// Public authFetch calls (would use app base URL)
|
|
82
|
+
const publicCall = resolveURL('/api/mentions', publicAppURL);
|
|
83
|
+
expect(publicCall).toBe('https://mention.earth/api/api/mentions');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('should support dynamic app URL changes', () => {
|
|
87
|
+
// Simulate changing app URL without affecting internal URL
|
|
88
|
+
let appBaseURL = 'https://staging.myapp.com';
|
|
89
|
+
const oxyBaseURL = 'https://api.oxy.so';
|
|
90
|
+
|
|
91
|
+
// Initial state
|
|
92
|
+
expect(resolveURL('/api/data', appBaseURL)).toBe('https://staging.myapp.com/api/data');
|
|
93
|
+
expect(resolveURL('/auth/me', oxyBaseURL)).toBe('https://api.oxy.so/auth/me');
|
|
94
|
+
|
|
95
|
+
// App changes its URL
|
|
96
|
+
appBaseURL = 'https://production.myapp.com';
|
|
97
|
+
|
|
98
|
+
// App URLs updated
|
|
99
|
+
expect(resolveURL('/api/data', appBaseURL)).toBe('https://production.myapp.com/api/data');
|
|
100
|
+
// Internal URLs unchanged
|
|
101
|
+
expect(resolveURL('/auth/me', oxyBaseURL)).toBe('https://api.oxy.so/auth/me');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Helper function to mimic resolveURL from the actual implementation
|
|
106
|
+
function resolveURL(input: RequestInfo | URL, baseURL: string): string {
|
|
107
|
+
if (!baseURL) {
|
|
108
|
+
throw new Error('Base URL not configured. Please provide a baseURL in OxyServices configuration.');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const url = input.toString();
|
|
112
|
+
|
|
113
|
+
// If it's already a full URL (http/https), return as is
|
|
114
|
+
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
115
|
+
return url;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Normalize base URL (remove trailing slash)
|
|
119
|
+
const normalizedBaseURL = baseURL.replace(/\/$/, '');
|
|
120
|
+
|
|
121
|
+
// If URL starts with /, it's relative to base URL
|
|
122
|
+
if (url.startsWith('/')) {
|
|
123
|
+
return `${normalizedBaseURL}${url}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Otherwise, append to base URL with /
|
|
127
|
+
return `${normalizedBaseURL}/${url}`;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for authFetch separation between public and internal usage
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { OxyServices } from '../../../core';
|
|
6
|
+
|
|
7
|
+
describe('AuthFetch Separation', () => {
|
|
8
|
+
let oxyServices: OxyServices;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
oxyServices = new OxyServices({
|
|
12
|
+
baseURL: 'https://api.oxy.so'
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('OxyServices internal base URL isolation', () => {
|
|
17
|
+
test('should preserve original base URL for internal calls', () => {
|
|
18
|
+
const originalBaseURL = 'https://api.oxy.so';
|
|
19
|
+
|
|
20
|
+
// Verify initial state
|
|
21
|
+
expect(oxyServices.getBaseURL()).toBe(originalBaseURL);
|
|
22
|
+
|
|
23
|
+
// Simulate app changing its own base URL (this should not affect OxyServices)
|
|
24
|
+
// Note: In the new implementation, setApiUrl only affects app authFetch, not OxyServices
|
|
25
|
+
const appBaseURL = 'https://app-api.example.com';
|
|
26
|
+
|
|
27
|
+
// OxyServices base URL should remain unchanged
|
|
28
|
+
expect(oxyServices.getBaseURL()).toBe(originalBaseURL);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('should handle OxyServices base URL changes separately', () => {
|
|
32
|
+
const originalBaseURL = 'https://api.oxy.so';
|
|
33
|
+
const newOxyBaseURL = 'https://new-oxy-api.example.com';
|
|
34
|
+
|
|
35
|
+
expect(oxyServices.getBaseURL()).toBe(originalBaseURL);
|
|
36
|
+
|
|
37
|
+
// This should still work for internal module updates
|
|
38
|
+
oxyServices.setBaseURL(newOxyBaseURL);
|
|
39
|
+
expect(oxyServices.getBaseURL()).toBe(newOxyBaseURL);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('should maintain separation - internal vs public URLs', () => {
|
|
43
|
+
const oxyBaseURL = 'https://api.oxy.so';
|
|
44
|
+
const appBaseURL = 'https://myapp.example.com';
|
|
45
|
+
|
|
46
|
+
// OxyServices keeps its own URL for internal calls
|
|
47
|
+
expect(oxyServices.getBaseURL()).toBe(oxyBaseURL);
|
|
48
|
+
|
|
49
|
+
// App can have its own URL (this would be managed in context)
|
|
50
|
+
// The separation ensures these don't interfere with each other
|
|
51
|
+
expect(oxyBaseURL).not.toBe(appBaseURL);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('URL validation and error handling', () => {
|
|
56
|
+
test('should validate URLs properly', () => {
|
|
57
|
+
expect(() => oxyServices.setBaseURL('')).toThrow('Base URL cannot be empty');
|
|
58
|
+
expect(() => oxyServices.setBaseURL('not-a-url')).not.toThrow(); // axios will handle invalid URLs
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('should handle URL normalization', () => {
|
|
62
|
+
oxyServices.setBaseURL('https://api.example.com/');
|
|
63
|
+
expect(oxyServices.getBaseURL()).toBe('https://api.example.com/');
|
|
64
|
+
|
|
65
|
+
oxyServices.setBaseURL('https://api.example.com');
|
|
66
|
+
expect(oxyServices.getBaseURL()).toBe('https://api.example.com');
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for refactored useAuthFetch hook
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { OxyServices } from '../../../core';
|
|
6
|
+
|
|
7
|
+
describe('useAuthFetch', () => {
|
|
8
|
+
let oxyServices: OxyServices;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
oxyServices = new OxyServices({
|
|
12
|
+
baseURL: 'https://api.test.com'
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('OxyServices integration', () => {
|
|
17
|
+
test('should set and get base URL', () => {
|
|
18
|
+
expect(oxyServices.getBaseURL()).toBe('https://api.test.com');
|
|
19
|
+
|
|
20
|
+
oxyServices.setBaseURL('https://new-api.test.com');
|
|
21
|
+
expect(oxyServices.getBaseURL()).toBe('https://new-api.test.com');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('should throw error for empty base URL', () => {
|
|
25
|
+
expect(() => oxyServices.setBaseURL('')).toThrow('Base URL cannot be empty');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should handle token validation methods', () => {
|
|
29
|
+
// Test that core methods exist
|
|
30
|
+
expect(typeof oxyServices.getAccessToken).toBe('function');
|
|
31
|
+
expect(typeof oxyServices.setTokens).toBe('function');
|
|
32
|
+
expect(typeof oxyServices.clearTokens).toBe('function');
|
|
33
|
+
expect(typeof oxyServices.refreshTokens).toBe('function');
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('URL resolution', () => {
|
|
38
|
+
test('should resolve relative URLs correctly', () => {
|
|
39
|
+
// This tests the logic that would be used in resolveURL function
|
|
40
|
+
const baseURL = 'https://api.test.com';
|
|
41
|
+
|
|
42
|
+
// Test relative URL with leading slash
|
|
43
|
+
const relativeUrl = '/api/users';
|
|
44
|
+
const expectedUrl = `${baseURL}${relativeUrl}`;
|
|
45
|
+
expect(expectedUrl).toBe('https://api.test.com/api/users');
|
|
46
|
+
|
|
47
|
+
// Test relative URL without leading slash
|
|
48
|
+
const relativeUrl2 = 'api/users';
|
|
49
|
+
const expectedUrl2 = `${baseURL}/${relativeUrl2}`;
|
|
50
|
+
expect(expectedUrl2).toBe('https://api.test.com/api/users');
|
|
51
|
+
|
|
52
|
+
// Test absolute URL (should remain unchanged)
|
|
53
|
+
const absoluteUrl = 'https://other-api.test.com/api/users';
|
|
54
|
+
expect(absoluteUrl).toBe('https://other-api.test.com/api/users');
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('Authentication middleware compatibility', () => {
|
|
59
|
+
test('should create middleware function', () => {
|
|
60
|
+
const middleware = oxyServices.createAuthenticateTokenMiddleware();
|
|
61
|
+
expect(typeof middleware).toBe('function');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('should validate tokens', async () => {
|
|
65
|
+
const result = await oxyServices.authenticateToken('invalid-token');
|
|
66
|
+
expect(result.valid).toBe(false);
|
|
67
|
+
expect(result.error).toBeTruthy();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
package/src/core/index.ts
CHANGED
|
@@ -68,14 +68,9 @@ interface JwtPayload {
|
|
|
68
68
|
/**
|
|
69
69
|
* OxyServices - Client library for interacting with the Oxy API
|
|
70
70
|
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* @deprecated Direct authentication methods - Use AuthManager instead
|
|
75
|
-
* @see packages/services/docs/authentication-v2.md
|
|
71
|
+
* Note: For authentication status in UI components, use `isAuthenticated` from useOxy() context
|
|
72
|
+
* instead of checking token status directly on this service.
|
|
76
73
|
*/
|
|
77
|
-
export { AuthManager } from './AuthManager';
|
|
78
|
-
|
|
79
74
|
export class OxyServices {
|
|
80
75
|
private client: AxiosInstance;
|
|
81
76
|
private accessToken: string | null = null;
|
|
@@ -169,6 +164,17 @@ export class OxyServices {
|
|
|
169
164
|
return this.client.defaults.baseURL || '';
|
|
170
165
|
}
|
|
171
166
|
|
|
167
|
+
/**
|
|
168
|
+
* Sets/updates the base URL for this OxyServices instance
|
|
169
|
+
* @param baseURL - The new base URL to use for API requests
|
|
170
|
+
*/
|
|
171
|
+
public setBaseURL(baseURL: string): void {
|
|
172
|
+
if (!baseURL) {
|
|
173
|
+
throw new Error('Base URL cannot be empty');
|
|
174
|
+
}
|
|
175
|
+
this.client.defaults.baseURL = baseURL;
|
|
176
|
+
}
|
|
177
|
+
|
|
172
178
|
/**
|
|
173
179
|
* Gets the currently authenticated user ID from the token
|
|
174
180
|
* @returns The user ID or null if not authenticated
|