How the Generic CRUD Client works
GenericHttpClient
class that serves as a base for all HTTP client instances. This class extends a base HttpClient
and provides generic CRUD (Create, Read, Update, Delete) operations for a specified data type T
. Here’s a general overview of its structure and usage.
GenericHttpClient
is a generic class parameterized by T
, which represents the data type it handles (e.g., User, Project, etc.). It includes methods for typical operations like findOne
, find
, create
, updateOne
, deleteOne
, and others. Each of these methods sends an HTTP request to the server and returns the server’s response, usually as a promise that resolves to an instance or array of T
.
GenericHttpClient
, you would typically extend it to create a more specific HTTP client for a particular data type. Here’s an example for a user service:
HttpUserService
extends GenericHttpClient<User>
, meaning it will handle HTTP requests related to User
objects. It’s implemented as a singleton to ensure that only one instance of this service exists throughout the application.
HttpUserService
, for example, there are methods like getCurrentUser
, updateCurrentUser
, and getClientPermissions
that handle specific user-related requests.
GenericHttpClient
offers several advantages for managing HTTP requests in our application: