HttpClient class, which provides consistent error handling, authentication, and request/response management.
Service Architecture
All services follow a factory pattern and are created using theHttpClient instance:
Available Services
Auth Service
Authentication and login functionality
Shipments Service
Shipment management and tracking operations
Payment Service
Mobile money verification and payment operations
Users Service
User wallet, transactions, and payout operations
HTTP Client Configuration
TheHttpClient class is the foundation for all API communication. It handles authentication, error handling, and request/response transformation.
Initialization
Configuration Options
The base URL for all API requests. Set via
EXPO_PUBLIC_API_BASE_URL environment variable.Request timeout in milliseconds.
Default headers to include in all requests.
Callback function invoked when token refresh fails (typically triggers logout).
Request Interceptors
The HTTP client automatically handles authentication by adding Bearer tokens to all requests:Response Interceptors
All responses are transformed into a consistent format:Error Handling
The HTTP client provides comprehensive error handling with localized French error messages:Error Types
Localized error message in French
HTTP status code (if available)
Error response data from the server
Always
true for API errorsError code (e.g., “NETWORK_ERROR”, “ECONNABORTED”)
HTTP Status Error Handling
| Status Code | Error Message |
|---|---|
| 401 | ”Non autorisé: Veuillez vous reconnecter” |
| 404 | ”Ressource non trouvée” |
| 422 | ”Erreur de validation” |
| 429 | ”Trop de requêtes. Réessayez dans X secondes.” |
| 500-504 | ”Erreur serveur: Veuillez réessayer plus tard” |
Network Error Handling
- NETWORK_ERROR: “Erreur de connexion: Vérifiez votre connexion internet”
- ECONNABORTED: “Délai d’attente dépassé: Veuillez réessayer”
- Generic network errors: “Erreur réseau: Veuillez réessayer plus tard”
Token Refresh
The HTTP client automatically attempts to refresh expired tokens on 401 errors:- Detects 401 Unauthorized response
- Attempts token refresh using stored refresh token
- Retries original request with new token
- On failure, invokes
onTokenRefreshFailedcallback
Token refresh functionality is currently a TODO and returns mock tokens. The implementation needs to be completed with actual API integration.
HTTP Methods
TheHttpClient provides standard HTTP methods:
GET Request
POST Request
PUT Request
PATCH Request
DELETE Request
API Endpoints
All API endpoints are centrally defined insrc/services/api/end-points.ts:
Common Types
ApiResponse
ApiResponse wrapping the expected response type.
Query Parameters
Pagination Metadata
Environment Configuration
The API base URL is configured via environment variable:Best Practices
- Always handle errors: All service calls should be wrapped in try-catch blocks
- Use TypeScript types: Leverage the provided type definitions for type safety
- Centralize endpoint definitions: Never hardcode URLs in components
- Handle loading states: Show appropriate UI feedback during API calls
- Implement retry logic: For critical operations, consider retry mechanisms
Next Steps
Auth Service
Learn about authentication methods
Shipments Service
Explore shipment operations