@oxyhq/services 5.12.5 → 5.12.8
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/LICENSE +1 -1
- package/README.md +5 -5
- package/lib/commonjs/core/OxyServices.js +13 -4
- package/lib/commonjs/core/OxyServices.js.map +1 -1
- package/lib/commonjs/ui/components/internal/TextField.js.map +1 -1
- package/lib/module/core/OxyServices.js +13 -4
- package/lib/module/core/OxyServices.js.map +1 -1
- package/lib/module/ui/components/internal/TextField.js.map +1 -1
- package/lib/typescript/core/OxyServices.d.ts +1 -1
- package/lib/typescript/core/OxyServices.d.ts.map +1 -1
- package/lib/typescript/types/shims.d.ts +24 -0
- package/package.json +9 -23
- package/src/core/OxyServices.ts +19 -10
- package/src/types/shims.d.ts +24 -0
- package/src/ui/components/internal/TextField.tsx +1 -1
- package/src/core/__tests__/OxyServices.test.ts +0 -180
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { OxyServices, OxyAuthenticationError, OxyAuthenticationTimeoutError } from '../OxyServices';
|
|
2
|
-
import { OXY_CLOUD_URL } from '../index';
|
|
3
|
-
|
|
4
|
-
// Mock axios for testing
|
|
5
|
-
jest.mock('axios');
|
|
6
|
-
|
|
7
|
-
describe('OxyServices', () => {
|
|
8
|
-
let oxyServices: OxyServices;
|
|
9
|
-
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
// Create a fresh instance for each test
|
|
12
|
-
oxyServices = new OxyServices({ baseURL: 'https://test.example.com' });
|
|
13
|
-
jest.clearAllMocks();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
describe('Constructor and Configuration', () => {
|
|
17
|
-
it('should create an instance with the provided baseURL', () => {
|
|
18
|
-
const customURL = 'https://custom.example.com';
|
|
19
|
-
const service = new OxyServices({ baseURL: customURL });
|
|
20
|
-
expect(service.getBaseURL()).toBe(customURL);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should have a default cloud URL constant', () => {
|
|
24
|
-
expect(OXY_CLOUD_URL).toBe('https://cloud.oxyhq.com');
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
describe('Token Management', () => {
|
|
29
|
-
it('should set and retrieve tokens', () => {
|
|
30
|
-
const accessToken = 'test-access-token';
|
|
31
|
-
const refreshToken = 'test-refresh-token';
|
|
32
|
-
|
|
33
|
-
oxyServices.setTokens(accessToken, refreshToken);
|
|
34
|
-
expect(oxyServices.hasValidToken()).toBe(true);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it('should clear tokens', () => {
|
|
38
|
-
oxyServices.setTokens('test-token', 'refresh-token');
|
|
39
|
-
expect(oxyServices.hasValidToken()).toBe(true);
|
|
40
|
-
|
|
41
|
-
oxyServices.clearTokens();
|
|
42
|
-
expect(oxyServices.hasValidToken()).toBe(false);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('should return null for user ID when no token is set', () => {
|
|
46
|
-
expect(oxyServices.getCurrentUserId()).toBeNull();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe('Authentication Methods', () => {
|
|
51
|
-
it('should call signUp endpoint with correct parameters', async () => {
|
|
52
|
-
const mockAxios = require('axios');
|
|
53
|
-
const mockResponse = { data: { message: 'Success', token: 'test-token', user: { id: '1', username: 'test' } } };
|
|
54
|
-
mockAxios.create().post.mockResolvedValue(mockResponse);
|
|
55
|
-
|
|
56
|
-
const result = await oxyServices.signUp('testuser', 'test@example.com', 'password');
|
|
57
|
-
|
|
58
|
-
expect(mockAxios.create().post).toHaveBeenCalledWith('/api/auth/signup', {
|
|
59
|
-
username: 'testuser',
|
|
60
|
-
email: 'test@example.com',
|
|
61
|
-
password: 'password'
|
|
62
|
-
});
|
|
63
|
-
expect(result).toEqual(mockResponse.data);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should call signIn endpoint with correct parameters', async () => {
|
|
67
|
-
const mockAxios = require('axios');
|
|
68
|
-
const mockResponse = { data: { accessToken: 'test-token', user: { id: '1', username: 'test' } } };
|
|
69
|
-
mockAxios.create().post.mockResolvedValue(mockResponse);
|
|
70
|
-
|
|
71
|
-
const result = await oxyServices.signIn('testuser', 'password');
|
|
72
|
-
|
|
73
|
-
expect(mockAxios.create().post).toHaveBeenCalledWith('/api/auth/login', {
|
|
74
|
-
username: 'testuser',
|
|
75
|
-
password: 'password',
|
|
76
|
-
deviceName: undefined,
|
|
77
|
-
deviceFingerprint: undefined
|
|
78
|
-
});
|
|
79
|
-
expect(result).toEqual(mockResponse.data);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
describe('Session Management', () => {
|
|
84
|
-
it('should get user by session ID', async () => {
|
|
85
|
-
const mockAxios = require('axios');
|
|
86
|
-
const mockUser = { id: '1', username: 'testuser' };
|
|
87
|
-
mockAxios.create().get.mockResolvedValue({ data: mockUser });
|
|
88
|
-
|
|
89
|
-
const result = await oxyServices.getUserBySession('test-session-id');
|
|
90
|
-
|
|
91
|
-
expect(mockAxios.create().get).toHaveBeenCalledWith('/api/session/user/test-session-id');
|
|
92
|
-
expect(result).toEqual(mockUser);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should get token by session ID and set it', async () => {
|
|
96
|
-
const mockAxios = require('axios');
|
|
97
|
-
const mockResponse = { data: { accessToken: 'new-token', expiresAt: '2024-01-01' } };
|
|
98
|
-
mockAxios.create().get.mockResolvedValue(mockResponse);
|
|
99
|
-
|
|
100
|
-
const result = await oxyServices.getTokenBySession('test-session-id');
|
|
101
|
-
|
|
102
|
-
expect(mockAxios.create().get).toHaveBeenCalledWith('/api/session/token/test-session-id');
|
|
103
|
-
expect(result).toEqual(mockResponse.data);
|
|
104
|
-
expect(oxyServices.hasValidToken()).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
describe('Error Handling', () => {
|
|
109
|
-
it('should handle API errors correctly', async () => {
|
|
110
|
-
const mockAxios = require('axios');
|
|
111
|
-
const mockError = new Error('API Error');
|
|
112
|
-
mockAxios.create().post.mockRejectedValue(mockError);
|
|
113
|
-
|
|
114
|
-
await expect(oxyServices.signUp('test', 'test@example.com', 'pass')).rejects.toThrow();
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
describe('Custom Error Types', () => {
|
|
119
|
-
it('should create OxyAuthenticationError with correct properties', () => {
|
|
120
|
-
const error = new OxyAuthenticationError('Test error', 'TEST_CODE', 401);
|
|
121
|
-
expect(error.message).toBe('Test error');
|
|
122
|
-
expect(error.code).toBe('TEST_CODE');
|
|
123
|
-
expect(error.status).toBe(401);
|
|
124
|
-
expect(error.name).toBe('OxyAuthenticationError');
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('should create OxyAuthenticationTimeoutError with correct properties', () => {
|
|
128
|
-
const error = new OxyAuthenticationTimeoutError('testOperation', 5000);
|
|
129
|
-
expect(error.message).toContain('testOperation');
|
|
130
|
-
expect(error.message).toContain('5000ms');
|
|
131
|
-
expect(error.code).toBe('AUTH_TIMEOUT');
|
|
132
|
-
expect(error.status).toBe(408);
|
|
133
|
-
expect(error.name).toBe('OxyAuthenticationTimeoutError');
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
describe('Health Check', () => {
|
|
138
|
-
it('should call health check endpoint', async () => {
|
|
139
|
-
const mockAxios = require('axios');
|
|
140
|
-
const mockResponse = { data: { status: 'ok', timestamp: '2024-01-01' } };
|
|
141
|
-
mockAxios.create().get.mockResolvedValue(mockResponse);
|
|
142
|
-
|
|
143
|
-
const result = await oxyServices.healthCheck();
|
|
144
|
-
|
|
145
|
-
expect(mockAxios.create().get).toHaveBeenCalledWith('/health');
|
|
146
|
-
expect(result).toEqual(mockResponse.data);
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
describe('Validation Methods', () => {
|
|
151
|
-
it('should return false when no token is available for validation', async () => {
|
|
152
|
-
const result = await oxyServices.validate();
|
|
153
|
-
expect(result).toBe(false);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
it('should validate token with server when token exists', async () => {
|
|
157
|
-
const mockAxios = require('axios');
|
|
158
|
-
oxyServices.setTokens('test-token');
|
|
159
|
-
mockAxios.create().get.mockResolvedValue({ data: { valid: true } });
|
|
160
|
-
|
|
161
|
-
const result = await oxyServices.validate();
|
|
162
|
-
|
|
163
|
-
expect(mockAxios.create().get).toHaveBeenCalledWith('/api/auth/validate');
|
|
164
|
-
expect(result).toBe(true);
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
describe('Wait for Authentication', () => {
|
|
169
|
-
it('should resolve immediately if token is already available', async () => {
|
|
170
|
-
oxyServices.setTokens('test-token');
|
|
171
|
-
const result = await oxyServices.waitForAuth(1000);
|
|
172
|
-
expect(result).toBe(true);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('should timeout if no token becomes available', async () => {
|
|
176
|
-
const result = await oxyServices.waitForAuth(100);
|
|
177
|
-
expect(result).toBe(false);
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
});
|