API Services Overview
Thesrc/services/api/ directory contains modules that encapsulate the logic for interacting with the CICADA backend, which is primarily Supabase (database and Edge Functions). These services provide a clean, typed, and reusable interface for frontend components to fetch data, submit changes, and invoke backend operations.
Purpose
- Abstraction: Decouple frontend components from the direct implementation details of backend calls.
- Reusability: Provide common functions for operations like fetching documents, uploading files, or getting chat responses.
- Type Safety: Utilize TypeScript types (defined in
src/services/api/types.ts) for request parameters and response data, improving code reliability. - Centralization: Group related API interactions into dedicated service files (e.g.,
documentService.ts,chatService.ts).
Structure
index.ts: Re-exports all types and functions from individual service files for easy importing.types.ts: Defines shared TypeScript interfaces and types used across different API services (e.g.,Document,ChatResponse,FolderStructure).- Individual Service Files:
adminService.ts: For admin-specific operations like fetching dashboard stats.chatService.ts: For interacting with the AI chat backend (e.g.,cleoChatEdge Function).documentService.ts: A facade for document-related operations, delegating toprivateDocumentService.tsorpublicDocumentService.tsbased on context or type.folderService.ts: For managing folder structures.privateDocumentService.ts: Handles CRUD operations and URL generation specifically for private documents.publicDocumentService.ts: Handles CRUD operations specifically for public documents.
Interaction with Supabase
These services primarily use the Supabase JavaScript client (supabase-js) from src/lib/supabase.ts to:
- Query Database Tables: Perform
select,insert,update,deleteoperations on Supabase PostgreSQL tables (e.g.,documents_private,documents_public). - Invoke Edge Functions: Call Supabase Edge Functions (e.g.,
getAdminStats,cleoChat,parseUploadedDocumentsindirectly). - Manage Storage: Upload files to and retrieve files/URLs from Supabase Storage.
Example: documentService.ts
This service often acts as a high-level interface. For instance, fetchDocumentById might try to fetch from public documents first, then private if the user is authenticated and the document isn’t found publicly, or based on an explicit fileSystemType parameter.