Add Web 2 workspace and controlled deployment updates
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
export class ApiError extends Error {
|
||||
constructor(message, status, detail) {
|
||||
super(message);
|
||||
this.name = 'ApiError';
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
}
|
||||
}
|
||||
|
||||
function errorMessage(data, status) {
|
||||
if (typeof data?.detail === 'string') return data.detail;
|
||||
if (typeof data?.error === 'string') return data.error;
|
||||
if (typeof data?.message === 'string') return data.message;
|
||||
if (Array.isArray(data?.detail)) return data.detail.map(item => `${(item.loc || []).slice(1).join('.')}: ${item.msg}`).join(';');
|
||||
return `请求未完成(HTTP ${status})`;
|
||||
}
|
||||
|
||||
export async function request(path, {method = 'GET', body, signal, blob = false} = {}) {
|
||||
const headers = {Accept: blob ? '*/*' : 'application/json'};
|
||||
if (body !== undefined && !(body instanceof FormData)) headers['Content-Type'] = 'application/json';
|
||||
let response;
|
||||
try {
|
||||
response = await fetch(path, {method, credentials: 'same-origin', cache: 'no-store', headers, signal,
|
||||
body: body === undefined ? undefined : body instanceof FormData ? body : JSON.stringify(body)});
|
||||
} catch (error) {
|
||||
if (error.name === 'AbortError') throw error;
|
||||
throw new ApiError('无法连接 Web 服务,请检查服务是否仍在运行。', 0);
|
||||
}
|
||||
if (response.status === 401 && !path.startsWith('/api/auth/')) window.dispatchEvent(new CustomEvent('auth-required'));
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => null);
|
||||
throw new ApiError(errorMessage(data, response.status), response.status, data);
|
||||
}
|
||||
if (blob) return response.blob();
|
||||
if (response.status === 204) return null;
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export const accountURL = id => `/api/accounts/${encodeURIComponent(id)}`;
|
||||
export const api = {
|
||||
auth: () => request('/api/auth/status'),
|
||||
login: password => request('/api/auth/login', {method: 'POST', body: {password}}),
|
||||
logout: () => request('/api/auth/logout', {method: 'POST', body: {}}),
|
||||
meta: () => request('/api/meta'),
|
||||
schema: () => request('/api/schema'),
|
||||
accounts: () => request('/api/accounts'),
|
||||
create: (label, demo) => request('/api/accounts', {method: 'POST', body: {label, demo}}),
|
||||
remove: id => request(accountURL(id), {method: 'DELETE'}),
|
||||
status: id => request(`${accountURL(id)}/status`),
|
||||
config: id => request(`${accountURL(id)}/config`),
|
||||
save: (id, config, revision) => request(`${accountURL(id)}/config`, {method: 'PUT', body: {config, revision}}),
|
||||
view: (id, page, query = {}) => request(`${accountURL(id)}/views/${encodeURIComponent(page)}?query=${encodeURIComponent(JSON.stringify(query))}`),
|
||||
action: (id, operation, payload = {}) => request(`${accountURL(id)}/actions/${encodeURIComponent(operation)}`, {method: 'POST', body: payload}),
|
||||
job: (id, job) => request(`${accountURL(id)}/jobs/${encodeURIComponent(job)}`),
|
||||
logs: (id, after, debug) => request(`${accountURL(id)}/logs?after=${encodeURIComponent(after)}&debug=${debug}`),
|
||||
debugEntry: (id, entry) => request(`${accountURL(id)}/debug/${encodeURIComponent(entry)}`),
|
||||
exportConfig: id => request(`${accountURL(id)}/config/export`),
|
||||
importConfig: (id, config) => request(`${accountURL(id)}/config/import`, {method: 'POST', body: config}),
|
||||
health: options => request('/api/health', options),
|
||||
updates: options => request('/api/system/updates', options),
|
||||
checkUpdates: options => request('/api/system/updates/check', {...options, method: 'POST', body: {}}),
|
||||
applyUpdate: (version, options) => request('/api/system/updates/apply', {...options, method: 'POST', body: version ? {version} : {}}),
|
||||
};
|
||||
Reference in New Issue
Block a user