Files
frpc-console/static/modules/api.js
T

26 lines
650 B
JavaScript

// modules/api.js
const API_BASE = '/api';
export function getToken() {
return localStorage.getItem('frpc_token') || '';
}
export function getHeaders() {
return {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + getToken(),
};
}
export async function apiFetch(endpoint, options = {}) {
const url = API_BASE + endpoint;
const headers = options.body instanceof FormData
? { 'Authorization': 'Bearer ' + getToken() }
: getHeaders();
const res = await fetch(url, {
...options,
headers: { ...headers, ...(options.headers || {}) },
});
return res.json();
}