async function request(url, options = {}) { const response = await fetch(url, { ...options, headers: { 'content-type': 'application/json', ...(options.headers || {}), }, }); const data = await response.json().catch(() => ({})); if (!response.ok || (data && data.success === false)) { throw new Error(data?.error || `Запрос ${url} завершился ошибкой ${response.status}`); } return data; } export const api = { state: () => request('/api/state'), config: () => request('/api/config'), rules: { get: () => request('/api/rules'), save: (rules) => request('/api/rules', { method: 'PUT', body: JSON.stringify({ rules }) }), }, subscription: { fetch: (url) => request('/api/subscription/fetch', { method: 'POST', body: JSON.stringify({ url }) }), forget: () => request('/api/subscription', { method: 'DELETE' }), }, apply: (selectedTag) => request('/api/apply', { method: 'POST', body: JSON.stringify({ selectedTag }) }), singbox: { stop: () => request('/api/singbox/stop', { method: 'POST' }), restart: () => request('/api/singbox/restart', { method: 'POST' }), clear: () => request('/api/singbox/clear', { method: 'POST' }), }, };