fix: 统一异常处理
This commit is contained in:
1 parent
4daf8dfcaa
commit
0c5cfcaa4a
21 files changed
+2124
-4218
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { getAuthHeaders } from '../utils/auth';
|
||||
import { requestJson } from './client';
|
||||
|
||||
export interface ScheduleTask {
|
||||
id: number;
|
||||
@@ -70,9 +71,7 @@ export interface DifyExecutionSnapshot {
|
||||
}
|
||||
|
||||
async function getJson<T>(url: string): Promise<T> {
|
||||
const res = await fetch(url, { headers: getAuthHeaders() });
|
||||
if (!res.ok) throw new Error((await res.json().catch(() => ({})))?.error || 'Request failed');
|
||||
return (await res.json()) as T;
|
||||
return requestJson<T>(url, { headers: getAuthHeaders() });
|
||||
}
|
||||
|
||||
export const listAdminSchedules = () =>
|
||||
@@ -81,39 +80,37 @@ export const listAdminSchedules = () =>
|
||||
export const exportAdminScheduleUrl = (taskId: number) => `/api/admin/schedules/${taskId}/export`;
|
||||
|
||||
export const importAdminSchedules = async (payload: any) => {
|
||||
const res = await fetch('/api/admin/schedules/import', {
|
||||
return requestJson('/api/admin/schedules/import', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data.error || 'Import failed');
|
||||
}
|
||||
return res.json();
|
||||
};
|
||||
|
||||
export const getAdminScheduleTaskConfig = (taskId: number) =>
|
||||
getJson<{ data_source_type: string; config: Record<string, any> | null }>(`/api/admin/schedule-config/${taskId}`);
|
||||
|
||||
export const updateAdminScheduleTaskConfig = (taskId: number, payload: Record<string, any>) =>
|
||||
fetch(`/api/admin/schedule-config/${taskId}`, {
|
||||
requestJson(`/api/admin/schedule-config/${taskId}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(payload),
|
||||
}).then(r => {
|
||||
if (!r.ok) throw new Error('Update config failed');
|
||||
return r.json();
|
||||
});
|
||||
|
||||
export interface TestScheduleSqlResult {
|
||||
total: number;
|
||||
showing: number;
|
||||
limit: number;
|
||||
sql: string;
|
||||
columns: string[];
|
||||
rows: Record<string, any>[];
|
||||
}
|
||||
|
||||
export const testAdminScheduleSql = (taskId: number, payload: { fetch_where_conditions?: string; fetch_order_by?: string; limit?: number }) =>
|
||||
fetch(`/api/admin/schedule-config/${taskId}/test-sql`, {
|
||||
requestJson<TestScheduleSqlResult>(`/api/admin/schedule-config/${taskId}/test-sql`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(payload),
|
||||
}).then(r => {
|
||||
if (!r.ok) return r.json().then(e => { throw new Error(e.error || 'Test failed'); });
|
||||
return r.json();
|
||||
});
|
||||
|
||||
export interface TestScheduleRunResult {
|
||||
@@ -133,38 +130,35 @@ export interface TestScheduleRunResult {
|
||||
}
|
||||
|
||||
export const testAdminScheduleRun = (taskId: number, payload: { fetch_where_conditions?: string; fetch_order_by?: string; limit?: number }) =>
|
||||
fetch(`/api/admin/schedule-config/${taskId}/test-run`, {
|
||||
requestJson<TestScheduleRunResult>(`/api/admin/schedule-config/${taskId}/test-run`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(payload),
|
||||
}).then(r => {
|
||||
if (!r.ok) return r.json().then(e => { throw new Error(e.error || 'Test run failed'); });
|
||||
return r.json() as Promise<TestScheduleRunResult>;
|
||||
});
|
||||
|
||||
export const createAdminSchedule = (payload: Partial<ScheduleTask> & { config?: Record<string, any> }) =>
|
||||
fetch('/api/admin/schedules', {
|
||||
requestJson('/api/admin/schedules', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
export const updateAdminSchedule = (id: number, payload: Partial<ScheduleTask> & { config?: Record<string, any> }) =>
|
||||
fetch(`/api/admin/schedules/${id}`, {
|
||||
requestJson(`/api/admin/schedules/${id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
export const deleteAdminSchedule = (id: number) =>
|
||||
fetch(`/api/admin/schedules/${id}`, {
|
||||
requestJson(`/api/admin/schedules/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
export const runAdminSchedule = (id: number) =>
|
||||
fetch(`/api/admin/schedules/${id}/run`, { method: 'POST', headers: getAuthHeaders() });
|
||||
requestJson(`/api/admin/schedules/${id}/run`, { method: 'POST', headers: getAuthHeaders() });
|
||||
export const toggleAdminSchedule = (id: number, enabled: boolean) =>
|
||||
fetch(`/api/admin/schedules/${id}/${enabled ? 'enable' : 'disable'}`, { method: 'POST', headers: getAuthHeaders() });
|
||||
requestJson(`/api/admin/schedules/${id}/${enabled ? 'enable' : 'disable'}`, { method: 'POST', headers: getAuthHeaders() });
|
||||
export const listAdminScheduleRuns = (taskId: number, params?: { status?: string; started_after?: string; started_before?: string }) => {
|
||||
const sp = new URLSearchParams();
|
||||
if (params?.status) sp.set('status', params.status);
|
||||
|
||||
Reference in new issue
Block a user