api.js
3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { getH3Base, getToken, setToken } from './utils';
export async function apiFetch(path, options = {}) {
const base = getH3Base();
const url = path.startsWith('http') ? path : `${base}${path}`;
const token = getToken();
const headers = { ...(options.headers || {}) };
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const res = await fetch(url, { ...options, headers });
if (res.status === 401) {
setToken('');
const newToken = window.prompt('H3 访问令牌 (Token) 已过期或无效,请输入新令牌:');
if (newToken) {
setToken(newToken);
headers['Authorization'] = `Bearer ${newToken}`;
return fetch(url, { ...options, headers });
}
}
return res;
}
export async function fetchJobs({ page = 1, limit = 20, q = '', status = '', kind = '', sort = 'desc', trash = false } = {}) {
const params = new URLSearchParams();
if (trash) {
params.set('trash', 'true');
params.set('limit', '50');
} else {
// 服务端分页参数是 offset 不是 page,传 page 会被忽略(永远返回第一页)
params.set('offset', String((page - 1) * limit));
params.set('limit', String(limit));
if (q) params.set('q', q);
if (status) params.set('status', status);
if (kind) params.set('kind', kind);
if (sort) params.set('sort', sort);
}
const res = await apiFetch(`/v1/videos?${params.toString()}`);
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function fetchJobDetail(id) {
const res = await apiFetch(`/v1/videos/${id}`);
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function createJob(body) {
const res = await apiFetch('/v1/videos', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function uploadAsset(file) {
const fd = new FormData();
fd.append('file', file);
const res = await apiFetch('/v1/assets', {
method: 'POST',
body: fd,
});
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function prioritizeJob(id) {
const res = await apiFetch(`/v1/videos/${id}/prioritize`, { method: 'POST' });
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function deleteJob(id) {
const res = await apiFetch(`/v1/videos/${id}`, { method: 'DELETE' });
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function restoreJob(id) {
const res = await apiFetch(`/v1/videos/${id}/restore`, { method: 'POST' });
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function cancelJob(id) {
const res = await apiFetch(`/v1/videos/${id}/cancel`, { method: 'POST' });
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function resetGpu() {
const res = await apiFetch('/v1/admin/reset-gpu', { method: 'POST' });
if (!res.ok) throw new Error(await res.text());
return res.json();
}
export async function fetchBackends() {
const res = await apiFetch('/v1/backends');
if (!res.ok) return null;
return res.json();
}
export async function fetchComfyHistory(id) {
const res = await apiFetch(`/v1/videos/${id}/comfy_history`);
if (!res.ok) throw new Error(await res.text());
return res.json();
}