kimu-core 0.4.1 → 0.4.2

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 (67) hide show
  1. package/.editorconfig +116 -30
  2. package/.gitattributes +81 -11
  3. package/.github/FUNDING.yml +8 -8
  4. package/.github/kimu-copilot-instructions.md +3779 -3779
  5. package/.github/workflows/deploy-demo.yml +39 -39
  6. package/.nvmrc +1 -0
  7. package/.prettierignore +44 -0
  8. package/.prettierrc +16 -0
  9. package/FUNDING.md +31 -31
  10. package/icon.svg +10 -10
  11. package/package.json +9 -2
  12. package/scripts/minify-css-assets.js +82 -82
  13. package/src/core/index.ts +47 -47
  14. package/src/core/kimu-global-styles.ts +136 -136
  15. package/src/core/kimu-reactive.ts +196 -196
  16. package/src/modules-repository/api-axios/CHANGELOG.md +48 -48
  17. package/src/modules-repository/api-axios/QUICK-REFERENCE.md +178 -178
  18. package/src/modules-repository/api-axios/README.md +304 -304
  19. package/src/modules-repository/api-axios/api-axios-service.ts +355 -355
  20. package/src/modules-repository/api-axios/examples.ts +293 -293
  21. package/src/modules-repository/api-axios/index.ts +19 -19
  22. package/src/modules-repository/api-axios/interfaces.ts +71 -71
  23. package/src/modules-repository/api-axios/module.ts +41 -41
  24. package/src/modules-repository/api-core/CHANGELOG.md +42 -42
  25. package/src/modules-repository/api-core/QUICK-REFERENCE.md +192 -192
  26. package/src/modules-repository/api-core/README.md +435 -435
  27. package/src/modules-repository/api-core/api-core-service.ts +289 -289
  28. package/src/modules-repository/api-core/examples.ts +432 -432
  29. package/src/modules-repository/api-core/index.ts +8 -8
  30. package/src/modules-repository/api-core/interfaces.ts +83 -83
  31. package/src/modules-repository/api-core/module.ts +30 -30
  32. package/src/modules-repository/event-bus/README.md +273 -273
  33. package/src/modules-repository/event-bus/event-bus-service.ts +176 -176
  34. package/src/modules-repository/event-bus/module.ts +30 -30
  35. package/src/modules-repository/notification/README.md +423 -423
  36. package/src/modules-repository/notification/module.ts +30 -30
  37. package/src/modules-repository/notification/notification-service.ts +436 -436
  38. package/src/modules-repository/router/README.it.md +61 -10
  39. package/src/modules-repository/router/README.md +61 -10
  40. package/src/modules-repository/router/router-config.ts.example +61 -0
  41. package/src/modules-repository/router/router.ts +18 -0
  42. package/src/modules-repository/state/README.md +409 -409
  43. package/src/modules-repository/state/module.ts +30 -30
  44. package/src/modules-repository/state/state-service.ts +296 -296
  45. package/src/modules-repository/theme/README.md +311 -267
  46. package/src/modules-repository/theme/module.ts +30 -30
  47. package/src/modules-repository/theme/pre-build.js +40 -40
  48. package/src/modules-repository/theme/theme-service.ts +411 -389
  49. package/src/modules-repository/theme/themes/theme-cherry-blossom.css +78 -78
  50. package/src/modules-repository/theme/themes/theme-cozy.css +111 -111
  51. package/src/modules-repository/theme/themes/theme-cyberpunk.css +150 -150
  52. package/src/modules-repository/theme/themes/theme-dark.css +79 -79
  53. package/src/modules-repository/theme/themes/theme-forest.css +171 -171
  54. package/src/modules-repository/theme/themes/theme-gold.css +100 -100
  55. package/src/modules-repository/theme/themes/theme-high-contrast.css +126 -126
  56. package/src/modules-repository/theme/themes/theme-lava.css +101 -101
  57. package/src/modules-repository/theme/themes/theme-lavender.css +90 -90
  58. package/src/modules-repository/theme/themes/theme-light.css +79 -79
  59. package/src/modules-repository/theme/themes/theme-matrix.css +103 -103
  60. package/src/modules-repository/theme/themes/theme-midnight.css +81 -81
  61. package/src/modules-repository/theme/themes/theme-nord.css +94 -94
  62. package/src/modules-repository/theme/themes/theme-ocean.css +84 -84
  63. package/src/modules-repository/theme/themes/theme-retro80s.css +343 -343
  64. package/src/modules-repository/theme/themes/theme-sunset.css +62 -62
  65. package/src/modules-repository/theme/themes-config-default.json +19 -0
  66. package/src/modules-repository/theme/themes-config.d.ts +27 -27
  67. package/src/modules-repository/theme/{themes-config.json → themes-config.json.example} +223 -213
@@ -1,432 +1,432 @@
1
- /**
2
- * KIMU API Core Module - Usage Examples
3
- * Demonstrates common use cases and patterns
4
- */
5
-
6
- import { apiCoreService } from './api-core-service';
7
-
8
- // ============================================
9
- // 1. BASIC CONFIGURATION
10
- // ============================================
11
-
12
- export function configureApiService() {
13
- apiCoreService.configure({
14
- baseURL: 'https://jsonplaceholder.typicode.com',
15
- timeout: 10000,
16
- headers: {
17
- 'Content-Type': 'application/json',
18
- 'Accept': 'application/json'
19
- }
20
- });
21
- }
22
-
23
- // ============================================
24
- // 2. SIMPLE GET REQUEST
25
- // ============================================
26
-
27
- export async function getUsersExample() {
28
- try {
29
- const response = await apiCoreService.get('/users');
30
- console.log('Users:', response.data);
31
- return response.data;
32
- } catch (error) {
33
- console.error('Error fetching users:', error);
34
- throw error;
35
- }
36
- }
37
-
38
- // ============================================
39
- // 3. GET WITH QUERY PARAMETERS
40
- // ============================================
41
-
42
- export async function getPostsWithFiltersExample() {
43
- try {
44
- const response = await apiCoreService.get('/posts', {
45
- params: {
46
- userId: 1,
47
- _limit: 10
48
- }
49
- });
50
- console.log('Filtered posts:', response.data);
51
- return response.data;
52
- } catch (error) {
53
- console.error('Error fetching posts:', error);
54
- throw error;
55
- }
56
- }
57
-
58
- // ============================================
59
- // 4. POST REQUEST WITH DATA
60
- // ============================================
61
-
62
- export async function createPostExample() {
63
- try {
64
- const newPost = {
65
- title: 'My New Post',
66
- body: 'This is the content of my post',
67
- userId: 1
68
- };
69
-
70
- const response = await apiCoreService.post('/posts', newPost);
71
- console.log('Created post:', response.data);
72
- return response.data;
73
- } catch (error) {
74
- console.error('Error creating post:', error);
75
- throw error;
76
- }
77
- }
78
-
79
- // ============================================
80
- // 5. PUT REQUEST (UPDATE)
81
- // ============================================
82
-
83
- export async function updatePostExample(postId: number) {
84
- try {
85
- const updatedPost = {
86
- id: postId,
87
- title: 'Updated Post Title',
88
- body: 'Updated content',
89
- userId: 1
90
- };
91
-
92
- const response = await apiCoreService.put(`/posts/${postId}`, updatedPost);
93
- console.log('Updated post:', response.data);
94
- return response.data;
95
- } catch (error) {
96
- console.error('Error updating post:', error);
97
- throw error;
98
- }
99
- }
100
-
101
- // ============================================
102
- // 6. PATCH REQUEST (PARTIAL UPDATE)
103
- // ============================================
104
-
105
- export async function patchPostExample(postId: number) {
106
- try {
107
- const response = await apiCoreService.patch(`/posts/${postId}`, {
108
- title: 'Partially Updated Title'
109
- });
110
- console.log('Patched post:', response.data);
111
- return response.data;
112
- } catch (error) {
113
- console.error('Error patching post:', error);
114
- throw error;
115
- }
116
- }
117
-
118
- // ============================================
119
- // 7. DELETE REQUEST
120
- // ============================================
121
-
122
- export async function deletePostExample(postId: number) {
123
- try {
124
- const response = await apiCoreService.delete(`/posts/${postId}`);
125
- console.log('Deleted post:', response.status);
126
- return response;
127
- } catch (error) {
128
- console.error('Error deleting post:', error);
129
- throw error;
130
- }
131
- }
132
-
133
- // ============================================
134
- // 8. REQUEST WITH CUSTOM HEADERS
135
- // ============================================
136
-
137
- export async function requestWithCustomHeadersExample() {
138
- try {
139
- const response = await apiCoreService.get('/users/1', {
140
- headers: {
141
- 'X-Custom-Header': 'custom-value',
142
- 'Accept-Language': 'it-IT'
143
- }
144
- });
145
- console.log('User with custom headers:', response.data);
146
- return response.data;
147
- } catch (error) {
148
- console.error('Error with custom headers:', error);
149
- throw error;
150
- }
151
- }
152
-
153
- // ============================================
154
- // 9. REQUEST WITH TIMEOUT
155
- // ============================================
156
-
157
- export async function requestWithTimeoutExample() {
158
- try {
159
- const response = await apiCoreService.get('/users', {
160
- timeout: 3000 // 3 seconds
161
- });
162
- console.log('Users with timeout:', response.data);
163
- return response.data;
164
- } catch (error) {
165
- console.error('Timeout or error:', error);
166
- throw error;
167
- }
168
- }
169
-
170
- // ============================================
171
- // 10. REQUEST WITH ABORT CONTROLLER
172
- // ============================================
173
-
174
- export async function requestWithAbortExample() {
175
- const controller = new AbortController();
176
-
177
- // Abort after 2 seconds
178
- setTimeout(() => {
179
- console.log('Aborting request...');
180
- controller.abort();
181
- }, 2000);
182
-
183
- try {
184
- const response = await apiCoreService.get('/users', {
185
- signal: controller.signal
186
- });
187
- console.log('Users:', response.data);
188
- return response.data;
189
- } catch (error) {
190
- console.error('Request aborted or error:', error);
191
- throw error;
192
- }
193
- }
194
-
195
- // ============================================
196
- // 11. USING REQUEST INTERCEPTOR
197
- // ============================================
198
-
199
- export function setupRequestInterceptorExample() {
200
- apiCoreService.configure({
201
- requestInterceptor: (config) => {
202
- // Add authentication token
203
- const token = localStorage.getItem('authToken');
204
- if (token) {
205
- config.headers = {
206
- ...config.headers,
207
- 'Authorization': `Bearer ${token}`
208
- };
209
- }
210
-
211
- // Log request
212
- console.log(`[Request] ${config.method} ${config.params}`);
213
-
214
- return config;
215
- }
216
- });
217
- }
218
-
219
- // ============================================
220
- // 12. USING RESPONSE INTERCEPTOR
221
- // ============================================
222
-
223
- export function setupResponseInterceptorExample() {
224
- apiCoreService.configure({
225
- responseInterceptor: (response) => {
226
- // Log response
227
- console.log(`[Response] ${response.status} - ${response.statusText}`);
228
-
229
- // Add metadata
230
- response.data = {
231
- ...response.data,
232
- _metadata: {
233
- receivedAt: new Date().toISOString(),
234
- status: response.status
235
- }
236
- };
237
-
238
- return response;
239
- }
240
- });
241
- }
242
-
243
- // ============================================
244
- // 13. USING ERROR INTERCEPTOR
245
- // ============================================
246
-
247
- export function setupErrorInterceptorExample() {
248
- apiCoreService.configure({
249
- errorInterceptor: async (error) => {
250
- // Log error
251
- console.error(`[Error] ${error.status} - ${error.message}`);
252
-
253
- // Handle 401 Unauthorized
254
- if (error.status === 401) {
255
- console.log('Unauthorized, redirecting to login...');
256
- // window.location.href = '/login';
257
- }
258
-
259
- // Handle 500 Server Error
260
- if (error.status && error.status >= 500) {
261
- console.log('Server error, please try again later');
262
- }
263
-
264
- return error;
265
- }
266
- });
267
- }
268
-
269
- // ============================================
270
- // 14. TYPED REQUESTS (TypeScript)
271
- // ============================================
272
-
273
- interface User {
274
- id: number;
275
- name: string;
276
- email: string;
277
- username: string;
278
- }
279
-
280
- interface Post {
281
- id: number;
282
- userId: number;
283
- title: string;
284
- body: string;
285
- }
286
-
287
- export async function typedRequestsExample() {
288
- // Type-safe GET
289
- const usersResponse = await apiCoreService.get<User[]>('/users');
290
- usersResponse.data.forEach(user => {
291
- console.log(user.name); // Type-safe access
292
- });
293
-
294
- // Type-safe POST
295
- const newPost: Omit<Post, 'id'> = {
296
- userId: 1,
297
- title: 'New Post',
298
- body: 'Content'
299
- };
300
-
301
- const postResponse = await apiCoreService.post<Post>('/posts', newPost);
302
- console.log(postResponse.data.id); // Type-safe
303
- }
304
-
305
- // ============================================
306
- // 15. ERROR HANDLING EXAMPLE
307
- // ============================================
308
-
309
- export async function errorHandlingExample() {
310
- try {
311
- const response = await apiCoreService.get('/invalid-endpoint');
312
- console.log(response.data);
313
- } catch (error: any) {
314
- // Access ApiError properties
315
- console.error('Error message:', error.message);
316
- console.error('HTTP status:', error.status);
317
- console.error('Status text:', error.statusText);
318
- console.error('Response data:', error.data);
319
- console.error('Original error:', error.originalError);
320
- }
321
- }
322
-
323
- // ============================================
324
- // 16. COMPLETE EXAMPLE: USER SERVICE
325
- // ============================================
326
-
327
- export class UserService {
328
- private api = apiCoreService;
329
-
330
- constructor() {
331
- // Configure service
332
- this.api.configure({
333
- baseURL: 'https://jsonplaceholder.typicode.com'
334
- });
335
- }
336
-
337
- async getAll(): Promise<User[]> {
338
- const response = await this.api.get<User[]>('/users');
339
- return response.data;
340
- }
341
-
342
- async getById(id: number): Promise<User> {
343
- const response = await this.api.get<User>(`/users/${id}`);
344
- return response.data;
345
- }
346
-
347
- async create(user: Omit<User, 'id'>): Promise<User> {
348
- const response = await this.api.post<User>('/users', user);
349
- return response.data;
350
- }
351
-
352
- async update(id: number, user: Partial<User>): Promise<User> {
353
- const response = await this.api.patch<User>(`/users/${id}`, user);
354
- return response.data;
355
- }
356
-
357
- async delete(id: number): Promise<void> {
358
- await this.api.delete(`/users/${id}`);
359
- }
360
-
361
- async search(query: string): Promise<User[]> {
362
- const response = await this.api.get<User[]>('/users', {
363
- params: { q: query }
364
- });
365
- return response.data;
366
- }
367
- }
368
-
369
- // ============================================
370
- // 17. USAGE IN KIMU COMPONENT
371
- // ============================================
372
-
373
- /*
374
- import { KimuComponentElement } from '@kimu/core';
375
- import { KimuModuleManager } from '@kimu/core';
376
-
377
- export class UserListComponent extends KimuComponentElement {
378
- private api: any;
379
- private users: User[] = [];
380
- private loading = false;
381
- private error: string | null = null;
382
-
383
- async onInit() {
384
- // Get API service
385
- this.api = KimuModuleManager.getInstance().getService('api-core');
386
-
387
- // Configure
388
- this.api.configure({
389
- baseURL: 'https://jsonplaceholder.typicode.com'
390
- });
391
-
392
- // Load data
393
- await this.loadUsers();
394
- }
395
-
396
- async loadUsers() {
397
- this.loading = true;
398
- this.error = null;
399
- this.onRender();
400
-
401
- try {
402
- const response = await this.api.get<User[]>('/users');
403
- this.users = response.data;
404
- } catch (error: any) {
405
- this.error = error.message;
406
- } finally {
407
- this.loading = false;
408
- this.onRender();
409
- }
410
- }
411
-
412
- async createUser(userData: Omit<User, 'id'>) {
413
- try {
414
- const response = await this.api.post<User>('/users', userData);
415
- this.users.push(response.data);
416
- this.onRender();
417
- } catch (error: any) {
418
- console.error('Failed to create user:', error);
419
- }
420
- }
421
-
422
- async deleteUser(id: number) {
423
- try {
424
- await this.api.delete(`/users/${id}`);
425
- this.users = this.users.filter(u => u.id !== id);
426
- this.onRender();
427
- } catch (error: any) {
428
- console.error('Failed to delete user:', error);
429
- }
430
- }
431
- }
432
- */
1
+ /**
2
+ * KIMU API Core Module - Usage Examples
3
+ * Demonstrates common use cases and patterns
4
+ */
5
+
6
+ import { apiCoreService } from './api-core-service';
7
+
8
+ // ============================================
9
+ // 1. BASIC CONFIGURATION
10
+ // ============================================
11
+
12
+ export function configureApiService() {
13
+ apiCoreService.configure({
14
+ baseURL: 'https://jsonplaceholder.typicode.com',
15
+ timeout: 10000,
16
+ headers: {
17
+ 'Content-Type': 'application/json',
18
+ 'Accept': 'application/json'
19
+ }
20
+ });
21
+ }
22
+
23
+ // ============================================
24
+ // 2. SIMPLE GET REQUEST
25
+ // ============================================
26
+
27
+ export async function getUsersExample() {
28
+ try {
29
+ const response = await apiCoreService.get('/users');
30
+ console.log('Users:', response.data);
31
+ return response.data;
32
+ } catch (error) {
33
+ console.error('Error fetching users:', error);
34
+ throw error;
35
+ }
36
+ }
37
+
38
+ // ============================================
39
+ // 3. GET WITH QUERY PARAMETERS
40
+ // ============================================
41
+
42
+ export async function getPostsWithFiltersExample() {
43
+ try {
44
+ const response = await apiCoreService.get('/posts', {
45
+ params: {
46
+ userId: 1,
47
+ _limit: 10
48
+ }
49
+ });
50
+ console.log('Filtered posts:', response.data);
51
+ return response.data;
52
+ } catch (error) {
53
+ console.error('Error fetching posts:', error);
54
+ throw error;
55
+ }
56
+ }
57
+
58
+ // ============================================
59
+ // 4. POST REQUEST WITH DATA
60
+ // ============================================
61
+
62
+ export async function createPostExample() {
63
+ try {
64
+ const newPost = {
65
+ title: 'My New Post',
66
+ body: 'This is the content of my post',
67
+ userId: 1
68
+ };
69
+
70
+ const response = await apiCoreService.post('/posts', newPost);
71
+ console.log('Created post:', response.data);
72
+ return response.data;
73
+ } catch (error) {
74
+ console.error('Error creating post:', error);
75
+ throw error;
76
+ }
77
+ }
78
+
79
+ // ============================================
80
+ // 5. PUT REQUEST (UPDATE)
81
+ // ============================================
82
+
83
+ export async function updatePostExample(postId: number) {
84
+ try {
85
+ const updatedPost = {
86
+ id: postId,
87
+ title: 'Updated Post Title',
88
+ body: 'Updated content',
89
+ userId: 1
90
+ };
91
+
92
+ const response = await apiCoreService.put(`/posts/${postId}`, updatedPost);
93
+ console.log('Updated post:', response.data);
94
+ return response.data;
95
+ } catch (error) {
96
+ console.error('Error updating post:', error);
97
+ throw error;
98
+ }
99
+ }
100
+
101
+ // ============================================
102
+ // 6. PATCH REQUEST (PARTIAL UPDATE)
103
+ // ============================================
104
+
105
+ export async function patchPostExample(postId: number) {
106
+ try {
107
+ const response = await apiCoreService.patch(`/posts/${postId}`, {
108
+ title: 'Partially Updated Title'
109
+ });
110
+ console.log('Patched post:', response.data);
111
+ return response.data;
112
+ } catch (error) {
113
+ console.error('Error patching post:', error);
114
+ throw error;
115
+ }
116
+ }
117
+
118
+ // ============================================
119
+ // 7. DELETE REQUEST
120
+ // ============================================
121
+
122
+ export async function deletePostExample(postId: number) {
123
+ try {
124
+ const response = await apiCoreService.delete(`/posts/${postId}`);
125
+ console.log('Deleted post:', response.status);
126
+ return response;
127
+ } catch (error) {
128
+ console.error('Error deleting post:', error);
129
+ throw error;
130
+ }
131
+ }
132
+
133
+ // ============================================
134
+ // 8. REQUEST WITH CUSTOM HEADERS
135
+ // ============================================
136
+
137
+ export async function requestWithCustomHeadersExample() {
138
+ try {
139
+ const response = await apiCoreService.get('/users/1', {
140
+ headers: {
141
+ 'X-Custom-Header': 'custom-value',
142
+ 'Accept-Language': 'it-IT'
143
+ }
144
+ });
145
+ console.log('User with custom headers:', response.data);
146
+ return response.data;
147
+ } catch (error) {
148
+ console.error('Error with custom headers:', error);
149
+ throw error;
150
+ }
151
+ }
152
+
153
+ // ============================================
154
+ // 9. REQUEST WITH TIMEOUT
155
+ // ============================================
156
+
157
+ export async function requestWithTimeoutExample() {
158
+ try {
159
+ const response = await apiCoreService.get('/users', {
160
+ timeout: 3000 // 3 seconds
161
+ });
162
+ console.log('Users with timeout:', response.data);
163
+ return response.data;
164
+ } catch (error) {
165
+ console.error('Timeout or error:', error);
166
+ throw error;
167
+ }
168
+ }
169
+
170
+ // ============================================
171
+ // 10. REQUEST WITH ABORT CONTROLLER
172
+ // ============================================
173
+
174
+ export async function requestWithAbortExample() {
175
+ const controller = new AbortController();
176
+
177
+ // Abort after 2 seconds
178
+ setTimeout(() => {
179
+ console.log('Aborting request...');
180
+ controller.abort();
181
+ }, 2000);
182
+
183
+ try {
184
+ const response = await apiCoreService.get('/users', {
185
+ signal: controller.signal
186
+ });
187
+ console.log('Users:', response.data);
188
+ return response.data;
189
+ } catch (error) {
190
+ console.error('Request aborted or error:', error);
191
+ throw error;
192
+ }
193
+ }
194
+
195
+ // ============================================
196
+ // 11. USING REQUEST INTERCEPTOR
197
+ // ============================================
198
+
199
+ export function setupRequestInterceptorExample() {
200
+ apiCoreService.configure({
201
+ requestInterceptor: (config) => {
202
+ // Add authentication token
203
+ const token = localStorage.getItem('authToken');
204
+ if (token) {
205
+ config.headers = {
206
+ ...config.headers,
207
+ 'Authorization': `Bearer ${token}`
208
+ };
209
+ }
210
+
211
+ // Log request
212
+ console.log(`[Request] ${config.method} ${config.params}`);
213
+
214
+ return config;
215
+ }
216
+ });
217
+ }
218
+
219
+ // ============================================
220
+ // 12. USING RESPONSE INTERCEPTOR
221
+ // ============================================
222
+
223
+ export function setupResponseInterceptorExample() {
224
+ apiCoreService.configure({
225
+ responseInterceptor: (response) => {
226
+ // Log response
227
+ console.log(`[Response] ${response.status} - ${response.statusText}`);
228
+
229
+ // Add metadata
230
+ response.data = {
231
+ ...response.data,
232
+ _metadata: {
233
+ receivedAt: new Date().toISOString(),
234
+ status: response.status
235
+ }
236
+ };
237
+
238
+ return response;
239
+ }
240
+ });
241
+ }
242
+
243
+ // ============================================
244
+ // 13. USING ERROR INTERCEPTOR
245
+ // ============================================
246
+
247
+ export function setupErrorInterceptorExample() {
248
+ apiCoreService.configure({
249
+ errorInterceptor: async (error) => {
250
+ // Log error
251
+ console.error(`[Error] ${error.status} - ${error.message}`);
252
+
253
+ // Handle 401 Unauthorized
254
+ if (error.status === 401) {
255
+ console.log('Unauthorized, redirecting to login...');
256
+ // window.location.href = '/login';
257
+ }
258
+
259
+ // Handle 500 Server Error
260
+ if (error.status && error.status >= 500) {
261
+ console.log('Server error, please try again later');
262
+ }
263
+
264
+ return error;
265
+ }
266
+ });
267
+ }
268
+
269
+ // ============================================
270
+ // 14. TYPED REQUESTS (TypeScript)
271
+ // ============================================
272
+
273
+ interface User {
274
+ id: number;
275
+ name: string;
276
+ email: string;
277
+ username: string;
278
+ }
279
+
280
+ interface Post {
281
+ id: number;
282
+ userId: number;
283
+ title: string;
284
+ body: string;
285
+ }
286
+
287
+ export async function typedRequestsExample() {
288
+ // Type-safe GET
289
+ const usersResponse = await apiCoreService.get<User[]>('/users');
290
+ usersResponse.data.forEach(user => {
291
+ console.log(user.name); // Type-safe access
292
+ });
293
+
294
+ // Type-safe POST
295
+ const newPost: Omit<Post, 'id'> = {
296
+ userId: 1,
297
+ title: 'New Post',
298
+ body: 'Content'
299
+ };
300
+
301
+ const postResponse = await apiCoreService.post<Post>('/posts', newPost);
302
+ console.log(postResponse.data.id); // Type-safe
303
+ }
304
+
305
+ // ============================================
306
+ // 15. ERROR HANDLING EXAMPLE
307
+ // ============================================
308
+
309
+ export async function errorHandlingExample() {
310
+ try {
311
+ const response = await apiCoreService.get('/invalid-endpoint');
312
+ console.log(response.data);
313
+ } catch (error: any) {
314
+ // Access ApiError properties
315
+ console.error('Error message:', error.message);
316
+ console.error('HTTP status:', error.status);
317
+ console.error('Status text:', error.statusText);
318
+ console.error('Response data:', error.data);
319
+ console.error('Original error:', error.originalError);
320
+ }
321
+ }
322
+
323
+ // ============================================
324
+ // 16. COMPLETE EXAMPLE: USER SERVICE
325
+ // ============================================
326
+
327
+ export class UserService {
328
+ private api = apiCoreService;
329
+
330
+ constructor() {
331
+ // Configure service
332
+ this.api.configure({
333
+ baseURL: 'https://jsonplaceholder.typicode.com'
334
+ });
335
+ }
336
+
337
+ async getAll(): Promise<User[]> {
338
+ const response = await this.api.get<User[]>('/users');
339
+ return response.data;
340
+ }
341
+
342
+ async getById(id: number): Promise<User> {
343
+ const response = await this.api.get<User>(`/users/${id}`);
344
+ return response.data;
345
+ }
346
+
347
+ async create(user: Omit<User, 'id'>): Promise<User> {
348
+ const response = await this.api.post<User>('/users', user);
349
+ return response.data;
350
+ }
351
+
352
+ async update(id: number, user: Partial<User>): Promise<User> {
353
+ const response = await this.api.patch<User>(`/users/${id}`, user);
354
+ return response.data;
355
+ }
356
+
357
+ async delete(id: number): Promise<void> {
358
+ await this.api.delete(`/users/${id}`);
359
+ }
360
+
361
+ async search(query: string): Promise<User[]> {
362
+ const response = await this.api.get<User[]>('/users', {
363
+ params: { q: query }
364
+ });
365
+ return response.data;
366
+ }
367
+ }
368
+
369
+ // ============================================
370
+ // 17. USAGE IN KIMU COMPONENT
371
+ // ============================================
372
+
373
+ /*
374
+ import { KimuComponentElement } from '@kimu/core';
375
+ import { KimuModuleManager } from '@kimu/core';
376
+
377
+ export class UserListComponent extends KimuComponentElement {
378
+ private api: any;
379
+ private users: User[] = [];
380
+ private loading = false;
381
+ private error: string | null = null;
382
+
383
+ async onInit() {
384
+ // Get API service
385
+ this.api = KimuModuleManager.getInstance().getService('api-core');
386
+
387
+ // Configure
388
+ this.api.configure({
389
+ baseURL: 'https://jsonplaceholder.typicode.com'
390
+ });
391
+
392
+ // Load data
393
+ await this.loadUsers();
394
+ }
395
+
396
+ async loadUsers() {
397
+ this.loading = true;
398
+ this.error = null;
399
+ this.onRender();
400
+
401
+ try {
402
+ const response = await this.api.get<User[]>('/users');
403
+ this.users = response.data;
404
+ } catch (error: any) {
405
+ this.error = error.message;
406
+ } finally {
407
+ this.loading = false;
408
+ this.onRender();
409
+ }
410
+ }
411
+
412
+ async createUser(userData: Omit<User, 'id'>) {
413
+ try {
414
+ const response = await this.api.post<User>('/users', userData);
415
+ this.users.push(response.data);
416
+ this.onRender();
417
+ } catch (error: any) {
418
+ console.error('Failed to create user:', error);
419
+ }
420
+ }
421
+
422
+ async deleteUser(id: number) {
423
+ try {
424
+ await this.api.delete(`/users/${id}`);
425
+ this.users = this.users.filter(u => u.id !== id);
426
+ this.onRender();
427
+ } catch (error: any) {
428
+ console.error('Failed to delete user:', error);
429
+ }
430
+ }
431
+ }
432
+ */