Appearance
useFetchClient Composable ​
File Information ​
- Path:
app/composables/api/useFetchClient.ts - Purpose: Fetch client implementation (Axios-like API using Fetch API)
- Category: Utility API Composable
Overview ​
The useFetchClient composable provides a Fetch-based HTTP client with Axios-like API, including interceptors, cancellation, and request/response handling. This is a lower-level utility used by the API client plugin.
Class: FetchClient ​
Constructor ​
typescript
constructor(context: NuxtContext)Creates a new FetchClient instance with the provided Nuxt context.
Properties ​
context: NuxtContext- Nuxt contextconfig: NuxtContext['$config']- Runtime configdefaults: { headers: Record<string, string> }- Default headersrequestInterceptors: RequestInterceptor[]- Request interceptorsresponseInterceptors: ResponseInterceptor[]- Response interceptors
Methods ​
addRequestInterceptor ​
Adds a request interceptor.
typescript
addRequestInterceptor(
onFulfilled?: (config: FetchConfig) => Promise<FetchConfig>,
onRejected?: (error: any) => any
): voidaddResponseInterceptor ​
Adds a response interceptor.
typescript
addResponseInterceptor(
onFulfilled?: (response: FetchResponse) => Promise<FetchResponse>,
onRejected?: (error: any) => any
): voidrequest ​
Main request method (similar to Axios).
typescript
request<T = any>(config: FetchConfig): Promise<FetchResponse<T>>get, post, put, patch, delete ​
HTTP method helpers.
typescript
get<T = any>(url: string, config?: FetchConfig): Promise<FetchResponse<T>>
post<T = any>(url: string, data?: any, config?: FetchConfig): Promise<FetchResponse<T>>
put<T = any>(url: string, data?: any, config?: FetchConfig): Promise<FetchResponse<T>>
patch<T = any>(url: string, data?: any, config?: FetchConfig): Promise<FetchResponse<T>>
delete<T = any>(url: string, config?: FetchConfig): Promise<FetchResponse<T>>isCancel ​
Checks if error is a cancellation error.
typescript
isCancel(error: any): booleanCancelToken ​
CancelToken equivalent for compatibility.
typescript
CancelToken = {
source: () => {
token: AbortSignal | null
cancel: (message?: string) => void
controller: AbortController | null
}
}Features ​
Request Interceptors ​
Default request interceptor setup:
- Rate Limiting: Enqueues requests (placeholder)
- AbortController: Creates abort controller for cancellation
- Throttling: Adds throttle controller for specific endpoints
- Workspace ID: Automatically adds workspace_id from route params
- Authentication: Adds Authorization header from auth token
Response Interceptors ​
Default response interceptor setup:
- Cleanup: Removes cancel/throttle controllers
- Error Handling: Handles AbortError (cancellation)
- 401 Handling: Auto-logout on 401 errors
- Error Transformation: Transforms errors to standard format
URL Building ​
- Handles base URL logic
- Supports both internal (
useApiBase: false) and external routes - Automatically adds query parameters
- Normalizes multiple slashes
Request Options ​
- Supports FormData (removes Content-Type header)
- Supports JSON (stringifies object body)
- Supports custom headers
- Supports AbortController signal
Response Handling ​
- Parses JSON responses
- Parses text responses
- Creates Axios-like response object
- Handles non-2xx responses as errors
Usage ​
Direct Usage ​
typescript
import useFetchClient from '~/composables/api/useFetchClient'
const fetchClient = useFetchClient(context)
// Make request
const response = await fetchClient.get('/api/endpoint')
const data = response.dataVia API Client Plugin ​
The API client plugin uses this composable internally:
typescript
// In app/plugins/api-client.ts
const fetchClient = useFetchClient(nuxtApp)
nuxtApp.$api = fetchClient.request.bind(fetchClient)With Cancellation ​
typescript
const { CancelToken } = useFetchClient(context)
const source = CancelToken.source()
fetchClient.get('/api/endpoint', {
signal: source.token
})
// Cancel request
source.cancel('Request cancelled')Type Definitions ​
typescript
import type {
FetchConfig,
FetchResponse,
FetchRequestInterceptor,
FetchResponseInterceptor,
FetchNuxtContext,
} from '~/types'Integration ​
The composable integrates with:
- Cancel Token Manager: For request cancellation
- Auth Module: For token injection
- Runtime Config: For base URLs and configuration
Related Documentation ​
- API Client Plugin - API client configuration
- Types - Fetch - Fetch type definitions