前端: 添加API工具函数
This commit is contained in:
parent
b375911a81
commit
99284ab320
41
frontend/src/renderer/utils/api.js
Normal file
41
frontend/src/renderer/utils/api.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
const API_BASE = process.env.REACT_APP_API_URL || '';
|
||||||
|
|
||||||
|
const getHeaders = () => {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
return { 'Authorization': token ? `Bearer ${token}` : '' };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const api = {
|
||||||
|
get: async (url) => {
|
||||||
|
const res = await fetch(`${API_BASE}${url}`, { headers: getHeaders() });
|
||||||
|
return res.json();
|
||||||
|
},
|
||||||
|
post: async (url, data) => {
|
||||||
|
const res = await fetch(`${API_BASE}${url}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json', ...getHeaders() },
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
});
|
||||||
|
return res.json();
|
||||||
|
},
|
||||||
|
put: async (url, data) => {
|
||||||
|
const res = await fetch(`${API_BASE}${url}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json', ...getHeaders() },
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
});
|
||||||
|
return res.json();
|
||||||
|
},
|
||||||
|
delete: async (url) => {
|
||||||
|
const res = await fetch(`${API_BASE}${url}`, { method: 'DELETE', headers: getHeaders() });
|
||||||
|
return res.json();
|
||||||
|
},
|
||||||
|
upload: async (url, file) => {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', file);
|
||||||
|
const res = await fetch(`${API_BASE}${url}`, { method: 'POST', headers: getHeaders(), body: formData });
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default api;
|
||||||
Loading…
Reference in New Issue
Block a user