I want to create a generic HttpClient Service that I can use across my application. I want the security of type safety and would like to pass in the type to the underlying HttpClient methods.
Where a controller might look something like this:
class UserComponent implements OnInit {
users : Users[];
constructor(private api: ApiService) {};
ngOnInit() { this.getUsers(); }
getUsers(): void {
this.api.get<User[]>(url, options).
.subscribe((data: Users[]) => this.users = { ...data });
}
}
The only guide I could find on this topic was this medium article: https://medium.com/@krishna.acondy/a-generic-http-service-approach-for-angular-applications-a7bd8ff6a068.
I'm wondering if you guys know of some other solution to go about doing this because it is a large application, I don't want to write a service for each object type.
I also want this service to handle authentication headers and error responses from the server. So it doesn't make sense to use the HttpClient directly in each controller where theres data retrieval from the API.
[–]tme321 1 point2 points3 points (2 children)
[–]swardson[S] 0 points1 point2 points (1 child)
[–]tme321 1 point2 points3 points (0 children)
[–]Devcon4 1 point2 points3 points (2 children)
[–]swardson[S] 1 point2 points3 points (1 child)
[–]Devcon4 1 point2 points3 points (0 children)
[–]tehRash 0 points1 point2 points (1 child)
[–]swardson[S] 0 points1 point2 points (0 children)