Axios

We use axios to communicate with the server in Vue applications and most new code.

In order to guarantee all defaults are set you should not use axios directly, you should import axios from axios_utils.

CSRF token

All our request require a CSRF token.
To guarantee this token is set, we are importing axios, setting the token, and exporting axios .

This exported module should be used instead of directly using axios to ensure the token is set.

Usage

importaxiosfrom'./lib/utils/axios_utils';axios.get(url).then((response)=>{// `data` is the response that was provided by the serverconstdata=response.data;// `headers` the headers that the server responded with// All header names are lower casedconstpaginationData=response.headers;}).catch(()=>{//handle the error});

Mock axios response on tests

importaxiosfrom'~/lib/utils/axios_utils';importMockAdapterfrom'axios-mock-adapter';letmock;beforeEach(()=>{// This sets the mock adapter on the default instancemock=newMockAdapter(axios);// Mock any GET request to /users// arguments for reply are (status, data, headers)mock.onGet('/users').reply(200,{users:[{id:1,name:'John Smith'}]});});afterEach(()=>{mock.reset();});

Mock poll requests on tests with axios

Because polling function requires an header object, we need to always include an object as the third argument: