120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
/**
|
|
* API 类型定义文件
|
|
*/
|
|
|
|
import type { AxiosResponse, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';
|
|
|
|
// API客户端接口定义
|
|
export interface ApiClient {
|
|
request<T = unknown>(config: AxiosRequestConfig): Promise<T>;
|
|
get<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
post<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T>;
|
|
put<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T>;
|
|
delete<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
patch<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T>;
|
|
setDefaults(config: Partial<AxiosRequestConfig>): void;
|
|
setBaseURL(baseURL: string): void;
|
|
createInstance(config?: Partial<AxiosRequestConfig>): ApiClient;
|
|
addRequestInterceptor(
|
|
onFulfilled?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>,
|
|
onRejected?: (error: unknown) => unknown
|
|
): number;
|
|
addResponseInterceptor(
|
|
onFulfilled?: (response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>,
|
|
onRejected?: (error: unknown) => unknown
|
|
): number;
|
|
removeRequestInterceptor(handler: number): void;
|
|
removeResponseInterceptor(handler: number): void;
|
|
}
|
|
|
|
// API响应接口
|
|
export interface ApiResponse<T = unknown> {
|
|
success: boolean;
|
|
data: T;
|
|
message?: string;
|
|
code?: number;
|
|
}
|
|
|
|
// 分页响应接口
|
|
export interface PaginatedResponse<T = unknown> {
|
|
items: T[];
|
|
total: number;
|
|
page: number;
|
|
pageSize: number;
|
|
totalPages: number;
|
|
}
|
|
|
|
// 错误响应接口
|
|
export interface ErrorResponse {
|
|
success: false;
|
|
message: string;
|
|
code?: number;
|
|
details?: unknown;
|
|
}
|
|
|
|
// API端点常量
|
|
export const API_ENDPOINTS = {
|
|
// 用户相关
|
|
USER_LOGIN: '/auth/login',
|
|
USER_REGISTER: '/auth/register',
|
|
USER_LOGOUT: '/auth/logout',
|
|
USER_PROFILE: '/user/profile',
|
|
USER_CURRENT: '/auth/me',
|
|
|
|
// 聊天相关
|
|
CHAT_LIST: '/chat/list',
|
|
CHAT_CREATE: '/chat/create',
|
|
CHAT_DETAIL: '/chat/detail',
|
|
CHAT_DELETE: '/chat/delete',
|
|
CHAT_MESSAGE: '/chat/message',
|
|
|
|
// 模型相关
|
|
MODEL_LIST: '/model/list',
|
|
MODEL_DETAIL: '/model/detail',
|
|
|
|
// 帖子相关
|
|
POST_LIST: '/post/list',
|
|
POST_DETAIL: '/post/detail',
|
|
POST_CREATE: '/post/create',
|
|
POST_UPDATE: '/post/update',
|
|
POST_DELETE: '/post/delete'
|
|
} as const;
|
|
|
|
// API状态码
|
|
export const ApiStatusCode = {
|
|
OK: 200,
|
|
CREATED: 201,
|
|
NO_CONTENT: 204,
|
|
BAD_REQUEST: 400,
|
|
UNAUTHORIZED: 401,
|
|
FORBIDDEN: 403,
|
|
NOT_FOUND: 404,
|
|
INTERNAL_SERVER_ERROR: 500
|
|
} as const;
|
|
|
|
// API错误类型
|
|
export const ApiErrorType = {
|
|
NETWORK_ERROR: 'NETWORK_ERROR',
|
|
VALIDATION_ERROR: 'VALIDATION_ERROR',
|
|
AUTHENTICATION_ERROR: 'AUTHENTICATION_ERROR',
|
|
AUTHORIZATION_ERROR: 'AUTHORIZATION_ERROR',
|
|
NOT_FOUND_ERROR: 'NOT_FOUND_ERROR',
|
|
SERVER_ERROR: 'SERVER_ERROR',
|
|
UNKNOWN_ERROR: 'UNKNOWN_ERROR'
|
|
} as const;
|
|
|
|
// 默认请求配置
|
|
export const DEFAULT_REQUEST_CONFIG = {
|
|
timeout: 10000,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
withCredentials: true
|
|
} as const;
|
|
|
|
// 默认分页配置
|
|
export const DEFAULT_PAGINATION_CONFIG = {
|
|
page: 1,
|
|
pageSize: 20
|
|
} as const;
|