Skip to content

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 withCredentials cookie handling
  • typed request/response flow with one wrapper
  • clean error translation into a project-specific ApiError

What the core does

  1. Creates shared client (apiClient) with base URL and credentials.
  2. Handles transport-level 401 redirect checks in interceptor.
  3. Exposes a typed request<T>() wrapper.
  4. Unwraps backend response shape ({ success, data, error, code }).
  5. Converts API failures into ApiError with code and status.
  6. 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:false with code/message -> typed ApiError
  • 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.