Overview
The MKing Admin application uses a centralized service layer architecture built on Axios. This pattern provides consistent HTTP communication, authentication handling, and error management across all API interactions.Architecture Pattern
The service layer is organized into domain-specific modules:- service.tsx - Core service with shared interceptors and main business logic
- admin.service.tsx - Admin panel operations (products, clients, employees, roles)
- ecomme.service.tsx - E-commerce related services
- event.service.ts - Calendar and event management
All service files share the same Axios instance with global request/response interceptors for authentication and error handling.
Base Configuration
Services use environment variables for API endpoint configuration:Axios Interceptors
Request Interceptor
The request interceptor automatically attaches JWT tokens to all outgoing requests:- Retrieves JWT token from localStorage
- Adds Bearer token to Authorization header
- Applied globally to all requests
The token is stored in localStorage after successful login via the
LoginService function.CORS Headers (Admin Services)
Admin and e-commerce services include additional CORS headers:admin.service.tsx:11-14, ecomme.service.tsx:11-14
Response Interceptor
The response interceptor handles errors globally using theResponse utility:
- Intercepts failed responses
- Extracts HTTP status code
- Passes to
Response()utility for user notification - Rejects promise for component-level handling
service.tsx:35-44
Service Function Patterns
Basic GET Request
service.tsx:134, admin.service.tsx:37
GET with Parameters
event.service.ts:15-19, service.tsx:50-51
GET with Path Parameters
service.tsx:80-81, admin.service.tsx:48
POST Requests
service.tsx:46, service.tsx:65-66, event.service.ts:21-23
PUT Requests
event.service.ts:25-27, admin.service.tsx:108
DELETE Requests
service.tsx:98-99, event.service.ts:29-31
Multipart Form Data
For file uploads, services override the default Content-Type:admin.service.tsx:38-42, admin.service.tsx:118-120
Products, employees, and quotations support file uploads for images and documents.
PDF Downloads
admin.service.tsx:96-98
Abort Signal Support
Some services support request cancellation:service.tsx:53-54, service.tsx:107-108
Abort signals are useful for canceling long-running requests when a component unmounts.
Domain-Specific Services
Core Services (service.tsx)
- Authentication:
LoginService - Dashboards:
TotalesService,Cartera - Reports:
SaldoVencido,HistoricoAnual,reporteRecuperacion - Comments:
guardarComentarios,obtenerComentarios - Messaging:
senMessageWhats,getTemplates - Payments:
pagoProveedorList,getListPayApply
Admin Services (admin.service.tsx)
Product Management:getProducts,getProduct,saveProduct,updateProduct,delProductgetCategories,createCategory,updateCategory,deleteCategorygetColors,getSizes,setPrimaryImage,delImg
getClients,getClient,saveClient,updateClient,delClientgetClientDetails,saveClientDetail,updateClientDetail
getEmployees,saveEmployee,updateEmployee,deleteEmployeegetUsers,deleteUser
getRoles,createRole,updateRole,deleteRolegetPermissions,createPermission,deletePermission
getAdminQuotations,createAdminQuotation,downloadAdminQuotationPdf
getInventoryByProduct,saveInventory,updateInventory
admin.service.tsx:37-133
E-commerce Services (ecomme.service.tsx)
getProducts- Product cataloggetUnits,getWarehouses- Inventory metadata
ecomme.service.tsx:35-40
Event Services (event.service.ts)
Calendar event CRUD operations with TypeScript interface:getEvents, createEvent, updateEvent, deleteEvent
Source: event.service.ts:4-31
Error Handling
All services inherit global error handling through the response interceptor. TheResponse utility handles:
- Network errors (status 0) - Server unreachable
- 401 Unauthorized - Token expiration with auto-redirect
- 403 Forbidden - Permission errors
- 404 Not Found - Missing resources
- 422 Validation errors - Form validation feedback
- 500 Server errors - Internal server issues
Best Practices
- Always use typed parameters - Prefer
id: numberoverid: any - Use async/await - Modern services use async functions
- Include abort signals - For long-running requests that may be canceled
- Handle file uploads properly - Override Content-Type for multipart data
- Destructure at call site - Services return full Axios response objects
Request Cancellation
The core service includes cancel token setup (currently disabled):service.tsx:5-33
This cancellation logic is currently inactive. Consider implementing AbortController for modern request cancellation.