Overview
MKing Admin uses Axios as the HTTP client for all API communications. The application includes centralized API configuration with request/response interceptors for authentication, error handling, and CORS management.Base Configuration
All API services use theVITE_BASE_URL environment variable to construct endpoints:
Example Service Structure
src/services/admin.service.tsx
Request Interceptors
Request interceptors add authentication tokens and configure headers for all outgoing requests.Authentication Interceptor
src/services/admin.service.tsx
The authentication token is automatically retrieved from
localStorage and added to all requests as a Bearer token.Request Headers
All API requests include the following headers:- Authorization:
Bearer {token}- JWT authentication token - Content-Type:
application/json- Default content type (overridable) - Access-Control-Allow-Origin:
*- CORS configuration - Access-Control-Allow-Methods:
GET, POST, PUT, DELETE - Access-Control-Allow-Headers:
Content-Type, Authorization
Response Interceptors
Response interceptors handle API responses and errors globally.Response Handler
src/services/admin.service.tsx
Response utility function handles error status codes and displays appropriate messages to users.
Error Status Handling
Common HTTP status codes handled by the interceptor:- 401 Unauthorized: Invalid or expired token - redirect to login
- 403 Forbidden: Insufficient permissions
- 404 Not Found: Resource not found
- 500 Internal Server Error: Server-side error
Service Modules
The application is organized into multiple service modules:Admin Service
Manages products, catalogs, clients, employees, and quotations.src/services/admin.service.tsx
Event Service
Manages calendar events.src/services/event.service.ts
E-commerce Service
Handles e-commerce specific operations.src/services/ecomme.service.tsx
File Upload Configuration
For file uploads (products, employees), usemultipart/form-data content type:
Image Management
Authentication Flow
Login Service
src/services/service.tsx
Token Storage
After successful login:-
Store the JWT token in
localStorage: - The request interceptor automatically adds it to subsequent requests
-
On logout, clear the token:
PDF Downloads
For PDF downloads, useblob response type:
src/services/admin.service.tsx
Query Parameters
Pass query parameters using theparams option:
API Endpoint Reference
Products
GET /api/product- Get all productsGET /api/product/{id}- Get single productPOST /api/product- Create product (multipart/form-data)PUT /api/product/{id}- Update product (multipart/form-data)DELETE /api/product/{id}- Delete product
Catalogs
GET /api/product_category- Get all categoriesGET /api/product_color- Get all colorsGET /api/product_size- Get all sizesGET /api/regimen_fiscal- Get fiscal regimesGET /api/cfdi- Get CFDI types
Clients
GET /api/client- Get all clientsGET /api/client/{id}- Get single clientPOST /api/client- Create clientPUT /api/client/{id}- Update clientDELETE /api/client/{id}- Delete client
Employees
GET /api/employee- Get all employeesPOST /api/employee- Create employee (multipart/form-data)PUT /api/employee/{id}- Update employee (multipart/form-data)DELETE /api/employee/{id}- Delete employee
Quotations
GET /api/quotation_admin- Get all quotationsGET /api/quotation_admin/{id}- Get single quotationPOST /api/quotation_admin- Create quotation (multipart/form-data)GET /api/quotation_admin/{uuid}/pdf- Download quotation PDF
Events
GET /api/events- Get all eventsPOST /api/events- Create eventPUT /api/events/{id}- Update eventDELETE /api/events/{id}- Delete event
Development Proxy
The development server includes an API proxy configuration invite.config.ts:
/api/* which will be proxied to http://localhost:3333/api/*.
Best Practices
- Centralize API calls: Keep all API calls in service modules
- Error handling: Always handle errors in components using try-catch
- Type safety: Define interfaces for request/response data
- Token refresh: Implement token refresh logic for long-running sessions
- Loading states: Show loading indicators during API calls
- Cancellation: Implement request cancellation for components that unmount
Troubleshooting
CORS Errors
If you encounter CORS errors:- Verify the backend CORS configuration
- Check the
Access-Control-Allow-Originheader in interceptors - Use the development proxy in
vite.config.ts
401 Unauthorized
If you receive 401 errors:- Check if the token is present in localStorage
- Verify the token hasn’t expired
- Ensure the Authorization header is properly formatted
- Confirm the backend accepts Bearer tokens
Network Errors
For network connectivity issues:- Verify
VITE_BASE_URLis correctly set - Check if the backend server is running
- Test the endpoint using a tool like Postman
- Review the browser console for detailed error messages