$accApi
The core API plugin injected by Accelerator. Handles proxying, reCAPTCHA injection, auth error handling, and concurrency limiting.
Overview
Accelerator registers a $accApi plugin that wraps $fetch with a set of behaviours applied to every request. All calls are routed through the Accelerator proxy at /acc/proxy, which forwards them to your configured backend.
Use the useAccApi composable in components, pages, composables, and Pinia stores. Access useNuxtApp().$accApi directly only outside of normal composition usage.
Setup
The plugin requires two environment variables for the backend connection. Accelerator reads these by default — just add them to your .env:
# .env
API_URL=https://your-backend.com
API_KEY=your-api-keyIf you need to customize the variable names or the API key header, set them explicitly in nuxt.config.ts:
// nuxt.config.ts
levarneAcc: {
backend: {
baseUrl: process.env.API_URL,
apiKey: process.env.API_KEY,
apiKeyHeader: 'x-api-key', // default
},
}reCAPTCHA injection
If the request body contains a captchaAction string, the plugin automatically calls useAccRecaptcha, resolves a token for that action, removes captchaAction from the body, and adds captchaToken in its place before the request is sent. This happens client-side only.
// Pass captchaAction in the body — the plugin handles the rest
const result = await useAccApi('/your-endpoint', {
method: 'POST',
body: {
captchaAction: 'submit_form',
email: 'user@example.com',
},
})
// What the server receives:
// { captchaToken: '<resolved-token>', email: 'user@example.com' }Auth error handling
On every 4xx response the plugin calls handleAuthError. If the status is 401 or 403, it syncs the auth state from the server. If no active session is found and a loginPage is configured, the user is redirected to the login page with a redirectTo query parameter preserving their current route.
Configure the login page in your nuxt.config.ts:
// nuxt.config.ts
levarneAcc: {
auth: {
enabled: true,
loginPage: '/login', // Path to your login page
// ... other auth config
},
} When redirected, users will land at /login?redirectTo=/original/path. Your login page can use the redirectTo query parameter to send users back to their intended destination after successful authentication.
Concurrency limiting
In production (client-side only), the plugin tracks concurrent in-flight requests. Once 5 or more requests are running simultaneously, new requests are delayed before firing and a warning is logged to the console. This prevents the Nuxt server and backend from being overwhelmed by bursts of parallel requests.
Proxy route
The plugin routes all calls through /acc/proxy/[...path] — a Nitro catch-all handler that:
- Strips the
/acc/proxyprefix and forwards the remaining path to the configured backend. - Forwards the request method and body (
POST,PUT,PATCH). - Attaches active session tokens from the server-side session store.
- Automatically refreshes the
accessTokenusing therefreshTokenwhen it has expired, before forwarding the request. - Server-side (SSR) only: forwards the browser's
cookieheader so it reaches the backend through the proxy.
Session and token lifetimes are configured via useAccAuth.