Sobes.tech
Senior

You have a large old application with many API requests pulling from different URLs and returning different response formats. How would you organize this?

sobes.tech AI

Answer from AI

To organize a large old application with many API requests that use different URLs and return different formats, I would do the following:

  1. Create a unified abstraction layer for API requests — for example, a service or set of utilities that encapsulate the details of API calls. This will allow centralized management of URLs, headers, and error handling.

  2. Standardize response formats — write adapters or transformers that bring data to a single format, convenient for the application. This will free the rest of the code from having to deal with different structures.

  3. Use constants or configuration files for URLs and endpoints — to avoid scattering strings throughout the code.

  4. Add typing (if using TypeScript) — to ensure data correctness and simplify maintenance.

  5. Gradually refactor and replace outdated calls with new ones, using a unified interface.

Example of a simple API service in TypeScript:

interface ApiResponse<T> {
  data: T;
  status: number;
}

class ApiService {
  private baseUrl = 'https://api.example.com';

  async get<T>(endpoint: string): Promise<T> {
    const response = await fetch(`${this.baseUrl}/${endpoint}`);
    const json = await response.json();
    // Transform the response to a unified format
    return this.transformResponse<T>(json);
  }

  private transformResponse<T>(raw: any): T {
    // Logic for transforming different formats into a single one
    return raw as T;
  }
}

This approach will significantly improve the support and scalability of the application.

You have a large old application with many API… - sobes.tech