From 99284ab320a9df4ef0121c04a25029886e4f5862 Mon Sep 17 00:00:00 2001 From: Frontend Developer Date: Tue, 10 Mar 2026 09:17:53 +0000 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF:=20=E6=B7=BB=E5=8A=A0API?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/renderer/utils/api.js | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 frontend/src/renderer/utils/api.js diff --git a/frontend/src/renderer/utils/api.js b/frontend/src/renderer/utils/api.js new file mode 100644 index 0000000..858b10e --- /dev/null +++ b/frontend/src/renderer/utils/api.js @@ -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;