kimu-core 0.4.1 → 0.5.0
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/.editorconfig +116 -30
- package/.gitattributes +81 -11
- package/.github/FUNDING.yml +8 -8
- package/.github/kimu-copilot-instructions.md +3779 -3779
- package/.github/workflows/deploy-demo.yml +39 -39
- package/.nvmrc +1 -0
- package/.prettierignore +44 -0
- package/.prettierrc +16 -0
- package/FUNDING.md +31 -31
- package/icon.svg +10 -10
- package/kimu-core-0.5.0.tgz +0 -0
- package/package.json +10 -3
- package/scripts/minify-css-assets.js +82 -82
- package/src/core/index.ts +47 -47
- package/src/core/kimu-global-styles.ts +136 -136
- package/src/core/kimu-reactive.ts +196 -196
- package/src/extensions/{kimu-home → app-root}/component.ts +5 -5
- package/src/extensions/extensions-manifest.json +4 -4
- package/src/main.ts +3 -3
- package/src/modules-repository/api-axios/CHANGELOG.md +48 -48
- package/src/modules-repository/api-axios/QUICK-REFERENCE.md +178 -178
- package/src/modules-repository/api-axios/README.md +304 -304
- package/src/modules-repository/api-axios/api-axios-service.ts +355 -355
- package/src/modules-repository/api-axios/examples.ts +293 -293
- package/src/modules-repository/api-axios/index.ts +19 -19
- package/src/modules-repository/api-axios/interfaces.ts +71 -71
- package/src/modules-repository/api-axios/module.ts +41 -41
- package/src/modules-repository/api-core/CHANGELOG.md +42 -42
- package/src/modules-repository/api-core/QUICK-REFERENCE.md +192 -192
- package/src/modules-repository/api-core/README.md +435 -435
- package/src/modules-repository/api-core/api-core-service.ts +289 -289
- package/src/modules-repository/api-core/examples.ts +432 -432
- package/src/modules-repository/api-core/index.ts +8 -8
- package/src/modules-repository/api-core/interfaces.ts +83 -83
- package/src/modules-repository/api-core/module.ts +30 -30
- package/src/modules-repository/event-bus/README.md +273 -273
- package/src/modules-repository/event-bus/event-bus-service.ts +176 -176
- package/src/modules-repository/event-bus/module.ts +30 -30
- package/src/modules-repository/notification/README.md +423 -423
- package/src/modules-repository/notification/module.ts +30 -30
- package/src/modules-repository/notification/notification-service.ts +436 -436
- package/src/modules-repository/router/README.it.md +61 -10
- package/src/modules-repository/router/README.md +61 -10
- package/src/modules-repository/router/router-config.ts.example +61 -0
- package/src/modules-repository/router/router.ts +18 -0
- package/src/modules-repository/state/README.md +409 -409
- package/src/modules-repository/state/module.ts +30 -30
- package/src/modules-repository/state/state-service.ts +296 -296
- package/src/modules-repository/theme/README.md +311 -267
- package/src/modules-repository/theme/module.ts +30 -30
- package/src/modules-repository/theme/pre-build.js +40 -40
- package/src/modules-repository/theme/theme-service.ts +411 -389
- package/src/modules-repository/theme/themes/theme-cherry-blossom.css +78 -78
- package/src/modules-repository/theme/themes/theme-cozy.css +111 -111
- package/src/modules-repository/theme/themes/theme-cyberpunk.css +150 -150
- package/src/modules-repository/theme/themes/theme-dark.css +79 -79
- package/src/modules-repository/theme/themes/theme-forest.css +171 -171
- package/src/modules-repository/theme/themes/theme-gold.css +100 -100
- package/src/modules-repository/theme/themes/theme-high-contrast.css +126 -126
- package/src/modules-repository/theme/themes/theme-lava.css +101 -101
- package/src/modules-repository/theme/themes/theme-lavender.css +90 -90
- package/src/modules-repository/theme/themes/theme-light.css +79 -79
- package/src/modules-repository/theme/themes/theme-matrix.css +103 -103
- package/src/modules-repository/theme/themes/theme-midnight.css +81 -81
- package/src/modules-repository/theme/themes/theme-nord.css +94 -94
- package/src/modules-repository/theme/themes/theme-ocean.css +84 -84
- package/src/modules-repository/theme/themes/theme-retro80s.css +343 -343
- package/src/modules-repository/theme/themes/theme-sunset.css +62 -62
- package/src/modules-repository/theme/themes-config-default.json +19 -0
- package/src/modules-repository/theme/themes-config.d.ts +27 -27
- package/src/modules-repository/theme/{themes-config.json → themes-config.json.example} +223 -213
- /package/src/extensions/{kimu-home → app-root}/lang/en.json +0 -0
- /package/src/extensions/{kimu-home → app-root}/lang/it.json +0 -0
- /package/src/extensions/{kimu-home → app-root}/style.css +0 -0
- /package/src/extensions/{kimu-home → app-root}/view.html +0 -0
|
@@ -1,293 +1,293 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* API Axios Service - Usage Examples
|
|
3
|
-
*
|
|
4
|
-
* Collection of practical examples demonstrating various features
|
|
5
|
-
* of the api-axios module.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { apiAxiosService } from './api-axios-service';
|
|
9
|
-
import type { ApiError } from './interfaces';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Example 1: Simple GET request
|
|
13
|
-
*/
|
|
14
|
-
export async function exampleSimpleGet() {
|
|
15
|
-
try {
|
|
16
|
-
const response = await apiAxiosService.get('https://api.ipify.org?format=json');
|
|
17
|
-
console.log('Your IP:', response.data.ip);
|
|
18
|
-
return response.data;
|
|
19
|
-
} catch (error: any) {
|
|
20
|
-
console.error('Error fetching IP:', error.message);
|
|
21
|
-
throw error;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Example 2: GET with query parameters
|
|
27
|
-
*/
|
|
28
|
-
export async function exampleGetWithParams() {
|
|
29
|
-
try {
|
|
30
|
-
const response = await apiAxiosService.get('https://api.github.com/search/repositories', {
|
|
31
|
-
params: {
|
|
32
|
-
q: 'kimu',
|
|
33
|
-
sort: 'stars',
|
|
34
|
-
order: 'desc'
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
console.log('Repositories found:', response.data.total_count);
|
|
38
|
-
return response.data;
|
|
39
|
-
} catch (error: any) {
|
|
40
|
-
console.error('Error searching repositories:', error.message);
|
|
41
|
-
throw error;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Example 3: POST request with data
|
|
47
|
-
*/
|
|
48
|
-
export async function examplePost() {
|
|
49
|
-
try {
|
|
50
|
-
const response = await apiAxiosService.post('https://jsonplaceholder.typicode.com/posts', {
|
|
51
|
-
title: 'My Post',
|
|
52
|
-
body: 'This is the content',
|
|
53
|
-
userId: 1
|
|
54
|
-
});
|
|
55
|
-
console.log('Post created:', response.data);
|
|
56
|
-
return response.data;
|
|
57
|
-
} catch (error: any) {
|
|
58
|
-
console.error('Error creating post:', error.message);
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Example 4: Request with retry logic
|
|
65
|
-
*/
|
|
66
|
-
export async function exampleWithRetry() {
|
|
67
|
-
try {
|
|
68
|
-
const response = await apiAxiosService.request(
|
|
69
|
-
'GET',
|
|
70
|
-
'https://api.ipify.org?format=json',
|
|
71
|
-
null,
|
|
72
|
-
{
|
|
73
|
-
retries: 3,
|
|
74
|
-
retryDelay: 1000
|
|
75
|
-
}
|
|
76
|
-
);
|
|
77
|
-
console.log('IP (with retry):', response.data.ip);
|
|
78
|
-
return response.data;
|
|
79
|
-
} catch (error: any) {
|
|
80
|
-
console.error('Failed after retries:', error.message);
|
|
81
|
-
throw error;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Example 5: Cached GET request
|
|
87
|
-
*/
|
|
88
|
-
export async function exampleWithCache() {
|
|
89
|
-
// First call - fetches from server
|
|
90
|
-
console.log('First call (from server)...');
|
|
91
|
-
const response1 = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
92
|
-
useCache: true
|
|
93
|
-
});
|
|
94
|
-
console.log('IP:', response1.data.ip);
|
|
95
|
-
|
|
96
|
-
// Second call - returns cached data
|
|
97
|
-
console.log('Second call (from cache)...');
|
|
98
|
-
const response2 = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
99
|
-
useCache: true
|
|
100
|
-
});
|
|
101
|
-
console.log('IP:', response2.data.ip);
|
|
102
|
-
|
|
103
|
-
return response2.data;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Example 6: Request with custom headers
|
|
108
|
-
*/
|
|
109
|
-
export async function exampleWithHeaders() {
|
|
110
|
-
try {
|
|
111
|
-
const response = await apiAxiosService.get('https://api.github.com/user', {
|
|
112
|
-
headers: {
|
|
113
|
-
'Authorization': 'Bearer YOUR_TOKEN_HERE',
|
|
114
|
-
'Accept': 'application/vnd.github.v3+json'
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
console.log('User:', response.data.login);
|
|
118
|
-
return response.data;
|
|
119
|
-
} catch (error: any) {
|
|
120
|
-
console.error('Error fetching user:', error.message);
|
|
121
|
-
throw error;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Example 7: Request with timeout
|
|
127
|
-
*/
|
|
128
|
-
export async function exampleWithTimeout() {
|
|
129
|
-
try {
|
|
130
|
-
const response = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
131
|
-
timeout: 5000 // 5 seconds
|
|
132
|
-
});
|
|
133
|
-
console.log('IP:', response.data.ip);
|
|
134
|
-
return response.data;
|
|
135
|
-
} catch (error: any) {
|
|
136
|
-
if (error.message.includes('timeout')) {
|
|
137
|
-
console.error('Request timed out!');
|
|
138
|
-
}
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Example 8: Upload with progress tracking
|
|
145
|
-
*/
|
|
146
|
-
export async function exampleUploadProgress() {
|
|
147
|
-
const formData = new FormData();
|
|
148
|
-
formData.append('file', new Blob(['test content'], { type: 'text/plain' }));
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
const response = await apiAxiosService.post(
|
|
152
|
-
'https://httpbin.org/post',
|
|
153
|
-
formData,
|
|
154
|
-
{
|
|
155
|
-
onUploadProgress: (event) => {
|
|
156
|
-
const percentComplete = Math.round((event.loaded * 100) / (event.total || 1));
|
|
157
|
-
console.log(`Upload progress: ${percentComplete}%`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
);
|
|
161
|
-
console.log('Upload complete:', response.status);
|
|
162
|
-
return response.data;
|
|
163
|
-
} catch (error: any) {
|
|
164
|
-
console.error('Upload failed:', error.message);
|
|
165
|
-
throw error;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Example 9: Request cancellation
|
|
171
|
-
*/
|
|
172
|
-
export async function exampleCancellation() {
|
|
173
|
-
const cancelToken = apiAxiosService.createCancelToken();
|
|
174
|
-
|
|
175
|
-
// Start the request
|
|
176
|
-
const requestPromise = apiAxiosService.get('https://api.github.com/users/torvalds', {
|
|
177
|
-
axiosConfig: {
|
|
178
|
-
cancelToken: cancelToken.token
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
// Cancel after 100ms
|
|
183
|
-
setTimeout(() => {
|
|
184
|
-
console.log('Cancelling request...');
|
|
185
|
-
cancelToken.cancel('User cancelled the request');
|
|
186
|
-
}, 100);
|
|
187
|
-
|
|
188
|
-
try {
|
|
189
|
-
const response = await requestPromise;
|
|
190
|
-
console.log('Request completed:', response.data.login);
|
|
191
|
-
return response.data;
|
|
192
|
-
} catch (error: any) {
|
|
193
|
-
if (error.message.includes('cancel')) {
|
|
194
|
-
console.log('Request was cancelled');
|
|
195
|
-
}
|
|
196
|
-
throw error;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Example 10: Using interceptors
|
|
202
|
-
*/
|
|
203
|
-
export function exampleInterceptors() {
|
|
204
|
-
// Add request interceptor to add auth token
|
|
205
|
-
apiAxiosService.addRequestInterceptor(async (config) => {
|
|
206
|
-
console.log('Request interceptor: Adding auth token');
|
|
207
|
-
config.headers = config.headers || {};
|
|
208
|
-
config.headers['X-Custom-Token'] = 'my-token-123';
|
|
209
|
-
return config;
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
// Add response interceptor to log responses
|
|
213
|
-
apiAxiosService.addResponseInterceptor(async (response) => {
|
|
214
|
-
console.log('Response interceptor: Status', response.status);
|
|
215
|
-
return response;
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
// Add error interceptor to handle errors globally
|
|
219
|
-
apiAxiosService.addErrorInterceptor(async (error) => {
|
|
220
|
-
console.error('Error interceptor:', error.message);
|
|
221
|
-
if (error.status === 401) {
|
|
222
|
-
console.log('Unauthorized - redirecting to login');
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
console.log('Interceptors configured');
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Example 11: TypeScript with generic types
|
|
231
|
-
*/
|
|
232
|
-
interface User {
|
|
233
|
-
id: number;
|
|
234
|
-
name: string;
|
|
235
|
-
email: string;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export async function exampleWithTypes() {
|
|
239
|
-
try {
|
|
240
|
-
const response = await apiAxiosService.get<User>('https://jsonplaceholder.typicode.com/users/1');
|
|
241
|
-
// response.data is typed as User
|
|
242
|
-
console.log('User name:', response.data.name);
|
|
243
|
-
console.log('User email:', response.data.email);
|
|
244
|
-
return response.data;
|
|
245
|
-
} catch (error: any) {
|
|
246
|
-
console.error('Error fetching user:', error.message);
|
|
247
|
-
throw error;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Example 12: Error handling with detailed information
|
|
253
|
-
*/
|
|
254
|
-
export async function exampleErrorHandling() {
|
|
255
|
-
try {
|
|
256
|
-
const response = await apiAxiosService.get('https://api.github.com/nonexistent');
|
|
257
|
-
return response.data;
|
|
258
|
-
} catch (error: any) {
|
|
259
|
-
const apiError = error as ApiError;
|
|
260
|
-
|
|
261
|
-
console.error('Error details:');
|
|
262
|
-
console.error('- Message:', apiError.message);
|
|
263
|
-
console.error('- Status:', apiError.status);
|
|
264
|
-
console.error('- Response data:', apiError.data);
|
|
265
|
-
console.error('- Original error:', apiError.originalError);
|
|
266
|
-
|
|
267
|
-
throw error;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Example 13: Cache management
|
|
273
|
-
*/
|
|
274
|
-
export async function exampleCacheManagement() {
|
|
275
|
-
// Set cache TTL to 10 minutes
|
|
276
|
-
apiAxiosService.setCacheTTL(10 * 60 * 1000);
|
|
277
|
-
|
|
278
|
-
// Make cached request
|
|
279
|
-
const response1 = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
280
|
-
useCache: true
|
|
281
|
-
});
|
|
282
|
-
console.log('First call:', response1.data.ip);
|
|
283
|
-
|
|
284
|
-
// Clear cache
|
|
285
|
-
apiAxiosService.clearCache();
|
|
286
|
-
console.log('Cache cleared');
|
|
287
|
-
|
|
288
|
-
// This will fetch from server again
|
|
289
|
-
const response2 = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
290
|
-
useCache: true
|
|
291
|
-
});
|
|
292
|
-
console.log('Second call (after clear):', response2.data.ip);
|
|
293
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* API Axios Service - Usage Examples
|
|
3
|
+
*
|
|
4
|
+
* Collection of practical examples demonstrating various features
|
|
5
|
+
* of the api-axios module.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { apiAxiosService } from './api-axios-service';
|
|
9
|
+
import type { ApiError } from './interfaces';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Example 1: Simple GET request
|
|
13
|
+
*/
|
|
14
|
+
export async function exampleSimpleGet() {
|
|
15
|
+
try {
|
|
16
|
+
const response = await apiAxiosService.get('https://api.ipify.org?format=json');
|
|
17
|
+
console.log('Your IP:', response.data.ip);
|
|
18
|
+
return response.data;
|
|
19
|
+
} catch (error: any) {
|
|
20
|
+
console.error('Error fetching IP:', error.message);
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Example 2: GET with query parameters
|
|
27
|
+
*/
|
|
28
|
+
export async function exampleGetWithParams() {
|
|
29
|
+
try {
|
|
30
|
+
const response = await apiAxiosService.get('https://api.github.com/search/repositories', {
|
|
31
|
+
params: {
|
|
32
|
+
q: 'kimu',
|
|
33
|
+
sort: 'stars',
|
|
34
|
+
order: 'desc'
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
console.log('Repositories found:', response.data.total_count);
|
|
38
|
+
return response.data;
|
|
39
|
+
} catch (error: any) {
|
|
40
|
+
console.error('Error searching repositories:', error.message);
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Example 3: POST request with data
|
|
47
|
+
*/
|
|
48
|
+
export async function examplePost() {
|
|
49
|
+
try {
|
|
50
|
+
const response = await apiAxiosService.post('https://jsonplaceholder.typicode.com/posts', {
|
|
51
|
+
title: 'My Post',
|
|
52
|
+
body: 'This is the content',
|
|
53
|
+
userId: 1
|
|
54
|
+
});
|
|
55
|
+
console.log('Post created:', response.data);
|
|
56
|
+
return response.data;
|
|
57
|
+
} catch (error: any) {
|
|
58
|
+
console.error('Error creating post:', error.message);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Example 4: Request with retry logic
|
|
65
|
+
*/
|
|
66
|
+
export async function exampleWithRetry() {
|
|
67
|
+
try {
|
|
68
|
+
const response = await apiAxiosService.request(
|
|
69
|
+
'GET',
|
|
70
|
+
'https://api.ipify.org?format=json',
|
|
71
|
+
null,
|
|
72
|
+
{
|
|
73
|
+
retries: 3,
|
|
74
|
+
retryDelay: 1000
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
console.log('IP (with retry):', response.data.ip);
|
|
78
|
+
return response.data;
|
|
79
|
+
} catch (error: any) {
|
|
80
|
+
console.error('Failed after retries:', error.message);
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Example 5: Cached GET request
|
|
87
|
+
*/
|
|
88
|
+
export async function exampleWithCache() {
|
|
89
|
+
// First call - fetches from server
|
|
90
|
+
console.log('First call (from server)...');
|
|
91
|
+
const response1 = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
92
|
+
useCache: true
|
|
93
|
+
});
|
|
94
|
+
console.log('IP:', response1.data.ip);
|
|
95
|
+
|
|
96
|
+
// Second call - returns cached data
|
|
97
|
+
console.log('Second call (from cache)...');
|
|
98
|
+
const response2 = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
99
|
+
useCache: true
|
|
100
|
+
});
|
|
101
|
+
console.log('IP:', response2.data.ip);
|
|
102
|
+
|
|
103
|
+
return response2.data;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Example 6: Request with custom headers
|
|
108
|
+
*/
|
|
109
|
+
export async function exampleWithHeaders() {
|
|
110
|
+
try {
|
|
111
|
+
const response = await apiAxiosService.get('https://api.github.com/user', {
|
|
112
|
+
headers: {
|
|
113
|
+
'Authorization': 'Bearer YOUR_TOKEN_HERE',
|
|
114
|
+
'Accept': 'application/vnd.github.v3+json'
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
console.log('User:', response.data.login);
|
|
118
|
+
return response.data;
|
|
119
|
+
} catch (error: any) {
|
|
120
|
+
console.error('Error fetching user:', error.message);
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Example 7: Request with timeout
|
|
127
|
+
*/
|
|
128
|
+
export async function exampleWithTimeout() {
|
|
129
|
+
try {
|
|
130
|
+
const response = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
131
|
+
timeout: 5000 // 5 seconds
|
|
132
|
+
});
|
|
133
|
+
console.log('IP:', response.data.ip);
|
|
134
|
+
return response.data;
|
|
135
|
+
} catch (error: any) {
|
|
136
|
+
if (error.message.includes('timeout')) {
|
|
137
|
+
console.error('Request timed out!');
|
|
138
|
+
}
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Example 8: Upload with progress tracking
|
|
145
|
+
*/
|
|
146
|
+
export async function exampleUploadProgress() {
|
|
147
|
+
const formData = new FormData();
|
|
148
|
+
formData.append('file', new Blob(['test content'], { type: 'text/plain' }));
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
const response = await apiAxiosService.post(
|
|
152
|
+
'https://httpbin.org/post',
|
|
153
|
+
formData,
|
|
154
|
+
{
|
|
155
|
+
onUploadProgress: (event) => {
|
|
156
|
+
const percentComplete = Math.round((event.loaded * 100) / (event.total || 1));
|
|
157
|
+
console.log(`Upload progress: ${percentComplete}%`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
console.log('Upload complete:', response.status);
|
|
162
|
+
return response.data;
|
|
163
|
+
} catch (error: any) {
|
|
164
|
+
console.error('Upload failed:', error.message);
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Example 9: Request cancellation
|
|
171
|
+
*/
|
|
172
|
+
export async function exampleCancellation() {
|
|
173
|
+
const cancelToken = apiAxiosService.createCancelToken();
|
|
174
|
+
|
|
175
|
+
// Start the request
|
|
176
|
+
const requestPromise = apiAxiosService.get('https://api.github.com/users/torvalds', {
|
|
177
|
+
axiosConfig: {
|
|
178
|
+
cancelToken: cancelToken.token
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Cancel after 100ms
|
|
183
|
+
setTimeout(() => {
|
|
184
|
+
console.log('Cancelling request...');
|
|
185
|
+
cancelToken.cancel('User cancelled the request');
|
|
186
|
+
}, 100);
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
const response = await requestPromise;
|
|
190
|
+
console.log('Request completed:', response.data.login);
|
|
191
|
+
return response.data;
|
|
192
|
+
} catch (error: any) {
|
|
193
|
+
if (error.message.includes('cancel')) {
|
|
194
|
+
console.log('Request was cancelled');
|
|
195
|
+
}
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Example 10: Using interceptors
|
|
202
|
+
*/
|
|
203
|
+
export function exampleInterceptors() {
|
|
204
|
+
// Add request interceptor to add auth token
|
|
205
|
+
apiAxiosService.addRequestInterceptor(async (config) => {
|
|
206
|
+
console.log('Request interceptor: Adding auth token');
|
|
207
|
+
config.headers = config.headers || {};
|
|
208
|
+
config.headers['X-Custom-Token'] = 'my-token-123';
|
|
209
|
+
return config;
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Add response interceptor to log responses
|
|
213
|
+
apiAxiosService.addResponseInterceptor(async (response) => {
|
|
214
|
+
console.log('Response interceptor: Status', response.status);
|
|
215
|
+
return response;
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// Add error interceptor to handle errors globally
|
|
219
|
+
apiAxiosService.addErrorInterceptor(async (error) => {
|
|
220
|
+
console.error('Error interceptor:', error.message);
|
|
221
|
+
if (error.status === 401) {
|
|
222
|
+
console.log('Unauthorized - redirecting to login');
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
console.log('Interceptors configured');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Example 11: TypeScript with generic types
|
|
231
|
+
*/
|
|
232
|
+
interface User {
|
|
233
|
+
id: number;
|
|
234
|
+
name: string;
|
|
235
|
+
email: string;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export async function exampleWithTypes() {
|
|
239
|
+
try {
|
|
240
|
+
const response = await apiAxiosService.get<User>('https://jsonplaceholder.typicode.com/users/1');
|
|
241
|
+
// response.data is typed as User
|
|
242
|
+
console.log('User name:', response.data.name);
|
|
243
|
+
console.log('User email:', response.data.email);
|
|
244
|
+
return response.data;
|
|
245
|
+
} catch (error: any) {
|
|
246
|
+
console.error('Error fetching user:', error.message);
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Example 12: Error handling with detailed information
|
|
253
|
+
*/
|
|
254
|
+
export async function exampleErrorHandling() {
|
|
255
|
+
try {
|
|
256
|
+
const response = await apiAxiosService.get('https://api.github.com/nonexistent');
|
|
257
|
+
return response.data;
|
|
258
|
+
} catch (error: any) {
|
|
259
|
+
const apiError = error as ApiError;
|
|
260
|
+
|
|
261
|
+
console.error('Error details:');
|
|
262
|
+
console.error('- Message:', apiError.message);
|
|
263
|
+
console.error('- Status:', apiError.status);
|
|
264
|
+
console.error('- Response data:', apiError.data);
|
|
265
|
+
console.error('- Original error:', apiError.originalError);
|
|
266
|
+
|
|
267
|
+
throw error;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Example 13: Cache management
|
|
273
|
+
*/
|
|
274
|
+
export async function exampleCacheManagement() {
|
|
275
|
+
// Set cache TTL to 10 minutes
|
|
276
|
+
apiAxiosService.setCacheTTL(10 * 60 * 1000);
|
|
277
|
+
|
|
278
|
+
// Make cached request
|
|
279
|
+
const response1 = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
280
|
+
useCache: true
|
|
281
|
+
});
|
|
282
|
+
console.log('First call:', response1.data.ip);
|
|
283
|
+
|
|
284
|
+
// Clear cache
|
|
285
|
+
apiAxiosService.clearCache();
|
|
286
|
+
console.log('Cache cleared');
|
|
287
|
+
|
|
288
|
+
// This will fetch from server again
|
|
289
|
+
const response2 = await apiAxiosService.get('https://api.ipify.org?format=json', {
|
|
290
|
+
useCache: true
|
|
291
|
+
});
|
|
292
|
+
console.log('Second call (after clear):', response2.data.ip);
|
|
293
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* API Axios Module - Public API
|
|
3
|
-
*
|
|
4
|
-
* Main entry point for the api-axios module.
|
|
5
|
-
*
|
|
6
|
-
* @module api-axios
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export { ApiAxiosService, apiAxiosService } from './api-axios-service';
|
|
10
|
-
export type {
|
|
11
|
-
ApiRequestConfig,
|
|
12
|
-
ApiResponse,
|
|
13
|
-
ApiError,
|
|
14
|
-
HttpMethod,
|
|
15
|
-
RequestInterceptor,
|
|
16
|
-
ResponseInterceptor,
|
|
17
|
-
ErrorInterceptor
|
|
18
|
-
} from './interfaces';
|
|
19
|
-
export { default as ApiAxiosModule } from './module';
|
|
1
|
+
/**
|
|
2
|
+
* API Axios Module - Public API
|
|
3
|
+
*
|
|
4
|
+
* Main entry point for the api-axios module.
|
|
5
|
+
*
|
|
6
|
+
* @module api-axios
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export { ApiAxiosService, apiAxiosService } from './api-axios-service';
|
|
10
|
+
export type {
|
|
11
|
+
ApiRequestConfig,
|
|
12
|
+
ApiResponse,
|
|
13
|
+
ApiError,
|
|
14
|
+
HttpMethod,
|
|
15
|
+
RequestInterceptor,
|
|
16
|
+
ResponseInterceptor,
|
|
17
|
+
ErrorInterceptor
|
|
18
|
+
} from './interfaces';
|
|
19
|
+
export { default as ApiAxiosModule } from './module';
|