создание, правка, удаление, скрытие инструмента
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();
|
||||
|
||||
@@ -23,16 +23,26 @@ export async function setCookie(name, value, days = 180) {
|
||||
}
|
||||
}
|
||||
|
||||
const secure = true; // TODO включить после тестов
|
||||
const sameSite = 'Lax';
|
||||
|
||||
const expires = new Date(Date.now() + days * 864e5).toUTCString();
|
||||
const encodedName = encodeURIComponent(name);
|
||||
|
||||
let cookie = `${encodeURIComponent(name)}=${cookieValue}; expires=${expires}; path=/`;
|
||||
if (secure) cookie += '; Secure';
|
||||
if (sameSite) cookie += `; SameSite=${sameSite}`;
|
||||
// ---------- 1. Пытаемся установить безопасную куку ----------
|
||||
let secureCookie = `${encodedName}=${cookieValue}; expires=${expires}; path=/; Secure; SameSite=Lax`;
|
||||
document.cookie = secureCookie;
|
||||
|
||||
document.cookie = cookie;
|
||||
// ---------- 2. Проверяем, записалась ли она ----------
|
||||
const isSet = document.cookie.split('; ')
|
||||
.some(c => c.startsWith(`${encodedName}=`));
|
||||
|
||||
if (isSet) {
|
||||
return true; // безопасная кука успешно установлена
|
||||
}
|
||||
|
||||
// ---------- 3. Фолбэк: ставим обычную (без Secure) ----------
|
||||
let normalCookie = `${encodedName}=${cookieValue}; expires=${expires}; path=/; SameSite=Lax`;
|
||||
document.cookie = normalCookie;
|
||||
|
||||
return false; // безопасную куку установить не удалось
|
||||
}
|
||||
|
||||
export async function getCookie(name) {
|
||||
|
||||
+1373
-95
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user