@codex-ts/core-lib 1.0.1 → 1.0.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.
- package/{fesm2022/codex-ts-core-lib.mjs → dist/core-lib/fesm2022/core-lib.mjs} +1 -1
- package/dist/core-lib/fesm2022/core-lib.mjs.map +1 -0
- package/ng-package.json +7 -0
- package/package.json +63 -42
- package/src/index.ts +44 -0
- package/src/lib/components/base-form.component.ts +328 -0
- package/src/lib/components/base-list-page.component.ts +328 -0
- package/src/lib/components/base-page.component.ts +33 -0
- package/src/lib/components/base-tabbed-form.component.ts +345 -0
- package/src/lib/components/formly/core-formly.module.ts +34 -0
- package/src/lib/components/formly/field.interface.ts +15 -0
- package/src/lib/components/formly/form-field.wrapper.ts +94 -0
- package/src/lib/components/formly/formly.constants.ts +14 -0
- package/src/lib/components/formly/primeng-formly.module.ts +77 -0
- package/src/lib/components/formly/types/checkbox.type.ts +43 -0
- package/src/lib/components/formly/types/date.type.ts +88 -0
- package/src/lib/components/formly/types/multiselect.type.ts +61 -0
- package/src/lib/components/formly/types/numeric-input.type.ts +45 -0
- package/src/lib/components/formly/types/radio.type.ts +47 -0
- package/src/lib/components/formly/types/select.type.ts +54 -0
- package/src/lib/components/formly/types/text-input.type.ts +33 -0
- package/src/lib/components/formly/types/textarea.type.ts +37 -0
- package/src/lib/constants/app.constants.ts +15 -0
- package/src/lib/constants/app.messages.ts +36 -0
- package/src/lib/enums/component-context.enum.ts +6 -0
- package/src/lib/enums/tabbed-form-type.enum.ts +4 -0
- package/src/lib/models/pagination/filter-criteria.interface.ts +19 -0
- package/src/lib/models/pagination/page-request.interface.ts +13 -0
- package/src/lib/models/pagination/page.interface.ts +9 -0
- package/src/lib/models/pagination/sort-criteria.interface.ts +4 -0
- package/src/lib/pipes/indian-date.pipe.ts +46 -0
- package/src/lib/services/base-crud.service.ts +81 -0
- package/src/lib/services/entity-registry.service.ts +51 -0
- package/src/lib/services/entity.service.ts +10 -0
- package/src/lib/services/enum-registry.service.ts +66 -0
- package/src/lib/services/formly-config.service.ts +50 -0
- package/src/lib/services/http-utility.service.example.ts +253 -0
- package/src/lib/services/http-utility.service.md +175 -0
- package/src/lib/services/http-utility.service.ts +389 -0
- package/src/lib/services/notification.service.ts +35 -0
- package/src/lib/services/reference-data-provider.service.ts +28 -0
- package/src/lib/services/utils.service.ts +154 -0
- package/fesm2022/codex-ts-core-lib.mjs.map +0 -1
- /package/{index.d.ts → dist/core-lib/index.d.ts} +0 -0
- /package/{lib → dist/core-lib/lib}/components/base-form.component.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/base-list-page.component.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/base-page.component.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/base-tabbed-form.component.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/core-formly.module.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/form-field.wrapper.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/formly.constants.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/types/checkbox.type.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/types/date.type.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/types/multiselect.type.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/types/numeric-input.type.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/types/radio.type.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/types/select.type.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/types/text-input.type.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/components/formly/types/textarea.type.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/constants/app.constants.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/constants/app.messages.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/enums/component-context.enum.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/enums/tabbed-form-type.enum.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/models/pagination/filter-criteria.interface.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/models/pagination/page-request.interface.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/models/pagination/page.interface.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/models/pagination/sort-criteria.interface.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/pipes/indian-date.pipe.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/base-crud.service.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/entity-registry.service.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/entity.service.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/enum-registry.service.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/formly-config.service.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/http-utility.service.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/notification.service.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/reference-data-provider.service.d.ts +0 -0
- /package/{lib → dist/core-lib/lib}/services/utils.service.d.ts +0 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { UtilsService } from './utils.service';
|
|
3
|
+
|
|
4
|
+
@Injectable({
|
|
5
|
+
providedIn: 'root'
|
|
6
|
+
})
|
|
7
|
+
export class EnumRegistryService {
|
|
8
|
+
private enumMap = new Map<string, any>();
|
|
9
|
+
|
|
10
|
+
constructor(private utilsService: UtilsService) {}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Initialize global enums
|
|
14
|
+
*/
|
|
15
|
+
static registerGlobalEnums(enums: Record<string, any>) {
|
|
16
|
+
return {
|
|
17
|
+
provide: EnumRegistryService,
|
|
18
|
+
useFactory: (utilsService: UtilsService) => {
|
|
19
|
+
const service = new EnumRegistryService(utilsService);
|
|
20
|
+
Object.entries(enums).forEach(([name, enumType]) => {
|
|
21
|
+
service.register(name, enumType);
|
|
22
|
+
});
|
|
23
|
+
return service;
|
|
24
|
+
},
|
|
25
|
+
deps: [UtilsService]
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Register an enum type with the service
|
|
31
|
+
* @param name Name to register the enum under
|
|
32
|
+
* @param enumType The enum type to register
|
|
33
|
+
*/
|
|
34
|
+
register(name: string, enumType: any) {
|
|
35
|
+
this.enumMap.set(name, enumType);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Get formly-compatible options array for a registered enum
|
|
40
|
+
* @param name Name of the registered enum
|
|
41
|
+
* @returns Array of {label, value} pairs suitable for formly select fields
|
|
42
|
+
*/
|
|
43
|
+
getEnumValues(name: string) {
|
|
44
|
+
const enumType = this.enumMap.get(name);
|
|
45
|
+
if (!enumType) {
|
|
46
|
+
console.warn(`Enum type "${name}" not found in registry`);
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
return this.utilsService.getEnumValues(enumType);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Check if an enum type is registered
|
|
54
|
+
* @param name Name of the enum to check
|
|
55
|
+
*/
|
|
56
|
+
hasEnum(name: string): boolean {
|
|
57
|
+
return this.enumMap.has(name);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Clear all registered enums
|
|
62
|
+
*/
|
|
63
|
+
clear() {
|
|
64
|
+
this.enumMap.clear();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { FormlyFieldConfig } from '@ngx-formly/core';
|
|
3
|
+
import { PageMode } from '../constants/app.constants';
|
|
4
|
+
import { EnumRegistryService } from './enum-registry.service';
|
|
5
|
+
|
|
6
|
+
@Injectable({
|
|
7
|
+
providedIn: 'root'
|
|
8
|
+
})
|
|
9
|
+
export class FormlyConfigService {
|
|
10
|
+
constructor(private enumRegistry: EnumRegistryService) {}
|
|
11
|
+
|
|
12
|
+
getFormConfig(
|
|
13
|
+
fields: FormlyFieldConfig[],
|
|
14
|
+
mode: PageMode,
|
|
15
|
+
dynamicOptions?: Record<string, any>
|
|
16
|
+
): FormlyFieldConfig[] {
|
|
17
|
+
const commonConfig = {
|
|
18
|
+
props: {
|
|
19
|
+
disabled: mode === 'view'
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return fields.map((field: FormlyFieldConfig) => {
|
|
24
|
+
// Apply dynamic options from child component
|
|
25
|
+
if (dynamicOptions && field.key && typeof field.key === 'string') {
|
|
26
|
+
const fieldOptions = dynamicOptions[field.key];
|
|
27
|
+
if (fieldOptions) {
|
|
28
|
+
field.props = {
|
|
29
|
+
...field.props,
|
|
30
|
+
...fieldOptions
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Handle field groups recursively
|
|
36
|
+
if (field.fieldGroup) {
|
|
37
|
+
field.fieldGroup = this.getFormConfig(field.fieldGroup, mode, dynamicOptions);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
...field,
|
|
42
|
+
...commonConfig,
|
|
43
|
+
props: {
|
|
44
|
+
...field.props,
|
|
45
|
+
...commonConfig.props
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { Component, OnInit } from '@angular/core';
|
|
2
|
+
import { HttpHeaders } from '@angular/common/http';
|
|
3
|
+
import { HttpUtilityService } from './http-utility.service';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Example component showing how to use the HttpUtilityService
|
|
7
|
+
* This is just a demonstration and not an actual component to use.
|
|
8
|
+
*/
|
|
9
|
+
@Component({
|
|
10
|
+
selector: 'app-http-utility-example',
|
|
11
|
+
template: `
|
|
12
|
+
<div>
|
|
13
|
+
<h2>HTTP Utility Service Example</h2>
|
|
14
|
+
<div *ngIf="loading">Loading...</div>
|
|
15
|
+
<div *ngIf="user">
|
|
16
|
+
<p>User ID: {{ user.id }}</p>
|
|
17
|
+
<p>Name: {{ user.name }}</p>
|
|
18
|
+
<p>Email: {{ user.email }}</p>
|
|
19
|
+
</div>
|
|
20
|
+
<div *ngIf="errorMessage" class="error">{{ errorMessage }}</div>
|
|
21
|
+
<button (click)="loadUser()">Load User</button>
|
|
22
|
+
<button (click)="createUser()">Create User</button>
|
|
23
|
+
<button (click)="updateUser()">Update User</button>
|
|
24
|
+
<button (click)="deleteUser()">Delete User</button>
|
|
25
|
+
<button (click)="uploadUserPhoto()">Upload Photo</button>
|
|
26
|
+
<button (click)="loadMultipleResources()">Load Multiple Resources</button>
|
|
27
|
+
<button (click)="downloadReport()">Download Report</button>
|
|
28
|
+
</div>
|
|
29
|
+
`
|
|
30
|
+
})
|
|
31
|
+
export class HttpUtilityExampleComponent implements OnInit {
|
|
32
|
+
loading = false;
|
|
33
|
+
user: any;
|
|
34
|
+
errorMessage = '';
|
|
35
|
+
|
|
36
|
+
// Make sure to inject the service in the constructor
|
|
37
|
+
constructor(private httpUtility: HttpUtilityService) {
|
|
38
|
+
// Subscribe to the loading state observable
|
|
39
|
+
this.httpUtility.loading$.subscribe(isLoading => {
|
|
40
|
+
this.loading = isLoading;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
ngOnInit(): void {
|
|
45
|
+
// Initial data loading
|
|
46
|
+
this.loadUser();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Example of a GET request with parameters
|
|
51
|
+
*/
|
|
52
|
+
loadUser(): void {
|
|
53
|
+
// Clear previous error messages
|
|
54
|
+
this.errorMessage = '';
|
|
55
|
+
|
|
56
|
+
// Creating parameters with the utility method
|
|
57
|
+
const params = this.httpUtility.createParams({
|
|
58
|
+
id: '12345',
|
|
59
|
+
includeDetails: true
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Example API call with options
|
|
63
|
+
this.httpUtility.get<any>('/api/users/current', {
|
|
64
|
+
params,
|
|
65
|
+
retries: 3,
|
|
66
|
+
timeoutMs: 10000
|
|
67
|
+
}).subscribe({
|
|
68
|
+
next: (data) => {
|
|
69
|
+
console.log('User data loaded:', data);
|
|
70
|
+
this.user = data;
|
|
71
|
+
},
|
|
72
|
+
error: (error) => {
|
|
73
|
+
console.error('Error loading user:', error);
|
|
74
|
+
this.errorMessage = error.message;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Example of a POST request with custom headers
|
|
81
|
+
*/
|
|
82
|
+
createUser(): void {
|
|
83
|
+
this.errorMessage = '';
|
|
84
|
+
|
|
85
|
+
// Example user data
|
|
86
|
+
const newUser = {
|
|
87
|
+
name: 'John Doe',
|
|
88
|
+
email: 'john.doe@example.com',
|
|
89
|
+
role: 'user'
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// Custom headers
|
|
93
|
+
const headers = new HttpHeaders({
|
|
94
|
+
'X-Custom-Header': 'some-value'
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
this.httpUtility.post<any>('/api/users', newUser, {
|
|
98
|
+
headers,
|
|
99
|
+
withCredentials: true
|
|
100
|
+
}).subscribe({
|
|
101
|
+
next: (response) => {
|
|
102
|
+
console.log('User created:', response);
|
|
103
|
+
this.user = response;
|
|
104
|
+
},
|
|
105
|
+
error: (error) => {
|
|
106
|
+
console.error('Error creating user:', error);
|
|
107
|
+
this.errorMessage = error.message;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Example of a PUT request
|
|
114
|
+
*/
|
|
115
|
+
updateUser(): void {
|
|
116
|
+
if (!this.user) {
|
|
117
|
+
this.errorMessage = 'No user loaded to update';
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const updatedUser = {
|
|
122
|
+
...this.user,
|
|
123
|
+
name: 'Updated Name'
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
this.httpUtility.put<any>(`/api/users/${this.user.id}`, updatedUser)
|
|
127
|
+
.subscribe({
|
|
128
|
+
next: (response) => {
|
|
129
|
+
console.log('User updated:', response);
|
|
130
|
+
this.user = response;
|
|
131
|
+
},
|
|
132
|
+
error: (error) => {
|
|
133
|
+
console.error('Error updating user:', error);
|
|
134
|
+
this.errorMessage = error.message;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Example of a DELETE request
|
|
141
|
+
*/
|
|
142
|
+
deleteUser(): void {
|
|
143
|
+
if (!this.user) {
|
|
144
|
+
this.errorMessage = 'No user loaded to delete';
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
this.httpUtility.delete<void>(`/api/users/${this.user.id}`)
|
|
149
|
+
.subscribe({
|
|
150
|
+
next: () => {
|
|
151
|
+
console.log('User deleted successfully');
|
|
152
|
+
this.user = null;
|
|
153
|
+
},
|
|
154
|
+
error: (error) => {
|
|
155
|
+
console.error('Error deleting user:', error);
|
|
156
|
+
this.errorMessage = error.message;
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Example of file upload with progress tracking
|
|
163
|
+
*/
|
|
164
|
+
uploadUserPhoto(): void {
|
|
165
|
+
// In a real application, you would get this from a file input
|
|
166
|
+
// This is just for demonstration purposes
|
|
167
|
+
const file = new File(['dummy content'], 'profile-photo.jpg', { type: 'image/jpeg' });
|
|
168
|
+
|
|
169
|
+
// Additional form data
|
|
170
|
+
const additionalData = {
|
|
171
|
+
userId: this.user?.id || 'unknown',
|
|
172
|
+
description: 'Profile photo'
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// Define the expected response type
|
|
176
|
+
interface PhotoUploadResponse {
|
|
177
|
+
photoUrl: string;
|
|
178
|
+
timestamp: string;
|
|
179
|
+
fileSize: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
this.httpUtility.uploadFile<PhotoUploadResponse>('/api/users/photo', file, additionalData, {
|
|
183
|
+
onProgress: (percentage) => {
|
|
184
|
+
console.log(`Upload progress: ${percentage}%`);
|
|
185
|
+
// Update UI with progress
|
|
186
|
+
}
|
|
187
|
+
}).subscribe({
|
|
188
|
+
next: (response) => {
|
|
189
|
+
console.log('Photo uploaded:', response);
|
|
190
|
+
// Update user with new photo URL
|
|
191
|
+
this.user = {
|
|
192
|
+
...this.user,
|
|
193
|
+
photoUrl: response.photoUrl
|
|
194
|
+
};
|
|
195
|
+
},
|
|
196
|
+
error: (error) => {
|
|
197
|
+
console.error('Error uploading photo:', error);
|
|
198
|
+
this.errorMessage = error.message;
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Example of batch requests
|
|
205
|
+
*/
|
|
206
|
+
loadMultipleResources(): void {
|
|
207
|
+
// Create multiple requests
|
|
208
|
+
const userRequest = this.httpUtility.get<any>('/api/users/current');
|
|
209
|
+
const postsRequest = this.httpUtility.get<any[]>('/api/posts');
|
|
210
|
+
const notificationsRequest = this.httpUtility.get<any[]>('/api/notifications');
|
|
211
|
+
|
|
212
|
+
// Execute all requests in parallel
|
|
213
|
+
this.httpUtility.batchRequests([
|
|
214
|
+
userRequest,
|
|
215
|
+
postsRequest,
|
|
216
|
+
notificationsRequest
|
|
217
|
+
]).subscribe({
|
|
218
|
+
next: ([user, posts, notifications]) => {
|
|
219
|
+
console.log('All data loaded:', { user, posts, notifications });
|
|
220
|
+
this.user = user;
|
|
221
|
+
// Handle other data
|
|
222
|
+
},
|
|
223
|
+
error: (error) => {
|
|
224
|
+
console.error('Error loading batch data:', error);
|
|
225
|
+
this.errorMessage = error.message;
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Example of file download
|
|
232
|
+
*/
|
|
233
|
+
downloadReport(): void {
|
|
234
|
+
this.httpUtility.downloadFile('/api/reports/user-activity', 'user-activity.pdf')
|
|
235
|
+
.subscribe({
|
|
236
|
+
next: (blob) => {
|
|
237
|
+
// Create a download link and trigger it
|
|
238
|
+
const url = window.URL.createObjectURL(blob);
|
|
239
|
+
const a = document.createElement('a');
|
|
240
|
+
a.href = url;
|
|
241
|
+
a.download = 'user-activity.pdf';
|
|
242
|
+
document.body.appendChild(a);
|
|
243
|
+
a.click();
|
|
244
|
+
window.URL.revokeObjectURL(url);
|
|
245
|
+
a.remove();
|
|
246
|
+
},
|
|
247
|
+
error: (error) => {
|
|
248
|
+
console.error('Error downloading file:', error);
|
|
249
|
+
this.errorMessage = error.message;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# HttpUtilityService
|
|
2
|
+
|
|
3
|
+
A general-purpose HTTP utility service that extends Angular's HttpClient with additional functionality for common HTTP operations.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Enhanced HTTP Methods**: Typed GET, POST, PUT, DELETE methods with additional options
|
|
8
|
+
- **Loading State Management**: Built-in loading state tracking
|
|
9
|
+
- **Error Handling**: Standardized error handling with message display
|
|
10
|
+
- **Request Configuration**:
|
|
11
|
+
- Timeout configuration
|
|
12
|
+
- Retry capabilities
|
|
13
|
+
- Custom headers and params support
|
|
14
|
+
- **Specialized Operations**:
|
|
15
|
+
- File uploads with progress tracking
|
|
16
|
+
- File downloads
|
|
17
|
+
- Batch request handling
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
The service is part of the core library. It's already registered with the Angular DI system, so you can inject it directly into your components and services.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Basic Import
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { HttpUtilityService } from '@core/core-lib';
|
|
29
|
+
|
|
30
|
+
@Component({
|
|
31
|
+
// ...
|
|
32
|
+
})
|
|
33
|
+
export class MyComponent {
|
|
34
|
+
constructor(private httpUtil: HttpUtilityService) {}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### GET Requests
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
// Simple GET request
|
|
42
|
+
this.httpUtil.get<UserData>('/api/users/123').subscribe({
|
|
43
|
+
next: (data) => console.log('User data:', data),
|
|
44
|
+
error: (err) => console.error('Error fetching user:', err)
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// GET with parameters and options
|
|
48
|
+
const params = this.httpUtil.createParams({
|
|
49
|
+
filter: 'active',
|
|
50
|
+
sort: 'name',
|
|
51
|
+
page: 1
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
this.httpUtil.get<PagedResponse<UserData>>('/api/users', {
|
|
55
|
+
params,
|
|
56
|
+
retries: 2,
|
|
57
|
+
timeoutMs: 5000
|
|
58
|
+
}).subscribe({
|
|
59
|
+
next: (data) => this.handleData(data),
|
|
60
|
+
error: (err) => this.handleError(err)
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### POST Requests
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
const newUser = {
|
|
68
|
+
name: 'John Doe',
|
|
69
|
+
email: 'john@example.com'
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
this.httpUtil.post<UserData>('/api/users', newUser).subscribe({
|
|
73
|
+
next: (data) => console.log('User created:', data),
|
|
74
|
+
error: (err) => console.error('Error creating user:', err)
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### PUT Requests
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const updatedUser = {
|
|
82
|
+
id: '123',
|
|
83
|
+
name: 'John Smith',
|
|
84
|
+
email: 'john@example.com'
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
this.httpUtil.put<UserData>(`/api/users/${updatedUser.id}`, updatedUser).subscribe({
|
|
88
|
+
next: (data) => console.log('User updated:', data),
|
|
89
|
+
error: (err) => console.error('Error updating user:', err)
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### DELETE Requests
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
this.httpUtil.delete<void>('/api/users/123').subscribe({
|
|
97
|
+
next: () => console.log('User deleted successfully'),
|
|
98
|
+
error: (err) => console.error('Error deleting user:', err)
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### File Upload with Progress
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Get file from a file input
|
|
106
|
+
const fileInput = event.target.files[0];
|
|
107
|
+
|
|
108
|
+
// Additional form data
|
|
109
|
+
const metadata = {
|
|
110
|
+
description: 'Profile photo',
|
|
111
|
+
userId: '123'
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
this.httpUtil.uploadFile<{url: string}>('/api/upload', fileInput, metadata, {
|
|
115
|
+
onProgress: (percent) => this.uploadProgress = percent
|
|
116
|
+
}).subscribe({
|
|
117
|
+
next: (response) => this.imageUrl = response.url,
|
|
118
|
+
error: (err) => console.error('Upload failed:', err)
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### File Download
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
this.httpUtil.downloadFile('/api/reports/123', 'report.pdf').subscribe({
|
|
126
|
+
next: (blob) => {
|
|
127
|
+
// Create download link
|
|
128
|
+
const url = window.URL.createObjectURL(blob);
|
|
129
|
+
const a = document.createElement('a');
|
|
130
|
+
a.href = url;
|
|
131
|
+
a.download = 'report.pdf';
|
|
132
|
+
document.body.appendChild(a);
|
|
133
|
+
a.click();
|
|
134
|
+
window.URL.revokeObjectURL(url);
|
|
135
|
+
a.remove();
|
|
136
|
+
},
|
|
137
|
+
error: (err) => console.error('Download failed:', err)
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Batch Requests
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
// Create multiple requests
|
|
145
|
+
const userRequest = this.httpUtil.get<UserData>('/api/users/123');
|
|
146
|
+
const ordersRequest = this.httpUtil.get<Order[]>(`/api/users/123/orders`);
|
|
147
|
+
|
|
148
|
+
// Execute all requests in parallel
|
|
149
|
+
this.httpUtil.batchRequests([userRequest, ordersRequest]).subscribe({
|
|
150
|
+
next: ([user, orders]) => {
|
|
151
|
+
this.user = user;
|
|
152
|
+
this.orders = orders;
|
|
153
|
+
},
|
|
154
|
+
error: (err) => console.error('Error fetching data:', err)
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Loading State
|
|
159
|
+
|
|
160
|
+
The service exposes a loading state observable that you can subscribe to:
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
this.httpUtil.loading$.subscribe(isLoading => {
|
|
164
|
+
this.isLoading = isLoading;
|
|
165
|
+
// Update UI based on loading state
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Error Handling
|
|
170
|
+
|
|
171
|
+
The service automatically handles common HTTP errors and displays appropriate messages using the MessageService if it's provided in the constructor.
|
|
172
|
+
|
|
173
|
+
## Advanced Usage
|
|
174
|
+
|
|
175
|
+
For more complex usage examples, see the `http-utility.service.example.ts` file.
|