Spaces:
Sleeping
Sleeping
Download dashboard/src/services/api.ts from jinksQspider/openWa: direct link, hf CLI and curl.
- Browser
- Download file 17.4 kB
-
https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/dashboard/src/services/api.ts
- Command line
-
hf download hf://spaces/jinksQspider/openWa/dashboard/src/services/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/dashboard/src/services/api.ts
17.4 kB
| // API Service Layer for OpenWA Dashboard | |
| // Centralized API client with TypeScript types | |
| // Resolve the API base URL. By default this is the same-origin relative path '/api', | |
| // correct when the dashboard and API are served from the same origin (the default | |
| // single-container setup). For a split-origin deployment (dashboard hosted separately | |
| // from the API), set VITE_API_URL at build time to the API ORIGIN — e.g. | |
| // `VITE_API_URL=https://gateway.example.com` — and the '/api' prefix is appended here. | |
| // Previously VITE_API_URL was documented but never read, so the dashboard always called | |
| // same-origin '/api' and a split deployment failed with "Invalid API Key" (#91). | |
| const API_BASE_URL = '/api'; | |
| // ============================================================================= | |
| // Types | |
| // ============================================================================= | |
| export interface Session { | |
| id: string; | |
| name: string; | |
| status: 'created' | 'idle' | 'initializing' | 'connecting' | 'qr_ready' | 'ready' | 'disconnected' | 'failed'; | |
| phone?: string; | |
| pushName?: string; | |
| lastActive?: string; | |
| createdAt: string; | |
| updatedAt: string; | |
| /** Human-readable reason for the most recent terminal engine failure (set only when status is 'failed'). */ | |
| lastError?: string | null; | |
| } | |
| export interface SessionStats { | |
| total: number; | |
| active: number; | |
| ready: number; | |
| disconnected: number; | |
| byStatus: Record<string, number>; | |
| memoryUsage: { heapUsed: number; heapTotal: number; rss: number }; | |
| } | |
| export interface Webhook { | |
| id: string; | |
| sessionId: string; | |
| url: string; | |
| events: string[]; | |
| active: boolean; | |
| secret?: string; | |
| createdAt: string; | |
| updatedAt: string; | |
| } | |
| export interface ApiKey { | |
| id: string; | |
| name: string; | |
| keyPrefix: string; | |
| role: 'admin' | 'operator' | 'viewer'; | |
| allowedIps?: string[]; | |
| allowedSessions?: string[]; | |
| isActive: boolean; | |
| expiresAt?: string; | |
| lastUsedAt?: string; | |
| usageCount: number; | |
| createdAt: string; | |
| apiKey?: string; // Only returned on creation | |
| } | |
| export interface AuditLog { | |
| id: string; | |
| action: string; | |
| severity: 'info' | 'warn' | 'error'; | |
| apiKeyId?: string; | |
| apiKeyName?: string; | |
| sessionId?: string; | |
| sessionName?: string; | |
| ipAddress?: string; | |
| method?: string; | |
| path?: string; | |
| statusCode?: number; | |
| errorMessage?: string; | |
| createdAt: string; | |
| } | |
| export interface MessageResponse { | |
| messageId: string; | |
| timestamp: number; | |
| } | |
| // Chat summary returned by GET /sessions/:id/chats (mirrors the backend ChatSummary). | |
| export interface Chat { | |
| id: string; | |
| name: string; | |
| isGroup: boolean; | |
| unreadCount: number; | |
| timestamp: number; | |
| lastMessage?: string; | |
| } | |
| export interface Contact { | |
| id: string; | |
| name?: string; | |
| pushName?: string; | |
| number: string; | |
| isMyContact: boolean; | |
| } | |
| export interface ChatMessage { | |
| id: string; | |
| waMessageId?: string; | |
| chatId: string; | |
| from: string; | |
| to: string; | |
| body: string; | |
| type: string; | |
| direction: 'incoming' | 'outgoing'; | |
| status: 'pending' | 'sent' | 'delivered' | 'read' | 'failed'; | |
| timestamp?: number; | |
| createdAt: string; | |
| metadata?: { | |
| media?: { mimetype: string; filename?: string; data?: string }; | |
| quotedMessage?: { id: string; body: string }; | |
| reactions?: Record<string, string>; | |
| }; | |
| } | |
| export interface SendMediaPayload { | |
| base64?: string; | |
| url?: string; | |
| mimetype?: string; | |
| filename?: string; | |
| caption?: string; | |
| } | |
| export interface HealthStatus { | |
| status: 'ok' | 'error'; | |
| timestamp?: string; | |
| details?: { | |
| database?: { status: string }; | |
| redis?: { status: string }; | |
| queue?: { status: string }; | |
| }; | |
| } | |
| export interface InfraStatus { | |
| database: { connected: boolean; type: string; host: string }; | |
| redis: { connected: boolean; host: string; port: number }; | |
| queue: { | |
| enabled: boolean; | |
| messages: { pending: number; completed: number; failed: number }; | |
| webhooks: { pending: number; completed: number; failed: number }; | |
| }; | |
| storage: { type: 'local' | 's3'; path?: string; bucket?: string }; | |
| engine: { type: string; headless: boolean }; | |
| } | |
| // Saved infrastructure config (from data/.env.generated) used to hydrate the form. | |
| // Secrets are never returned — `*Set` flags indicate whether a value is stored. | |
| export interface SavedConfig { | |
| database: { | |
| type: 'sqlite' | 'postgres'; | |
| builtIn: boolean; | |
| host: string; | |
| port: string; | |
| username: string; | |
| database: string; | |
| poolSize: number; | |
| sslEnabled: boolean; | |
| sslRejectUnauthorized: boolean; | |
| passwordSet: boolean; | |
| }; | |
| redis: { enabled: boolean; builtIn: boolean; host: string; port: string; passwordSet: boolean }; | |
| queue: { enabled: boolean }; | |
| storage: { | |
| type: 'local' | 's3'; | |
| builtIn: boolean; | |
| localPath: string; | |
| s3Bucket: string; | |
| s3Region: string; | |
| s3Endpoint: string; | |
| s3CredentialsSet: boolean; | |
| }; | |
| engine: { headless: boolean; sessionDataPath: string; browserArgs: string }; | |
| } | |
| export interface SaveConfigPayload { | |
| database?: { | |
| type: 'sqlite' | 'postgres'; | |
| builtIn?: boolean; | |
| host?: string; | |
| port?: string; | |
| username?: string; | |
| password?: string; | |
| database?: string; | |
| poolSize?: number; | |
| sslEnabled?: boolean; | |
| sslRejectUnauthorized?: boolean; | |
| }; | |
| redis?: { | |
| enabled?: boolean; | |
| builtIn?: boolean; | |
| host?: string; | |
| port?: string; | |
| password?: string; | |
| }; | |
| queue?: { | |
| enabled?: boolean; | |
| }; | |
| storage?: { | |
| type: 'local' | 's3'; | |
| builtIn?: boolean; | |
| localPath?: string; | |
| s3Bucket?: string; | |
| s3Region?: string; | |
| s3AccessKey?: string; | |
| s3SecretKey?: string; | |
| s3Endpoint?: string; | |
| }; | |
| engine?: { | |
| headless?: boolean; | |
| sessionDataPath?: string; | |
| browserArgs?: string; | |
| }; | |
| } | |
| export interface Settings { | |
| general: { apiBaseUrl: string; sessionTimeout: number; autoReconnect: boolean; debugMode: boolean }; | |
| api: { rateLimit: number; rateLimitWindow: number; enableDocs: boolean }; | |
| notifications: { emailEnabled: boolean; notificationEmail: string; webhookAlerts: boolean }; | |
| } | |
| // ============================================================================= | |
| // API Client | |
| // ============================================================================= | |
| async function request<T>(endpoint: string, options: RequestInit = {}): Promise<T> { | |
| const url = `${API_BASE_URL}${endpoint}`; | |
| // Get API key from sessionStorage for authentication | |
| const apiKey = sessionStorage.getItem('openwa_api_key'); | |
| const headers: HeadersInit = { | |
| 'Content-Type': 'application/json', | |
| ...(apiKey ? { 'X-API-Key': apiKey } : {}), | |
| ...options.headers, | |
| }; | |
| const response = await fetch(url, { ...options, headers }); | |
| if (!response.ok) { | |
| const error = await response.json().catch(() => ({ message: response.statusText })); | |
| throw new Error(error.message || `HTTP ${response.status}`); | |
| } | |
| if (response.status === 204) { | |
| return undefined as T; | |
| } | |
| return response.json(); | |
| } | |
| // ============================================================================= | |
| // Session API | |
| // ============================================================================= | |
| export const sessionApi = { | |
| list: () => request<Session[]>('/sessions'), | |
| get: (id: string) => request<Session>(`/sessions/${id}`), | |
| create: (name: string) => | |
| request<Session>('/sessions', { | |
| method: 'POST', | |
| body: JSON.stringify({ name }), | |
| }), | |
| delete: (id: string) => request<void>(`/sessions/${id}`, { method: 'DELETE' }), | |
| start: (id: string) => request<Session>(`/sessions/${id}/start`, { method: 'POST' }), | |
| stop: (id: string) => request<Session>(`/sessions/${id}/stop`, { method: 'POST' }), | |
| getQR: (id: string) => request<{ qrCode: string; status: string }>(`/sessions/${id}/qr`), | |
| getStats: () => request<SessionStats>('/sessions/stats/overview'), | |
| getGroups: (id: string) => | |
| request<{ id: string; name: string; linkedParentJID?: string | null }[]>(`/sessions/${id}/groups`), | |
| getChats: (id: string) => request<Chat[]>(`/sessions/${id}/chats`), | |
| getContacts: (id: string) => | |
| request<{ id: string; name?: string; pushName?: string; number: string; isMyContact: boolean }[]>( | |
| `/sessions/${id}/contacts`, | |
| ), | |
| markChatRead: (id: string, chatId: string) => | |
| request<{ success: boolean }>(`/sessions/${id}/chats/read`, { | |
| method: 'POST', | |
| body: JSON.stringify({ chatId }), | |
| }), | |
| getChatMessages: (id: string, chatId: string, limit = 100) => | |
| request<{ messages: ChatMessage[]; total: number }>( | |
| `/sessions/${id}/messages?chatId=${encodeURIComponent(chatId)}&limit=${limit}`, | |
| ), | |
| }; | |
| // ============================================================================= | |
| // Webhook API | |
| // ============================================================================= | |
| export const webhookApi = { | |
| listBySession: (sessionId: string) => request<Webhook[]>(`/sessions/${sessionId}/webhooks`), | |
| listAll: () => request<Webhook[]>('/webhooks'), | |
| get: (sessionId: string, id: string) => request<Webhook>(`/sessions/${sessionId}/webhooks/${id}`), | |
| create: (sessionId: string, data: { url: string; events: string[] }) => | |
| request<Webhook>(`/sessions/${sessionId}/webhooks`, { | |
| method: 'POST', | |
| body: JSON.stringify(data), | |
| }), | |
| update: (sessionId: string, id: string, data: Partial<Webhook>) => | |
| request<Webhook>(`/sessions/${sessionId}/webhooks/${id}`, { | |
| method: 'PUT', | |
| body: JSON.stringify(data), | |
| }), | |
| delete: (sessionId: string, id: string) => | |
| request<void>(`/sessions/${sessionId}/webhooks/${id}`, { method: 'DELETE' }), | |
| test: (sessionId: string, id: string) => | |
| request<{ success: boolean; statusCode?: number; error?: string }>(`/sessions/${sessionId}/webhooks/${id}/test`, { | |
| method: 'POST', | |
| }), | |
| }; | |
| // ============================================================================= | |
| // API Key API | |
| // ============================================================================= | |
| export const apiKeyApi = { | |
| list: () => request<ApiKey[]>('/auth/api-keys'), | |
| get: (id: string) => request<ApiKey>(`/auth/api-keys/${id}`), | |
| create: (data: { | |
| name: string; | |
| role: string; | |
| allowedIps?: string[]; | |
| allowedSessions?: string[]; | |
| expiresAt?: string; | |
| }) => | |
| request<ApiKey>('/auth/api-keys', { | |
| method: 'POST', | |
| body: JSON.stringify(data), | |
| }), | |
| update: (id: string, data: Partial<ApiKey>) => | |
| request<ApiKey>(`/auth/api-keys/${id}`, { | |
| method: 'PUT', | |
| body: JSON.stringify(data), | |
| }), | |
| delete: (id: string) => request<void>(`/auth/api-keys/${id}`, { method: 'DELETE' }), | |
| revoke: (id: string) => request<ApiKey>(`/auth/api-keys/${id}/revoke`, { method: 'POST' }), | |
| }; | |
| // ============================================================================= | |
| // Audit/Logs API | |
| // ============================================================================= | |
| export const auditApi = { | |
| list: (params?: { action?: string; severity?: string; limit?: number; offset?: number }) => { | |
| const query = new URLSearchParams(); | |
| if (params?.action) query.set('action', params.action); | |
| if (params?.severity) query.set('severity', params.severity); | |
| if (params?.limit) query.set('limit', String(params.limit)); | |
| if (params?.offset) query.set('offset', String(params.offset)); | |
| const queryStr = query.toString(); | |
| return request<{ data: AuditLog[]; total: number }>(`/audit${queryStr ? `?${queryStr}` : ''}`); | |
| }, | |
| }; | |
| // ============================================================================= | |
| // Message API | |
| // ============================================================================= | |
| export const messageApi = { | |
| getChatHistory: (sessionId: string, chatId: string, limit = 50) => | |
| request<ChatMessage[]>(`/sessions/${sessionId}/messages/${encodeURIComponent(chatId)}/history?limit=${limit}`), | |
| sendText: (sessionId: string, chatId: string, text: string) => | |
| request<MessageResponse>(`/sessions/${sessionId}/messages/send-text`, { | |
| method: 'POST', | |
| body: JSON.stringify({ chatId, text }), | |
| }), | |
| sendImage: (sessionId: string, chatId: string, url: string, caption?: string) => | |
| request<MessageResponse>(`/sessions/${sessionId}/messages/send-image`, { | |
| method: 'POST', | |
| body: JSON.stringify({ chatId, url, caption }), | |
| }), | |
| sendVideo: (sessionId: string, chatId: string, url: string, caption?: string) => | |
| request<MessageResponse>(`/sessions/${sessionId}/messages/send-video`, { | |
| method: 'POST', | |
| body: JSON.stringify({ chatId, url, caption }), | |
| }), | |
| sendAudio: (sessionId: string, chatId: string, url: string) => | |
| request<MessageResponse>(`/sessions/${sessionId}/messages/send-audio`, { | |
| method: 'POST', | |
| body: JSON.stringify({ chatId, url }), | |
| }), | |
| sendDocument: (sessionId: string, chatId: string, url: string, filename?: string) => | |
| request<MessageResponse>(`/sessions/${sessionId}/messages/send-document`, { | |
| method: 'POST', | |
| body: JSON.stringify({ chatId, url, filename }), | |
| }), | |
| sendMedia: ( | |
| sessionId: string, | |
| chatId: string, | |
| mediaType: 'image' | 'video' | 'audio' | 'document', | |
| payload: SendMediaPayload, | |
| ) => | |
| request<MessageResponse>(`/sessions/${sessionId}/messages/send-${mediaType}`, { | |
| method: 'POST', | |
| body: JSON.stringify({ chatId, ...payload }), | |
| }), | |
| reply: (sessionId: string, data: { chatId: string; quotedMessageId: string; text: string }) => | |
| request<MessageResponse>(`/sessions/${sessionId}/messages/reply`, { | |
| method: 'POST', | |
| body: JSON.stringify(data), | |
| }), | |
| react: (sessionId: string, data: { chatId: string; messageId: string; emoji: string }) => | |
| request<void>(`/sessions/${sessionId}/messages/react`, { | |
| method: 'POST', | |
| body: JSON.stringify(data), | |
| }), | |
| delete: (sessionId: string, data: { chatId: string; messageId: string; forEveryone?: boolean }) => | |
| request<void>(`/sessions/${sessionId}/messages/delete`, { | |
| method: 'POST', | |
| body: JSON.stringify(data), | |
| }), | |
| }; | |
| // ============================================================================= | |
| // Health & Infrastructure API | |
| // ============================================================================= | |
| export const healthApi = { | |
| check: () => request<HealthStatus>('/health'), | |
| ready: () => request<HealthStatus>('/health/ready'), | |
| }; | |
| export const infraApi = { | |
| getStatus: () => request<InfraStatus>('/infra/status'), | |
| getConfig: () => request<SavedConfig>('/infra/config'), | |
| updateConfig: (config: Partial<InfraStatus>) => | |
| request<InfraStatus>('/infra/config', { | |
| method: 'PUT', | |
| body: JSON.stringify(config), | |
| }), | |
| saveConfig: (config: SaveConfigPayload) => | |
| request<{ message: string; saved: boolean; envPath: string; profiles: string[] }>('/infra/config', { | |
| method: 'PUT', | |
| body: JSON.stringify(config), | |
| }), | |
| restart: (profiles?: string[], profilesToRemove?: string[]) => | |
| request<{ | |
| message: string; | |
| restarting: boolean; | |
| profiles: string[]; | |
| profilesToRemove: string[]; | |
| estimatedTime: number; | |
| }>('/infra/restart', { | |
| method: 'POST', | |
| body: JSON.stringify({ profiles: profiles || [], profilesToRemove: profilesToRemove || [] }), | |
| }), | |
| healthCheck: () => request<{ status: string; timestamp: string }>('/infra/health'), | |
| }; | |
| // ============================================================================= | |
| // Settings API | |
| // ============================================================================= | |
| export const settingsApi = { | |
| get: () => request<Settings>('/settings'), | |
| update: (settings: Partial<Settings>) => | |
| request<Settings>('/settings', { | |
| method: 'PUT', | |
| body: JSON.stringify(settings), | |
| }), | |
| }; | |
| // ============================================================================= | |
| // Plugin Types | |
| // ============================================================================= | |
| export interface Plugin { | |
| id: string; | |
| name: string; | |
| version: string; | |
| type: 'engine' | 'storage' | 'queue' | 'auth' | 'extension'; | |
| description?: string; | |
| author?: string; | |
| status: 'installed' | 'enabled' | 'disabled' | 'error'; | |
| config: Record<string, unknown>; | |
| builtIn: boolean; | |
| provides: string[]; | |
| loadedAt?: string; | |
| enabledAt?: string; | |
| error?: string; | |
| } | |
| export interface Engine { | |
| id: string; | |
| name: string; | |
| enabled: boolean; | |
| features: string[]; | |
| } | |
| // ============================================================================= | |
| // Plugins API | |
| // ============================================================================= | |
| export const pluginsApi = { | |
| list: () => request<Plugin[]>('/plugins'), | |
| get: (id: string) => request<Plugin>(`/plugins/${id}`), | |
| enable: (id: string) => | |
| request<{ success: boolean; message: string }>(`/plugins/${id}/enable`, { | |
| method: 'POST', | |
| }), | |
| disable: (id: string) => | |
| request<{ success: boolean; message: string }>(`/plugins/${id}/disable`, { | |
| method: 'POST', | |
| }), | |
| updateConfig: (id: string, config: Record<string, unknown>) => | |
| request<{ success: boolean; message: string }>(`/plugins/${id}/config`, { | |
| method: 'PUT', | |
| body: JSON.stringify({ config }), | |
| }), | |
| healthCheck: (id: string) => request<{ healthy: boolean; message?: string }>(`/plugins/${id}/health`), | |
| getEngines: () => request<Engine[]>('/infra/engines'), | |
| getCurrentEngine: () => request<{ engineType: string }>('/infra/engines/current'), | |
| }; | |