Overview
TheBackendAPIService is a fully-featured HTTP client for interacting with the Laravel (GIMA) backend API. It handles authentication via Bearer tokens, automatic pagination unwrapping, retry logic with exponential backoff, and timeout management optimized for Vercel Edge Functions.
Key Features
- Bearer Authentication: Laravel Sanctum token-based auth
- Auto Pagination: Unwraps Laravel
LengthAwarePaginatorresponses - Retry Logic: Exponential backoff with 2 retries by default
- Timeout Management: 8s default timeout (Vercel Edge compatible)
- Type Safety: Full TypeScript support with Zod validation
- Error Handling: Custom error types for different failure scenarios
- Query Building: Automatic filter parameter serialization
Class Definition
Constructor
Parameters
API configuration object
Dependency injection for testing
Example
Factory Function
The recommended way to create an instance is using the factory function:Methods
Catalog Methods
getActivos()
Fetch assets (activos) from the catalog with optional filters.Maintenance Methods
getMantenimientos()
Fetch maintenance records with filters.getCalendario()
Fetch maintenance calendar/schedule.getReportes()
Fetch maintenance reports.Inventory Methods
getRepuestos()
Fetch spare parts inventory.getProveedores()
Fetch supplier list.Response Types
PaginatedResult
All methods return paginated results:Error Handling
BackendAPIError
Base error class for all API errors.BackendTimeoutError
Thrown when request exceeds timeout.BackendAuthError
Thrown on 401 authentication failures.Example Error Handling
Retry Logic
The service automatically retries failed requests with exponential backoff:Non-Retryable Errors
- 401 (Auth): No retry
- 4xx (except 408, 429): No retry
- Auth errors: No retry
Retryable Errors
- Timeout errors: Retries up to
maxRetries - 5xx errors: Retries with backoff
- Network errors: Retries with backoff
- 408, 429: Retries with backoff
Usage in AI Tools
The service is designed for use in AI chat tools:Testing
Mock the fetch function for testing:Related
Chat Service
AI chat service using this API client
Backend Schemas
Type definitions and Zod schemas
AI Tools
Tools that consume this API
Laravel API Docs
Complete backend API documentation