type ApiResponse = { code: number | string message: string data: T requestId?: string timestamp?: number } const BASE = typeof window !== 'undefined' ? window.location.protocol + '//' + window.location.host + '/api/hss' : '/api/hss' export function useApi() { function headers(extra?: Record): Record { const h: Record = { 'Content-Type': 'application/json', 'X-Tenant-Id': '1', 'X-Org-Id': '1' } if (typeof window !== 'undefined') { const stored = localStorage.getItem('hss_platform_user') if (stored) { try { const u = JSON.parse(stored) h['X-User-Id'] = u.userId h['X-User-Role'] = u.userRole h['X-Tenant-Id'] = u.tenantId h['X-Org-Id'] = u.orgId } catch {} } } return { ...h, ...extra } } async function get(path: string): Promise { const res = await $fetch>(BASE + path, { headers: headers() }) if (typeof res.code === 'string' ? (res.code !== '200' && res.code !== 'SUCCESS') : res.code !== 200) { throw new Error(res.message || 'API error') } return res.data } async function post(path: string, body?: any): Promise { const h = headers() if (body) h['Idempotency-Key'] = 'web-' + Date.now() + '-' + Math.random().toString(36).slice(2, 8) const res = await $fetch>(BASE + path, { method: 'POST', headers: h, body: body ? JSON.stringify(body) : undefined }) if (typeof res.code === 'string' ? (res.code !== '200' && res.code !== 'SUCCESS') : res.code !== 200) { throw new Error(res.message || 'API error') } return res.data } async function rawGet(path: string): Promise> { return await $fetch>(BASE + path, { headers: headers() }) } return { get, post, rawGet } }