/** * Dashboard API client functions. * All calls use credentials: 'include' for cookie-based auth. */ const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001'; export async function fetchLayout(): Promise> { const res = await fetch(`${API_URL}/dashboard/layout`, { credentials: 'include', }); if (!res.ok) throw new Error('Failed to fetch layout'); return res.json(); } export async function saveLayout( layouts: Record, ): Promise { const res = await fetch(`${API_URL}/dashboard/layout`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ layouts }), }); if (!res.ok) throw new Error('Failed to save layout'); } export async function fetchWidgets(): Promise< Array<{ id: string; widgetType: string; config: Record }> > { const res = await fetch(`${API_URL}/dashboard/widgets`, { credentials: 'include', }); if (!res.ok) throw new Error('Failed to fetch widgets'); return res.json(); } export async function addWidget( widgetType: string, ): Promise<{ id: string; widgetType: string; config: Record }> { const res = await fetch(`${API_URL}/dashboard/widgets`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ widgetType }), }); if (!res.ok) throw new Error('Failed to add widget'); return res.json(); } export async function removeWidget(id: string): Promise { const res = await fetch(`${API_URL}/dashboard/widgets/${id}`, { method: 'DELETE', credentials: 'include', }); if (!res.ok) throw new Error('Failed to remove widget'); } export async function updateWidgetConfig( id: string, config: Record, signal?: AbortSignal, ): Promise { const res = await fetch(`${API_URL}/dashboard/widgets/${id}/config`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ config }), signal, }); if (!res.ok) throw new Error('Failed to update widget config'); } // --- Search Provider API (05-02) --- export interface SearchProviderResponse { id: string; name: string; urlTemplate: string; isDefault: boolean; userId?: string | null; } export async function fetchSearchProviders(): Promise { const res = await fetch(`${API_URL}/dashboard/search-providers`, { credentials: 'include', }); if (!res.ok) throw new Error('Failed to fetch search providers'); return res.json(); } export async function addSearchProvider(payload: { name: string; urlTemplate: string; }): Promise { const res = await fetch(`${API_URL}/dashboard/search-providers`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(payload), }); if (!res.ok) throw new Error('Failed to add search provider'); return res.json(); } export async function removeSearchProvider(id: string): Promise { const res = await fetch(`${API_URL}/dashboard/search-providers/${id}`, { method: 'DELETE', credentials: 'include', }); if (!res.ok) throw new Error('Failed to remove search provider'); }