mirror of
https://github.com/gnmyt/LicenseAPI.git
synced 2026-01-10 10:11:47 +00:00
Create RequestUtil.js
This commit is contained in:
44
dashboard/src/common/utils/RequestUtil.js
Normal file
44
dashboard/src/common/utils/RequestUtil.js
Normal file
@ -0,0 +1,44 @@
|
||||
const API_VERSION = "v1";
|
||||
|
||||
export const request = async (url, method, body, headers) => {
|
||||
const response = await fetch(`/api/${API_VERSION}${url}`, {
|
||||
method: method,
|
||||
headers: {...headers, "Content-Type": "application/json"},
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
if (response.status === 401) throw new Error("Unauthorized");
|
||||
|
||||
const rawData = await response.text();
|
||||
const data = rawData ? JSON.parse(rawData) : rawData.toString();
|
||||
|
||||
if (data.code >= 300) throw data;
|
||||
|
||||
if (!response.ok) throw data;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
export const sessionRequest = (url, method, token, body) => {
|
||||
return request(url, method, body, {"Authorization": `Bearer ${token}`});
|
||||
}
|
||||
|
||||
export const getRequest = (url) => {
|
||||
return sessionRequest(url, "GET", localStorage.getItem("sessionToken"));
|
||||
}
|
||||
|
||||
export const postRequest = (url, body) => {
|
||||
return sessionRequest(url, "POST", localStorage.getItem("sessionToken"), body);
|
||||
}
|
||||
|
||||
export const putRequest = (url, body) => {
|
||||
return sessionRequest(url, "PUT", localStorage.getItem("sessionToken"), body);
|
||||
}
|
||||
|
||||
export const deleteRequest = (url) => {
|
||||
return sessionRequest(url, "DELETE", localStorage.getItem("sessionToken"));
|
||||
}
|
||||
|
||||
export const patchRequest = (url, body) => {
|
||||
return sessionRequest(url, "PATCH", localStorage.getItem("sessionToken"), body);
|
||||
}
|
||||
Reference in New Issue
Block a user