создание, правка, удаление, скрытие инструмента
This commit is contained in:
+16
-3
@@ -1,14 +1,27 @@
|
||||
// api.js
|
||||
export async function apiRequest(url, payload = {}, method = 'POST') {
|
||||
const res = await fetch(url, {
|
||||
method = method.toUpperCase();
|
||||
|
||||
let finalUrl = url;
|
||||
let options = {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
credentials: 'same-origin'
|
||||
});
|
||||
};
|
||||
|
||||
// --- Если GET → добавляем payload в URL ---
|
||||
if (method === 'GET') {
|
||||
const params = new URLSearchParams(payload);
|
||||
finalUrl = `${url}?${params.toString()}`;
|
||||
} else {
|
||||
// --- Для остальных методов → отправляем body ---
|
||||
options.body = JSON.stringify(payload);
|
||||
}
|
||||
|
||||
const res = await fetch(finalUrl, options);
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
|
||||
Reference in New Issue
Block a user