feat(DOC): 完整地审查了代码,更完整的注释说明
- 无代码修改
This commit is contained in:
@@ -3,7 +3,7 @@ import type { ApiClient } from './types';
|
||||
import type { AxiosError, AxiosResponse, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';
|
||||
import { ApiError } from './errors'; // 直接导入ApiError
|
||||
|
||||
// 创建API客户端实例的工厂函数
|
||||
// 创建API客户端实例的工厂函数(直接用axios的requestConfig)
|
||||
const createApiClientInstance = (config?: Partial<AxiosRequestConfig>): ApiClient => {
|
||||
// 创建axios实例
|
||||
const instance = axios.create({
|
||||
@@ -22,8 +22,9 @@ const createApiClientInstance = (config?: Partial<AxiosRequestConfig>): ApiClien
|
||||
// 响应拦截器数组
|
||||
const responseInterceptors: Array<number> = [];
|
||||
|
||||
// 添加请求拦截器
|
||||
// 添加请求拦截器(函数声明,非函数实体,接收两个钩子参数)
|
||||
const addRequestInterceptor = (
|
||||
// InternalAxiosRequestConfig 是 axios 内部专门用来管理监听器的
|
||||
onFulfilled?: (_config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>,
|
||||
onRejected?: (_error: unknown) => unknown
|
||||
): number => {
|
||||
@@ -46,12 +47,14 @@ const createApiClientInstance = (config?: Partial<AxiosRequestConfig>): ApiClien
|
||||
const removeRequestInterceptor = (handler: number): void => {
|
||||
const index = requestInterceptors.indexOf(handler);
|
||||
if (index !== -1) {
|
||||
// axios内部的移除逻辑
|
||||
instance.interceptors.request.eject(handler);
|
||||
// 封装管理的数据结构的移除(有点像LRU)
|
||||
requestInterceptors.splice(index, 1);
|
||||
}
|
||||
};
|
||||
|
||||
// 移除响应拦截器
|
||||
// 移除响应拦截器(同上)
|
||||
const removeResponseInterceptor = (handler: number): void => {
|
||||
const index = responseInterceptors.indexOf(handler);
|
||||
if (index !== -1) {
|
||||
@@ -60,19 +63,20 @@ const createApiClientInstance = (config?: Partial<AxiosRequestConfig>): ApiClien
|
||||
}
|
||||
};
|
||||
|
||||
// 设置默认配置
|
||||
// 设置默认配置(单例的)
|
||||
// defaults是最底层请求配置
|
||||
const setDefaults = (_config: Partial<AxiosRequestConfig>): void => {
|
||||
Object.assign(instance.defaults, _config);
|
||||
};
|
||||
|
||||
// 更新基础URL - 专门用于Runtime层配置
|
||||
// 更新基础URL,比上面那个更细
|
||||
const setBaseURL = (baseURL: string): void => {
|
||||
instance.defaults.baseURL = baseURL;
|
||||
};
|
||||
|
||||
// 创建新实例 - 用于跨域请求等特殊场景
|
||||
const createInstance = (config?: Partial<AxiosRequestConfig>): ApiClient => {
|
||||
// 创建新的axios实例
|
||||
// 创建新的axios实例(工厂模式+原型模式)
|
||||
const newInstance = axios.create({
|
||||
...instance.defaults,
|
||||
...config
|
||||
@@ -83,6 +87,8 @@ const createApiClientInstance = (config?: Partial<AxiosRequestConfig>): ApiClien
|
||||
const newResponseInterceptors: Array<number> = [];
|
||||
|
||||
// 复制拦截器
|
||||
// 其实逻辑有很大问题,包括闭包问题、handler不会对应、双重断言等等
|
||||
// 应该不复制,而是重新添加
|
||||
requestInterceptors.forEach(handler => {
|
||||
// 由于AxiosInterceptorManager类型定义中没有handlers属性,
|
||||
// 我们使用类型断言来访问内部属性
|
||||
@@ -107,6 +113,7 @@ const createApiClientInstance = (config?: Partial<AxiosRequestConfig>): ApiClien
|
||||
}
|
||||
});
|
||||
|
||||
// 同上
|
||||
responseInterceptors.forEach(handler => {
|
||||
// 由于AxiosInterceptorManager类型定义中没有handlers属性,
|
||||
// 我们使用类型断言来访问内部属性
|
||||
@@ -164,14 +171,14 @@ const createApiClientInstance = (config?: Partial<AxiosRequestConfig>): ApiClien
|
||||
newInstance.defaults.baseURL = baseURL;
|
||||
},
|
||||
createInstance: (newConfig?: Partial<AxiosRequestConfig>): ApiClient => {
|
||||
// 手动处理headers字段的类型转换
|
||||
// 手动处理headers字段的类型转换(这里newInstance实际上是原型)
|
||||
const { headers, ...otherDefaults } = newInstance.defaults;
|
||||
const configWithTypedHeaders: Partial<AxiosRequestConfig> = {
|
||||
...otherDefaults,
|
||||
...newConfig
|
||||
};
|
||||
|
||||
// 只有当headers存在时才添加
|
||||
// 只有当headers存在时才添加(因为defaults和axiosRequestConfig在这里类型不一样)
|
||||
if (headers) {
|
||||
// 手动转换headers字段,确保类型安全
|
||||
const convertedHeaders: Record<string, string | number | boolean> = {};
|
||||
@@ -275,5 +282,6 @@ const createApiClient = (config?: Partial<AxiosRequestConfig>) => {
|
||||
return createApiClientInstance(config);
|
||||
};
|
||||
|
||||
// 导出API客户端和工厂函数
|
||||
// 导出API客户端单例和工厂函数
|
||||
// 其实单例没用,因为baseurl总归不一样的,但是怎么说呢,单例也只需要改一个baseurl就好了
|
||||
export { apiClient, createApiClient };
|
||||
|
||||
@@ -13,7 +13,7 @@ export interface IApiError {
|
||||
}
|
||||
|
||||
/**
|
||||
* API错误类
|
||||
* API错误类(诶,就是要自定义)
|
||||
*/
|
||||
export class ApiError extends Error implements IApiError {
|
||||
public readonly code: number;
|
||||
@@ -27,7 +27,7 @@ export class ApiError extends Error implements IApiError {
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Axios错误创建API错误
|
||||
* 从Axios错误创建API错误(就是为了简化)
|
||||
*/
|
||||
static fromAxiosError(error: AxiosError): ApiError {
|
||||
if (!error.response) {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { chatApi } from './modules/chat';
|
||||
import { modelApi } from './modules/model';
|
||||
|
||||
/**
|
||||
* API 工厂函数,用于创建和管理 API 实例
|
||||
* API 工厂函数,用于创建和管理 API 实例(模块化封装)
|
||||
* 提供统一的 API 访问入口和配置管理
|
||||
*/
|
||||
export const createApi = (config?: Partial<AxiosRequestConfig>) => {
|
||||
@@ -36,5 +36,6 @@ export const createApi = (config?: Partial<AxiosRequestConfig>) => {
|
||||
export const api = createApi();
|
||||
|
||||
// 向后兼容的导出
|
||||
// 单例还是输出了
|
||||
export { apiClient } from './client';
|
||||
export { postApi, userApi, chatApi, modelApi } from './modules';
|
||||
|
||||
@@ -20,7 +20,7 @@ export const chatApi = (client: ApiClient) => ({
|
||||
return client.post('/chat/sessions', data);
|
||||
},
|
||||
|
||||
// 更新聊天会话
|
||||
// 更新聊天会话(纯充数那个)
|
||||
updateSession: ({ sessionId, ...data }: UpdateChatSessionRequest): Promise<{ session: ChatSession }> => {
|
||||
return client.put(`/chat/sessions/${sessionId}`, data);
|
||||
},
|
||||
@@ -30,7 +30,7 @@ export const chatApi = (client: ApiClient) => ({
|
||||
return client.post(`/chat/sessions/${data.sessionId}/messages`, data);
|
||||
},
|
||||
|
||||
// 获取聊天会话列表
|
||||
// 获取聊天会话列表(不知道写了个什么玩意儿,转什么,应该不用参数的)
|
||||
getSessions: (params?: GetChatSessionsRequest): Promise<GetChatSessionsResponse> => {
|
||||
const config: AxiosRequestConfig = params ? { params } : {};
|
||||
return client.get('/chat/sessions', config);
|
||||
|
||||
@@ -4,7 +4,7 @@ import { userApi as createUserApi } from './user';
|
||||
import { chatApi as createChatApi } from './chat';
|
||||
import { modelApi as createModelApi } from './model';
|
||||
|
||||
// 导出工厂函数
|
||||
// 导出工厂函数(和实例同名导出是什么鬼啊),幸好不是从这里导出的
|
||||
export { postApi as createPostApi, userApi as createUserApi, chatApi as createChatApi, modelApi as createModelApi };
|
||||
|
||||
// 向后兼容的默认实例
|
||||
|
||||
@@ -4,8 +4,6 @@ import type {
|
||||
LoginResponse,
|
||||
RegisterRequest,
|
||||
RegisterResponse,
|
||||
RefreshTokenRequest,
|
||||
RefreshTokenResponse,
|
||||
UserProfileUpdateRequest,
|
||||
UserProfileUpdateResponse,
|
||||
UserFollowRequest,
|
||||
@@ -28,11 +26,6 @@ export const userApi = (client: ApiClient) => ({
|
||||
return client.post('/auth/register', data);
|
||||
},
|
||||
|
||||
// 刷新令牌
|
||||
refreshToken: (data: RefreshTokenRequest): Promise<RefreshTokenResponse> => {
|
||||
return client.post('/auth/refresh', data);
|
||||
},
|
||||
|
||||
// 获取用户档案
|
||||
getProfile: (): Promise<UserProfileUpdateResponse> => {
|
||||
return client.get('/user/profile');
|
||||
|
||||
@@ -27,6 +27,7 @@ export interface ApiClient {
|
||||
removeResponseInterceptor(handler: number): void;
|
||||
}
|
||||
|
||||
// 后面的除了test里用,core里其他地方根本没用上过,都用泛型unknown了,这是不对的
|
||||
// API响应接口
|
||||
export interface ApiResponse<T = unknown> {
|
||||
success: boolean;
|
||||
|
||||
Reference in New Issue
Block a user