useAxios(), and every response, success or error, through useResponse(). That is what keeps the headers, the toasts and the Pinia stores consistent across projects.
A request
useAxios() returns a new axios instance with the helpers interceptors. Its $ methods resolve to the JSON body (res.data of axios), so response is the autoAjax response: message, data, store.
useAxios()
FormData; useObjectToFormData(object) converts a nested object:
On a Nuxt server,
useAxios() captures the current request when it is created. Create it in setup, a plugin or a hook, or pass the nuxtApp after an await: useAxios(nuxtApp).Headers
Every request gets:
The backend reads
app-locale to set the language and app-type to choose the bootstrap class. Add your own headers through Axios.addHeaders():
plugins/01.app.js
A callback registered with a key is global and replaces the previous one of that key, so it must read request state through the
pinia it is handed, never through a store captured elsewhere. Without a key, a callback registered in a plugin belongs to the current request only.
useResponse()
useResponse(response) does two things.
It binds store into the Pinia stores. Every key of the store object goes into the store with the same id, as described in Sharing data with the frontend.
It shows the message as a toast:
The toast lasts
data.toast_duration, set on the backend by autoAjax()->duration($seconds).
Without arguments useResponse() returns the Response object:
Offline requests
$postAsync, $getAsync and $deleteAsync are for requests which must reach the backend even when the device is offline: a tracked event, a read receipt, a device token.
useResponse() automatically. A failed request is stored in the ajax store and sent again by the network loop after options.failedRequestDelaySeconds (60 s by default). The third argument is a key; a second request with the same key is not queued while one is waiting. The bootstrap can tune the queue with an ajax section:
Forms with v-autoAjax
Projects using the autoajax JS package for forms route its requests through the same axios and response:
plugins/03.autoajax.js