Appearance
Fetch Types ​
File Information ​
- Path:
app/types/fetch.ts - Purpose: TypeScript interfaces for custom fetch client
- Exported From:
~/types(viaindex.ts)
Overview ​
This file contains TypeScript interfaces for the custom fetch client configuration, responses, interceptors, and Nuxt context. These types are used for type-safe HTTP requests throughout the application.
Type Definitions ​
Fetch Configuration ​
FetchConfig ​
Fetch request configuration.
typescript
interface FetchConfig {
url?: string
method?: string
headers?: Record<string, string>
params?: Record<string, any>
data?: any
signal?: AbortSignal
controller?: AbortController
useApiBase?: boolean
baseURL?: string
}Properties:
url?: string- Request URLmethod?: string- HTTP method (GET, POST, etc.)headers?: Record<string, string>- Request headersparams?: Record<string, any>- URL query parametersdata?: any- Request body datasignal?: AbortSignal- Abort signal for cancellationcontroller?: AbortController- Abort controlleruseApiBase?: boolean- Use API base URLbaseURL?: string- Custom base URL
Usage:
typescript
import type { FetchConfig } from '~/types'
const config: FetchConfig = {
url: '/api/endpoint',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
data: { key: 'value' }
}Fetch Response ​
FetchResponse<T> ​
Generic fetch response wrapper.
typescript
interface FetchResponse<T = any> {
data: T
status: number
statusText: string
headers: Record<string, string>
config: FetchConfig
request: Response
}Generic Parameter:
T- Response data type (default:any)
Properties:
data: T- Response datastatus: number- HTTP status codestatusText: string- HTTP status textheaders: Record<string, string>- Response headersconfig: FetchConfig- Original request configrequest: Response- Native Response object
Usage:
typescript
import type { FetchResponse } from '~/types'
interface UserData {
id: number
name: string
}
const response: FetchResponse<UserData> = {
data: { id: 1, name: 'John' },
status: 200,
statusText: 'OK',
headers: {},
config: {},
request: new Response()
}Interceptor Types ​
FetchRequestInterceptor ​
Request interceptor interface.
typescript
interface FetchRequestInterceptor {
onFulfilled?: (config: FetchConfig) => FetchConfig | Promise<FetchConfig>
onRejected?: (error: any) => any
}Properties:
onFulfilled?: (config: FetchConfig) => FetchConfig | Promise<FetchConfig>- Success handleronRejected?: (error: any) => any- Error handler
Usage:
typescript
import type { FetchRequestInterceptor } from '~/types'
const interceptor: FetchRequestInterceptor = {
onFulfilled: (config) => {
// Add auth token
config.headers = {
...config.headers,
Authorization: `Bearer ${token}`
}
return config
},
onRejected: (error) => {
console.error('Request error:', error)
return error
}
}FetchResponseInterceptor ​
Response interceptor interface.
typescript
interface FetchResponseInterceptor {
onFulfilled?: (response: FetchResponse) => FetchResponse | Promise<FetchResponse>
onRejected?: (error: any) => any
}Properties:
onFulfilled?: (response: FetchResponse) => FetchResponse | Promise<FetchResponse>- Success handleronRejected?: (error: any) => any- Error handler
Usage:
typescript
import type { FetchResponseInterceptor } from '~/types'
const interceptor: FetchResponseInterceptor = {
onFulfilled: (response) => {
// Transform response
return response
},
onRejected: (error) => {
// Handle error
if (error.status === 401) {
// Handle unauthorized
}
return error
}
}Nuxt Context Types ​
FetchNuxtContext ​
Nuxt context structure for fetch client.
typescript
interface FetchNuxtContext {
$config: {
isLocal: boolean
baseUrl: string
apiBaseUrl: string
}
route: {
params: Record<string, any>
}
$auth: {
strategy: {
token: {
get(): string | null
}
}
}
$clearAuthCookies(): void
error(options: { statusCode: number; path: string; message: string }): void
}Properties:
$config- Runtime configurationroute- Current route information$auth- Authentication context$clearAuthCookies()- Clear auth cookies methoderror()- Error handler method
Usage Examples ​
Basic Fetch Request ​
typescript
import type { FetchConfig, FetchResponse } from '~/types'
async function fetchData(): Promise<FetchResponse<UserData>> {
const config: FetchConfig = {
url: '/api/user',
method: 'GET'
}
const response = await fetchClient.request<UserData>(config)
return response
}Request with Interceptors ​
typescript
import type { FetchRequestInterceptor, FetchResponseInterceptor } from '~/types'
const requestInterceptor: FetchRequestInterceptor = {
onFulfilled: (config) => {
// Add auth header
config.headers = {
...config.headers,
Authorization: `Bearer ${getToken()}`
}
return config
}
}
const responseInterceptor: FetchResponseInterceptor = {
onFulfilled: (response) => {
// Log response
console.log('Response:', response.data)
return response
},
onRejected: (error) => {
// Handle errors
if (error.status === 401) {
logout()
}
return error
}
}Related Types ​
- Used by API client plugin
- Used by composables making HTTP requests
Import Example ​
typescript
import type {
FetchConfig,
FetchResponse,
FetchRequestInterceptor,
FetchResponseInterceptor,
FetchNuxtContext
} from '~/types'Notes ​
- All fetch types are exported from
~/types FetchResponseis generic for type safety- Interceptors support both sync and async handlers
- Nuxt context types provide type safety for Nuxt-specific features
- Abort controllers supported for request cancellation