feat(clinical-web): 添加管理端与工作台 API 模块及类型定义

This commit is contained in:
yelan
2026-08-27 15:35:36 +08:00
parent c89cc6f95b
commit 24d8a9ece1
22 changed files with 797 additions and 17 deletions
+77
View File
@@ -0,0 +1,77 @@
import axios, {
type AxiosInstance,
type AxiosRequestConfig,
type AxiosResponse,
type InternalAxiosRequestConfig,
} from 'axios'
const DEFAULT_TIMEOUT = 15_000
class Request {
private readonly instance: AxiosInstance
constructor(baseURL = import.meta.env.VITE_API_BASE_URL || '/api', timeout = DEFAULT_TIMEOUT) {
this.instance = axios.create({
baseURL,
timeout,
withCredentials: true,
})
this.instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
const token = localStorage.getItem('token')
if (token) {
config.headers.set('Authorization', 'Bearer ' + token)
}
return config
})
this.instance.interceptors.response.use(
(response: AxiosResponse) => response,
(error: unknown) => Promise.reject(error),
)
}
request<T = unknown>(config: AxiosRequestConfig): Promise<T> {
return this.instance.request<T, AxiosResponse<T>>(config).then((response) => response.data)
}
get<T = unknown>(url: string, config: AxiosRequestConfig = {}): Promise<T> {
return this.request<T>({
...config,
url,
method: 'GET',
})
}
post<T = unknown>(url: string, data?: unknown, config: AxiosRequestConfig = {}): Promise<T> {
return this.request<T>({
...config,
url,
data,
method: 'POST',
})
}
put<T = unknown>(url: string, data?: unknown, config: AxiosRequestConfig = {}): Promise<T> {
return this.request<T>({
...config,
url,
data,
method: 'PUT',
})
}
delete<T = unknown>(url: string, config: AxiosRequestConfig = {}): Promise<T> {
return this.request<T>({
...config,
url,
method: 'DELETE',
})
}
}
export const request = new Request()
export default request