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 |
|---|---|---|
|
| Numeric code representing the API response status (e.g., HTTP code or custom code). |
|
| The payload of the response, typed generically to accommodate any data shape. |
|
| Human-readable message describing the result of the request. |
|
| Numeric status typically indicating success or failure (may mirror |
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 |
|---|---|---|
|
| The data returned from a GET request. |
|
| Indicates whether the GET request is still loading. Defaults to |
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 |
|---|---|---|
|
| The data returned from the POST request. |
|
| Indicates whether the POST operation is still in progress. |
|
| 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
Generic Typing: All interfaces use generics (
<T = any>) to allow flexible typing of thedataproperty, enabling strong type checking tailored to specific API endpoint responses.Optional Loading Flags: The GET and POST response types include optional
loadingflags to support UI components or services that track asynchronous operation states.Index Signature in
ResponsePostType: The[key: string]: unknown;allows the interface to be extended with additional properties dynamically, useful for carrying extra metadata or response-specific fields.
Interaction with Other Parts of the System
These interfaces serve as foundational data contracts used by API service layers, network request handlers, and UI components.
They enforce uniform response handling patterns, reducing bugs and improving developer experience when integrating new API endpoints.
They likely interact with HTTP client utilities (e.g., Axios or Fetch wrappers) and React/Vue components or state management stores that consume API data.
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.