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

124 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-10-23 10:34:28 +00:00
// Using the user store
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
2023-10-23 10:34:28 +00:00
// Error handling function
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-10-23 10:34:28 +00:00
// Fetch request function
2023-09-27 07:34:12 +00:00
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-10-23 10:34:28 +00:00
// Add the token to the request headers
2023-09-26 15:54:17 +00:00
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-23 10:34:28 +00:00
// If not 20X, then throw an error
2023-10-03 13:29:07 +00:00
if (!successResponseArray.includes(response.status)) {
2023-10-23 10:34:28 +00:00
// Handle errors, such as token expiration
2023-10-03 12:55:53 +00:00
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 }