-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
190 lines (159 loc) · 6.66 KB
/
Copy pathapi.ts
File metadata and controls
190 lines (159 loc) · 6.66 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import axios, { AxiosError } from 'axios';
import type { ClipboardItem, ChatMsg, RoomSettings, PluginInfo } from './types';
// ---- Multi-backend: custom server URL support ----
const CUSTOM_SERVER_KEY = 'claytablet_custom_server';
export function getBaseUrl(): string {
return localStorage.getItem(CUSTOM_SERVER_KEY) || '';
}
export function setCustomServer(url: string) {
const clean = url.replace(/\/$/, ''); // remove trailing slash
localStorage.setItem(CUSTOM_SERVER_KEY, clean);
}
export function clearCustomServer() {
localStorage.removeItem(CUSTOM_SERVER_KEY);
}
export function isUsingCustomServer(): boolean {
const val = localStorage.getItem(CUSTOM_SERVER_KEY);
return !!val && val !== '';
}
// ---- Auth headers ----
const getAuthHeaders = (roomId: string) => {
const token = sessionStorage.getItem(`room_token_${roomId}`);
return token ? { 'X-Room-Password': token } : {};
};
// Interceptor fires events only for room endpoints (not /api/auth/*)
axios.interceptors.response.use(
(response) => response,
(error: AxiosError) => {
const url = error.config?.url || '';
if (url.includes('/api/claytablet/') || url.includes('/api/ws/')) {
if (error.response?.status === 401) {
window.dispatchEvent(new CustomEvent('password:required'));
} else if (error.response?.status === 403) {
window.dispatchEvent(new CustomEvent('auth:required'));
}
}
return Promise.reject(error);
}
);
// ---- API response types ----
interface ClipboardResponse {
texts: (Omit<ClipboardItem, 'type'>)[];
images: (Omit<ClipboardItem, 'type'>)[];
audios: (Omit<ClipboardItem, 'type'>)[];
files: (Omit<ClipboardItem, 'type'>)[];
chats: ChatMsg[];
settings: RoomSettings;
layout_order: string[];
}
// ---- API client ----
export async function fetchClipboard(roomId: string): Promise<{
items: ClipboardItem[];
chats: ChatMsg[];
settings: RoomSettings;
layoutOrder: string[];
}> {
const res = await axios.get<ClipboardResponse>(`${getBaseUrl()}/api/claytablet/${roomId}`, { headers: getAuthHeaders(roomId) });
const texts: ClipboardItem[] = res.data.texts.map(t => ({ ...t, type: 'text' as const }));
const images: ClipboardItem[] = res.data.images.map(i => ({ ...i, type: 'image' as const }));
const audios: ClipboardItem[] = res.data.audios.map(a => ({ ...a, type: 'audio' as const }));
const files: ClipboardItem[] = (res.data.files || []).map(f => ({ ...f, type: 'file' as const }));
const items = [...texts, ...images, ...audios, ...files].sort((a, b) => b.timestamp - a.timestamp);
return {
items,
chats: res.data.chats || [],
settings: res.data.settings || { ttl: '24h' },
layoutOrder: res.data.layout_order || [],
};
}
export async function addText(roomId: string, content: string) {
return axios.post(`${getBaseUrl()}/api/claytablet/${roomId}/text`, { content }, { headers: getAuthHeaders(roomId) });
}
export async function addImage(roomId: string, file: File) {
const formData = new FormData();
formData.append('file', file);
return axios.post(`${getBaseUrl()}/api/claytablet/${roomId}/image`, formData, { headers: getAuthHeaders(roomId) });
}
export async function addAudio(roomId: string, file: File) {
const formData = new FormData();
formData.append('file', file);
return axios.post(`${getBaseUrl()}/api/claytablet/${roomId}/audio`, formData, { headers: getAuthHeaders(roomId) });
}
export async function addFile(roomId: string, file: File) {
const formData = new FormData();
formData.append('file', file);
return axios.post(`${getBaseUrl()}/api/claytablet/${roomId}/file`, formData, { headers: getAuthHeaders(roomId) });
}
export async function addChat(roomId: string, author: string, text: string) {
return axios.post(`${getBaseUrl()}/api/claytablet/${roomId}/chat`, { author, text }, { headers: getAuthHeaders(roomId) });
}
export async function deleteItem(roomId: string, itemId: string) {
return axios.delete(`${getBaseUrl()}/api/claytablet/${roomId}/${itemId}`, { headers: getAuthHeaders(roomId) });
}
export async function clearAll(roomId: string) {
return axios.delete(`${getBaseUrl()}/api/claytablet/${roomId}/all`, { headers: getAuthHeaders(roomId) });
}
export async function getRoomSettings(roomId: string): Promise<RoomSettings> {
const res = await axios.get<RoomSettings>(`${getBaseUrl()}/api/claytablet/${roomId}/settings`, { headers: getAuthHeaders(roomId) });
return res.data;
}
export async function updateRoomSettings(roomId: string, settings: RoomSettings): Promise<RoomSettings> {
const res = await axios.post<RoomSettings>(`${getBaseUrl()}/api/claytablet/${roomId}/settings`, settings, { headers: getAuthHeaders(roomId) });
return res.data;
}
export async function updateOrder(roomId: string, order: string[]) {
return axios.post(`${getBaseUrl()}/api/claytablet/${roomId}/order`, { order }, { headers: getAuthHeaders(roomId) });
}
export async function verifyPassword(roomId: string, password: string): Promise<boolean> {
try {
await axios.post(`${getBaseUrl()}/api/claytablet/${roomId}/verify-password`, { password });
sessionStorage.setItem(`room_token_${roomId}`, password);
return true;
} catch (error) {
if (axios.isAxiosError(error)) {
if (error.response?.status === 401) return false;
// 403 = personal room, re-throw so Board can handle it
}
throw error;
}
}
// ---- Auth ----
export async function getMe() {
const res = await axios.get(`${getBaseUrl()}/api/auth/me`);
return res.data;
}
export async function getMyRooms() {
const res = await axios.get(`${getBaseUrl()}/api/auth/me/rooms`);
return res.data;
}
export async function getPublicRooms(all = false) {
const res = await axios.get(`${getBaseUrl()}/api/claytablet/rooms/public`, { params: all ? { all: true } : {} });
return res.data;
}
export async function getSystemRooms() {
const res = await axios.get(`${getBaseUrl()}/api/claytablet/rooms/system`);
return res.data;
}
export async function logout() {
await axios.post(`${getBaseUrl()}/api/auth/logout`);
}
export async function getCliToken(): Promise<string | null> {
try {
const res = await axios.get(`${getBaseUrl()}/api/auth/token`);
return res.data.token ?? null;
} catch {
return null;
}
}
// ---- Plugin API ----
export async function getPlugins(): Promise<PluginInfo[]> {
const res = await axios.get(`${getBaseUrl()}/api/plugins`);
return res.data;
}
export async function getPluginConfig(pluginId: string): Promise<unknown> {
const res = await axios.get(`${getBaseUrl()}/api/plugins/${pluginId}/config`);
return res.data;
}
export async function setPluginConfig(pluginId: string, config: unknown): Promise<void> {
await axios.post(`${getBaseUrl()}/api/plugins/${pluginId}/config`, config);
}