refactor(ui): step3 - extract api.js (authHeaders, getJSON, requestJSON)
This commit is contained in:
+1
-38
@@ -14,6 +14,7 @@
|
||||
</script>
|
||||
<link rel="stylesheet" href="css/styles.css">
|
||||
<script src="js/config.js"></script>
|
||||
<script src="js/api.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
@@ -410,44 +411,6 @@
|
||||
currentInvoke: null
|
||||
};
|
||||
|
||||
function authHeaders() {
|
||||
return {
|
||||
'X-Auth-Token': localStorage.getItem('auth_token') || '',
|
||||
'X-Auth-Env': localStorage.getItem('auth_env') || 'test'
|
||||
};
|
||||
}
|
||||
|
||||
async function getJSON(url) {
|
||||
const r = await fetch(url, { headers: authHeaders() });
|
||||
if (r.status === 401) { doLogout(); throw new Error('Сессия истекла'); }
|
||||
if (!r.ok) {
|
||||
let msg = '';
|
||||
try {
|
||||
const body = await r.json();
|
||||
msg = body.error || JSON.stringify(body);
|
||||
} catch (_) {
|
||||
msg = await r.text();
|
||||
}
|
||||
throw new Error(url + ': ' + r.status + (msg ? ' - ' + msg : ''));
|
||||
}
|
||||
return r.json();
|
||||
}
|
||||
|
||||
async function requestJSON(url, method, body) {
|
||||
const r = await fetch(url, {
|
||||
method: method,
|
||||
headers: Object.assign({ 'Content-Type': 'application/json' }, authHeaders()),
|
||||
body: body ? JSON.stringify(body) : undefined
|
||||
});
|
||||
if (r.status === 401) { doLogout(); throw new Error('Сессия истекла'); }
|
||||
let data = {};
|
||||
try { data = await r.json(); } catch (_) { }
|
||||
if (!r.ok) {
|
||||
throw new Error(data.error || (url + ': ' + r.status));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
function setText(id, value) {
|
||||
document.getElementById(id).textContent = String(value);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/* api.js — HTTP-утилиты */
|
||||
|
||||
function authHeaders() {
|
||||
return {
|
||||
'X-Auth-Token': localStorage.getItem('auth_token') || '',
|
||||
'X-Auth-Env': localStorage.getItem('auth_env') || 'test'
|
||||
};
|
||||
}
|
||||
|
||||
async function getJSON(url) {
|
||||
const r = await fetch(url, { headers: authHeaders() });
|
||||
if (r.status === 401) { doLogout(); throw new Error('Сессия истекла'); }
|
||||
if (!r.ok) {
|
||||
let msg = '';
|
||||
try {
|
||||
const body = await r.json();
|
||||
msg = body.error || JSON.stringify(body);
|
||||
} catch (_) {
|
||||
msg = await r.text();
|
||||
}
|
||||
throw new Error(url + ': ' + r.status + (msg ? ' - ' + msg : ''));
|
||||
}
|
||||
return r.json();
|
||||
}
|
||||
|
||||
async function requestJSON(url, method, body) {
|
||||
const r = await fetch(url, {
|
||||
method: method,
|
||||
headers: Object.assign({ 'Content-Type': 'application/json' }, authHeaders()),
|
||||
body: body ? JSON.stringify(body) : undefined
|
||||
});
|
||||
if (r.status === 401) { doLogout(); throw new Error('Сессия истекла'); }
|
||||
let data = {};
|
||||
try { data = await r.json(); } catch (_) { }
|
||||
if (!r.ok) {
|
||||
throw new Error(data.error || (url + ': ' + r.status));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user