In our application, we have a 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.

Structure of GenericHttpClient

import { HttpClient } from "./http.client";

export class GenericHttpClient<T> extends HttpClient {
  path: string;

  constructor(path: string) {
    super();
    this.path = path;
  }

  // CRUD operations and other methods...
}
The 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.

Usage of GenericHttpClient

To use the 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:
import { User } from "../../schemas/user.schemas/user.schema";
import { GenericHttpClient } from "./config/http.generic.client";

export class HttpUserService extends GenericHttpClient<User> {
  static _instance: HttpUserService;

  static getInstance(): HttpUserService {
    if (this._instance == null) {
      this._instance = new HttpUserService("/users");
    }
    return this._instance;
  }

  // Specific methods for the user...
}
In this example, 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.

Specific Methods

While the generic client provides basic CRUD operations, you can also define specific methods for more particular needs. In HttpUserService, for example, there are methods like getCurrentUser, updateCurrentUser, and getClientPermissions that handle specific user-related requests.

Benefits of Using a Generic HTTP Client

The GenericHttpClient offers several advantages for managing HTTP requests in our application:
  • Reusability: The generic client serves as a reusable template for handling all types of data. This significantly reduces code duplication by providing a common set of methods for CRUD operations.
  • Consistency: By having a standard approach to perform HTTP requests, we ensure consistency across different parts of the application. This uniformity makes the code easier to understand and work with.
  • Maintainability: Centralizing the common logic for HTTP requests in the generic client simplifies the maintenance and updating of the code. Any changes to the HTTP request handling can be made in one place, affecting all clients that extend the generic client.
By adhering to this pattern, developers can efficiently create new HTTP clients for different data types, leading to a clean, maintainable, and consistent codebase.