Primeiro commit
This commit is contained in:
50
src/api.js
Executable file
50
src/api.js
Executable file
@@ -0,0 +1,50 @@
|
||||
const API_BASE = ''; // Ex: http://192.168.4.1 ou leave empty for relative
|
||||
|
||||
let credentials = '';
|
||||
|
||||
export function setCredentials(user, pass) {
|
||||
credentials = btoa(`${user}:${pass}`);
|
||||
}
|
||||
|
||||
function getHeaders(isJson = true) {
|
||||
const headers = {
|
||||
Authorization: `Basic ${credentials}`,
|
||||
};
|
||||
if (isJson) headers['Content-Type'] = 'application/json';
|
||||
return headers;
|
||||
}
|
||||
|
||||
export async function get(path) {
|
||||
const res = await fetch(`${API_BASE}${path}`, {
|
||||
headers: getHeaders(false),
|
||||
});
|
||||
if (!res.ok) throw new Error(`GET ${path} failed`);
|
||||
return await res.json();
|
||||
}
|
||||
|
||||
export async function post(path, body) {
|
||||
const res = await fetch(`${API_BASE}${path}`, {
|
||||
method: 'POST',
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!res.ok) throw new Error(`POST ${path} failed`);
|
||||
return await res.text();
|
||||
}
|
||||
|
||||
export async function postPlain(path) {
|
||||
const res = await fetch(`${API_BASE}${path}`, {
|
||||
method: 'POST',
|
||||
headers: getHeaders(false),
|
||||
});
|
||||
if (!res.ok) throw new Error(`POST ${path} failed`);
|
||||
return await res.text();
|
||||
}
|
||||
|
||||
export async function fetchLogs(index = 0) {
|
||||
const res = await fetch(`${API_BASE}/api/v1/log?index=${index}`, {
|
||||
headers: getHeaders(false),
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to fetch logs');
|
||||
return await res.text();
|
||||
}
|
||||
Reference in New Issue
Block a user