Composables
useAjax
Description
The useAjax composable is a reusable utility to streamline HTTP requests across the application. It wraps Nuxt’s native $fetch with a standardized interface, built-in support for loading indicators, error handling, and optional callback integration.
This composable allows you to easily handle asynchronous data fetching while reducing boilerplate and enforcing consistency across all API calls.
FEATURE GOALS
- Provide a centralized and configurable interface for AJAX requests.
- Support common HTTP methods (
GET,POST,PUT,PATCH,DELETE). - Allow dynamic configuration of request parameters, headers, and query strings.
- Integrate seamlessly with UI elements like loading indicators.
- Trigger error handling and allow for debug-level logs.
- Allow optional callbacks to handle responses in real-time.
FEATURE EXPECTATIONS
- Accepts and merges custom options with default configurations.
- Automatically distinguishes between
GETand other request types for parameter placement (queryvsbody). - Shows or hides a global loading indicator based on config.
- Handles exceptions gracefully and allows severity tagging.
- Optionally enables verbose debugging via the
debugflag. - Provides callback support for post-response logic.
- Error handling is delegated to a custom client error handler.
- Supports future notification system integration via
show_notificationsflag (currently disabled).
DEV STRATEGIES
- Composables Logic
const response = await useAjax<User[]>('/api/users', {
method: 'get',
query: { role: 'admin' },
headers: { Authorization: 'Bearer token' },
debug: true
}, (data, response) => {
if (data) {
console.log('Success:', data);
} else {
console.warn('Failed:', response.errors);
}
});
- Merge default config with custom options.
- Based on method, assign params to body or leave as query.
- Add required headers, including JSON defaults.
- Display loading state if enabled.
- On success, invoke callback and return the response.
- On failure, trigger client error handler, log errors, invoke callback, and return a standardized error object.
- COMPOSABLE STRUCTURE
useAjax<T>(
url: string,
options?: {
params?: object,
method?: 'get' | 'post' | 'put' | 'patch' | 'delete',
query?: object,
headers?: object,
show_notifications?: boolean,
show_loading_indicator?: boolean,
severity?: string,
debug?: boolean
},
callback?: Function
): Promise<T | AjaxErrorResponse>
- DEFAULT BEHAVIOR
| Option | Default | Description |
|---|---|---|
method | get | HTTP method to be used |
params | null | Data to be sent as request body (non-GET only) |
query | null | Query string params for GET or any method |
headers | null | Custom headers (auto includes Accept: application/json) |
show_notifications | true | (Commented-out) trigger user notifications |
show_loading_indicator | true | Show/hide loading spinner |
severity | null | Custom prefix for error name |
debug | false | Show detailed error messages in response |
- ERROR RESPONSE STRUCTURE
- If the request fails, the returned response has this structure:
{
success: false,
errors: string[],
data: any
}
- If
debug: true,errorswill contain the raw error message.
FLOW SUMMARY
- Composable is invoked with a URL, optional config, and a callback.
- Internal config is merged with defaults.
$fetchis used to send the request with the final config.- A global loading indicator is optionally triggered.
- On success:
- Callback is called with response data.
- Response is returned.
- On error:
- Error is handled via
customClientErrorHandler. - Callback is called with
falseand formatted error.
- Error is handled via
- Final error response object is returned.