API Core (lib/services/api/core.ts)
Why Axios for this system
Axios is a good fit here because this app needs:
- central interceptors (e.g., 401 redirect behavior)
- consistent
withCredentialscookie handling - typed request/response flow with one wrapper
- clean error translation into a project-specific
ApiError
What the core does
- Creates shared client (
apiClient) with base URL and credentials. - Handles transport-level 401 redirect checks in interceptor.
- Exposes a typed
request<T>()wrapper. - Unwraps backend response shape (
{ success, data, error, code }). - Converts API failures into
ApiErrorwithcodeandstatus. - Optionally runs centralized error handler (
handleErrors !== false).
Standard call pattern
Service methods should call request() only, not raw axios.
Example pattern (from existing services):
usersService.getUsers(...)->request<UsersListResult>({ method: 'GET', url: '/users', params })systemService.getSystemPermissionEntries(...)->request<{ permissions: SystemPermissionEntry[] }>({ method: 'GET', url: '/system/permissions' })
Edge cases handled
- backend returns
success:falsewith code/message -> typedApiError - axios throws with API body -> converted to
ApiError - non-API/network failure -> fallback
INTERNAL_ERROR
Maintainer rule
Any new frontend API integration must go through request() and a domain service module.