feat(clinical-web): 添加管理端与工作台 API 模块及类型定义
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user