Skip to main content
The DPM Delivery Mobile app uses a service-oriented architecture with HTTP-based API communication. All services are built on top of the HttpClient class, which provides consistent error handling, authentication, and request/response management.

Service Architecture

All services follow a factory pattern and are created using the HttpClient instance:
const authService = createAuthService(httpClient);
const shipmentsService = createShipmentService(httpClient);
const paymentService = createPaymentService(httpClient);
const usersService = createUserService(httpClient);

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

The HttpClient class is the foundation for all API communication. It handles authentication, error handling, and request/response transformation.

Initialization

import { createHttpClient } from '@/services/http.service';
import { ENV } from '@/utils/env';

const httpClient = createHttpClient({
  baseURL: ENV.apiBaseUrl,
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json',
  },
  onTokenRefreshFailed: () => {
    // Handle logout on token refresh failure
  },
});

Configuration Options

baseURL
string
required
The base URL for all API requests. Set via EXPO_PUBLIC_API_BASE_URL environment variable.
timeout
number
default:"30000"
Request timeout in milliseconds.
headers
Record<string, string>
Default headers to include in all requests.
onTokenRefreshFailed
() => void
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:
// Automatically added to each request
config.headers.Authorization = `Bearer ${authToken}`;
Source: src/services/http.service.ts:57-68

Response Interceptors

All responses are transformed into a consistent format:
interface AxiosApiResponse<T> {
  data: T;
  status: number;
  statusText: string;
}

Error Handling

The HTTP client provides comprehensive error handling with localized French error messages:

Error Types

message
string
Localized error message in French
status
number
HTTP status code (if available)
data
any
Error response data from the server
isApiError
boolean
Always true for API errors
code
string
Error code (e.g., “NETWORK_ERROR”, “ECONNABORTED”)

HTTP Status Error Handling

Status CodeError 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”
Source: src/services/http.service.ts:89-174

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:
  1. Detects 401 Unauthorized response
  2. Attempts token refresh using stored refresh token
  3. Retries original request with new token
  4. On failure, invokes onTokenRefreshFailed callback
Source: src/services/http.service.ts:176-206
Token refresh functionality is currently a TODO and returns mock tokens. The implementation needs to be completed with actual API integration.

HTTP Methods

The HttpClient provides standard HTTP methods:

GET Request

httpClient.get<T>(endpoint: string, params?: any, config?: AxiosRequestConfig)

POST Request

httpClient.post<T>(endpoint: string, data?: any, config?: AxiosRequestConfig)

PUT Request

httpClient.put<T>(endpoint: string, data?: any, config?: AxiosRequestConfig)

PATCH Request

httpClient.patch<T>(endpoint: string, data?: any, config?: AxiosRequestConfig)

DELETE Request

httpClient.delete<T>(endpoint: string, config?: AxiosRequestConfig)

API Endpoints

All API endpoints are centrally defined in src/services/api/end-points.ts:
export const apiEndPoints = {
  baseUrl: apiBaseUrl,
  auth: authEndPoints,
  riders: riderEndPoints,
  users: userEndPoints,
  payouts: payoutEndPoints,
  shipments: shipmentEndPoints,
  payment: paymentEndPoints,
};

Common Types

ApiResponse

type ApiResponse<T> = Promise<AxiosApiResponse<T>>;
All service methods return an ApiResponse wrapping the expected response type.

Query Parameters

type Query = Record<string, string | number | boolean | undefined>;
Used for filtering and pagination in list endpoints.

Pagination Metadata

interface PaginationMeta {
  totalItems: number;
  itemCount: number;
  itemsPerPage: number;
  totalPages: number;
  currentPage: number;
}

Environment Configuration

The API base URL is configured via environment variable:
EXPO_PUBLIC_API_BASE_URL=https://api.example.com
Source: src/utils/env.ts:8-12

Best Practices

  1. Always handle errors: All service calls should be wrapped in try-catch blocks
  2. Use TypeScript types: Leverage the provided type definitions for type safety
  3. Centralize endpoint definitions: Never hardcode URLs in components
  4. Handle loading states: Show appropriate UI feedback during API calls
  5. Implement retry logic: For critical operations, consider retry mechanisms

Next Steps

Auth Service

Learn about authentication methods

Shipments Service

Explore shipment operations

Build docs developers (and LLMs) love