2023-09-24 13:46:04 +00:00
|
|
|
|
// 操作 cookie 的函数
|
|
|
|
|
import { getToken } from '@/utils/cookie'
|
2023-09-26 15:54:17 +00:00
|
|
|
|
// 错误处理函数
|
|
|
|
|
import { onRequestError } from '@/error/onRequestError'
|
2023-09-24 13:46:04 +00:00
|
|
|
|
|
2023-06-10 09:24:56 +00:00
|
|
|
|
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
|
2023-05-29 05:22:36 +00:00
|
|
|
|
|
2023-06-11 08:50:46 +00:00
|
|
|
|
export type FetchOptions = {
|
2023-06-10 09:24:56 +00:00
|
|
|
|
method: HttpMethod
|
2023-09-26 18:01:00 +00:00
|
|
|
|
credentials: 'include'
|
2023-06-10 09:24:56 +00:00
|
|
|
|
headers?: Record<string, string>
|
|
|
|
|
body?: BodyInit
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 07:34:12 +00:00
|
|
|
|
// fetch 请求函数
|
|
|
|
|
const kunFetchRequest = async <T>(
|
2023-05-29 05:22:36 +00:00
|
|
|
|
url: string,
|
2023-06-10 09:24:56 +00:00
|
|
|
|
options: FetchOptions
|
|
|
|
|
): Promise<T> => {
|
2023-09-26 15:54:17 +00:00
|
|
|
|
const baseUrl = import.meta.env.VITE_API_BASE_URL
|
|
|
|
|
const fullUrl = `${baseUrl}${url}`
|
2023-06-14 11:09:55 +00:00
|
|
|
|
|
2023-09-26 15:54:17 +00:00
|
|
|
|
// 在请求头中添加 token
|
|
|
|
|
const headers = {
|
|
|
|
|
...options.headers,
|
|
|
|
|
Authorization: `Bearer ${getToken()}`,
|
|
|
|
|
}
|
2023-06-10 09:24:56 +00:00
|
|
|
|
|
2023-09-26 15:54:17 +00:00
|
|
|
|
const response = await fetch(fullUrl, { ...options, headers })
|
2023-06-10 09:24:56 +00:00
|
|
|
|
|
2023-09-27 07:34:12 +00:00
|
|
|
|
// 处理错误,token 过期
|
|
|
|
|
await onRequestError(response)
|
2023-09-26 15:54:17 +00:00
|
|
|
|
|
|
|
|
|
const data: T = await response.json()
|
|
|
|
|
return data
|
2023-05-29 05:22:36 +00:00
|
|
|
|
}
|
2023-06-10 09:24:56 +00:00
|
|
|
|
|
|
|
|
|
const fetchGet = async <T>(
|
|
|
|
|
url: string,
|
|
|
|
|
headers?: Record<string, string>
|
|
|
|
|
): Promise<T> => {
|
|
|
|
|
const options: FetchOptions = {
|
|
|
|
|
method: 'GET',
|
2023-09-26 18:01:00 +00:00
|
|
|
|
credentials: 'include',
|
2023-06-10 09:24:56 +00:00
|
|
|
|
headers: headers,
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 07:34:12 +00:00
|
|
|
|
return await kunFetchRequest<T>(url, options)
|
2023-06-10 09:24:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fetchPost = async <T>(
|
|
|
|
|
url: string,
|
|
|
|
|
body?: Record<string, any>,
|
|
|
|
|
headers?: Record<string, string>
|
|
|
|
|
): Promise<T> => {
|
|
|
|
|
const options: FetchOptions = {
|
|
|
|
|
method: 'POST',
|
2023-09-26 18:01:00 +00:00
|
|
|
|
credentials: 'include',
|
2023-06-10 09:24:56 +00:00
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...headers,
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 07:34:12 +00:00
|
|
|
|
return await kunFetchRequest<T>(url, options)
|
2023-06-10 09:24:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fetchPut = async <T>(
|
|
|
|
|
url: string,
|
|
|
|
|
body?: Record<string, any>,
|
|
|
|
|
headers?: Record<string, string>
|
|
|
|
|
): Promise<T> => {
|
|
|
|
|
const options: FetchOptions = {
|
|
|
|
|
method: 'PUT',
|
2023-09-26 18:01:00 +00:00
|
|
|
|
credentials: 'include',
|
2023-06-10 09:24:56 +00:00
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...headers,
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 07:34:12 +00:00
|
|
|
|
return await kunFetchRequest<T>(url, options)
|
2023-06-10 09:24:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fetchDelete = async <T>(
|
|
|
|
|
url: string,
|
|
|
|
|
headers?: Record<string, string>
|
|
|
|
|
): Promise<T> => {
|
|
|
|
|
const options: FetchOptions = {
|
|
|
|
|
method: 'DELETE',
|
2023-09-26 18:01:00 +00:00
|
|
|
|
credentials: 'include',
|
2023-06-10 09:24:56 +00:00
|
|
|
|
headers: headers,
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 07:34:12 +00:00
|
|
|
|
return await kunFetchRequest<T>(url, options)
|
2023-06-10 09:24:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { fetchGet, fetchPost, fetchPut, fetchDelete }
|