Composables
useAjaxServer
Description
The useAjaxServer composable is a lightweight wrapper around the core useAjax utility. It simplifies server-side AJAX calls within a Nuxt-based application by enforcing default behaviors like suppressing UI notifications and loading indicators. This makes it particularly suitable for backend-oriented requests or use in SSR (Server Side Rendering) contexts where visual feedback is unnecessary.
It emphasizes clean error handling, optional debugging, and consistent return formats, making it easier to work with data fetching in server logic or background processes.
FEATURE GOALS
- Extend useAjax with server-safe default options.
- Suppress loading spinners and user-facing notifications.
- Return uniform error structure on failure.
- Provide an optional debugging mode for error inspection.
FEATURE EXPECTATIONS
- Accepts standard HTTP request configurations.
- Ensures all calls are silent (no UI loading indicators).
- Accepts an optional callback for post-processing response data.
- Returns a consistent error format to simplify backend logic.
- Enables debug logging by exposing raw error messages when
debugis true.
DEV STRATEGIES
- Composables Logic
const result = await useAjaxServer('/api/internal/process', {
method: 'post',
params: { jobId: 123 },
debug: true,
}, (data) => {
console.log('Server process result:', data);
});
- Ignores show_loading_indicator and show_notifications even if passed.
- Optionally logs raw error messages if
debug: true. - Executes callback (if provided) on successful response.
- Returns
{ success: false, errors: [...] }if request fails.
- COMPOSABLE STRUCTURE
useAjaxServer<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 |
|---|---|---|
show_notifications | false | Prevents user-facing popups or messages. |
show_loading_indicator | false | Avoids triggering global loading spinners. |
callback | null | Optional function called on successful fetch. |
debug | false | When true, returns full error messages. |
- ERROR RESPONSE STRUCTURE
- If the request fails,
useAjaxServerreturns:
{
success: false,
errors: string[], // generic or debug-specific messages
}
- Debug mode enables detailed error messages.
- Standardized failure structure eases error processing in server workflows.
FLOW SUMMARY
- Accepts an API endpoint, request options, and optional callback.
- Disables all UI-related indicators to suit server environments.
- Invokes
useAjaxwith modified options. - On failure, returns structured error object.
- On success, optionally runs callback and returns data.