API Requests
In frontend development, making API requests is a very common operation. To facilitate management and maintenance, and to standardize the handling of API requests, ensuring that all request parameters and response data have complete TypeScript type definitions, we have encapsulated a general-purpose API handling tool.
Getting Started
The API handling tool is currently maintained in the shared/src/utils/request
directory and supports various API request configuration methods for flexible selection.
Creating an instance:
// client.ts
import { createRequestClient } from "@milesight/shared/src/utils/request";
/** Business request header configuration */
const headerHandler = async () => {
// ...
};
/** Auto-refresh logic handling */
const autoJumpHandler = async () => {
// ...
};
/** API timeout tracking and reporting */
const trackerHandler = async () => {
// ...
};
const client = createRequestClient({
// API base URL
baseURL: "https://xxx.host.com",
// Static API request headers
headers: {
"x-headers": "xxx",
},
configHandlers: [headerHandler, autoJumpHandler],
onResponse(resp) {
// Todo: Global common response handling
return resp;
},
onResponseError(error) {
// Todo: Global common error handling
return error;
},
});
export default client;
Creating an API:
// services/http/user.ts
import { attachAPI } from "@milesight/shared/src/utils/request";
import client from "client.ts";
// APISchema is defined in @milesight/shared/types/common.d.ts
interface UserAPISchema extends APISchema {
/** Get user by ID */
getUser: {
request: {
id: number;
};
response: {
avatar: string;
id: number;
name: string;
};
};
/** Get the currently logged-in user */
getLoginUser: {
request: void;
response: {
id: number;
name: string;
avatar: string;
};
};
/** Create a new user */
createUser: {
request: {
avatar: string;
name: string;
enterpriseId: number;
};
response: {
avatar: string;
id: number;
name: string;
};
};
/** Download resource */
download: {
request: {
id: number;
};
response: unknown;
};
}
export default attachAPI<UserAPISchema>(client, {
// API error and response handling is delegated to the service layer, and can be defined by the business logic
onError(error) {
// Todo: Unified error handling for APIs
return error;
},
onResponse(resp) {
// Todo: Unified response handling for APIs
return resp;
},
// Supports 3 configuration methods for flexible selection
apis: {
// String configuration
getUser: "GET api/user/:id",
getLoginUser: "GET api/user/current",
// Object configuration
createUser: {
method: "POST",
path: "api/user/:enterpriseId",
// Special configuration
headers: { "x-abc": "xxx" },
},
// Function configuration
download: async (params) => {
const resp = await client.request({
url: "http://xxx.milesight.com",
method: "GET",
params,
headers: {
enterpriseId: "xxx",
},
responseType: "blob",
});
let result = resp.data.data;
// ...
return result;
},
},
});
Business usage:
import userAPI from "@/services/http/user.ts";
userAPI.getUser({ id: 123 }).then((resp) => {
console.log(resp.data.data);
});