base.ts


Overview

The base.ts file defines TypeScript interfaces that standardize the structure of API response data across the application. These interfaces provide consistent typing for different kinds of response payloads, such as general responses, GET request responses, and POST request responses. By doing so, base.ts promotes type safety, code clarity, and easier maintenance when handling API responses in various parts of the system.


Interfaces and Their Purpose

1. ResponseType<T = any>

Defines the overall structure of a generic API response, typically used for responses that include a status code, a message, and data payload.

Properties

Property

Type

Description

code

number

Numeric code representing the API response status (e.g., HTTP code or custom code).

data

T (generic)

The payload of the response, typed generically to accommodate any data shape.

message

string

Human-readable message describing the result of the request.

status

number

Numeric status typically indicating success or failure (may mirror code).

Usage Example

function handleApiResponse(response: ResponseType<User>) {
  if (response.code === 200) {
    console.log('User data:', response.data);
  } else {
    console.error('Error:', response.message);
  }
}

2. ResponseGetType<T = any>

Represents the structure for GET request responses where the data is returned along with an optional loading state, useful for UI components managing asynchronous data fetching.

Properties

Property

Type

Description

data

T (generic)

The data returned from a GET request.

loading

boolean (optional)

Indicates whether the GET request is still loading. Defaults to false if omitted.

Usage Example

const userResponse: ResponseGetType<User> = {
  data: { id: 1, name: 'John Doe' },
  loading: false
};

3. ResponsePostType<T = any>

Defines the response format for POST requests, which includes data, optional loading status, and allows additional arbitrary properties to accommodate extended metadata or response attributes.

Properties

Property

Type

Description

data

T (generic)

The data returned from the POST request.

loading

boolean (optional)

Indicates whether the POST operation is still in progress.

[key: string]

unknown

Index signature to allow any other additional properties not predefined, supporting extensibility.

Usage Example

const postResponse: ResponsePostType<Order> = {
  data: { orderId: 123, status: 'confirmed' },
  loading: false,
  timestamp: Date.now(),
};

Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class ResponseType~T~ {
        +code: number
        +data: T
        +message: string
        +status: number
    }

    class ResponseGetType~T~ {
        +data: T
        +loading?: boolean
    }

    class ResponsePostType~T~ {
        +data: T
        +loading?: boolean
        +[key: string]: unknown
    }

The diagram above shows the three interfaces, their generic nature, and their key properties, illustrating their structural relationship as distinct but related response contracts.


Summary

base.ts is a utility file that provides fundamental TypeScript interfaces to model API responses consistently. It enables flexible yet strongly typed handling of data returned from server endpoints, accommodating various HTTP methods and additional metadata needs. This file is a critical part of the data flow infrastructure in the application, ensuring that components and services can rely on predictable and well-typed response shapes.