Files
xiaoqu_mcp_service/xiaoqu-agent-service/front/src/api.js
T

27 lines
820 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
let csrfToken = ''
export function setCsrf(value) {
csrfToken = value || ''
}
export async function api(path, options = {}) {
const method = options.method || 'GET'
const response = await fetch(`/api/admin${path}`, {
credentials: 'same-origin',
...options,
headers: {
...(options.body ? { 'Content-Type': 'application/json' } : {}),
...(!['GET', 'HEAD'].includes(method) && csrfToken ? { 'X-CSRF-Token': csrfToken } : {}),
...options.headers,
},
})
if (!response.ok) {
let message = `请求失败(${response.status})`
try { message = (await response.json()).message || message } catch { /* empty response */ }
const error = new Error(message)
error.status = response.status
throw error
}
return response.status === 204 ? null : response.json()
}