部分重型模块的原子化拆解
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// 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();
|
||||
}
|
||||
Reference in New Issue
Block a user