kun-galgame-vue/src/utils/request.ts

124 lines
2.7 KiB
TypeScript
Raw Normal View History

// 使用用户 store
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
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-10-03 13:29:07 +00:00
const successResponseArray = [200, 201, 202, 204, 205, 206]
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>(
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 ${useKUNGalgameUserStore().getToken()}`,
2023-09-26 15:54:17 +00:00
}
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-10-03 13:29:07 +00:00
// 不为 20X 则报错
if (!successResponseArray.includes(response.status)) {
2023-10-03 12:55:53 +00:00
// 处理错误token 过期
await onRequestError(response)
throw new Error('KUNGalgame Fetch Error occurred, but no problem')
}
2023-09-26 15:54:17 +00:00
const data: T = await response.json()
return data
}
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
}
2023-10-15 14:26:01 +00:00
const fetchPostWithFormData = async <T>(
url: string,
formData: FormData,
headers?: Record<string, string>
): Promise<T> => {
const options: FetchOptions = {
method: 'POST',
credentials: 'include',
headers: {
...headers,
},
body: formData,
}
return await kunFetchRequest<T>(url, options)
}
export { fetchGet, fetchPost, fetchPut, fetchDelete, fetchPostWithFormData }