Open NethraRaghu opened 4 years ago
agree with you, also want to know how to toggle the mock
From what I saw in the docs, it intercepts all requests... it is even possible to mock only the requests for which we have a mock code written?
I solved this in a similar way mentioned here: https://github.com/ctimmerm/axios-mock-adapter/issues/43
My implementation runs in a Vue 3 vuex mutation so I can toggle it via a UI switch.
My axios.js initilization file
const mock = new MockAdapter(axiosInstance);
const savedMockAdapter = axiosInstance.defaults.adapter
axiosInstance.defaults.adapter = `mock.originalAdapter;
export {
axiosInstance as axios,
mock as mock,
savedMockAdapter as savedMockAdapter,
}
Vuex Store:
import { axios, mock, savedMockAdapter } from '../../plugins/axios'
/*
* Toggle Mocking API on/off depending on boolean value of toggleState
*/
toggleMockApi(state, toggleState) {
if (toggleState === true) {
axios.defaults.adapter = savedMockAdapter;
state.mockApi = true;
} else {
axios.defaults.adapter = mock.originalAdapter;
state.mockApi = false;
}
},
I have a config file that tells me whether API should use mock or not. Based on the configuration it should switch between Axios or Axios mock.
I have a middleware file which does the condition check
Middleware function is called before axios.get() with the api name.
I'm getting "Request failed with status code 404" error for /api2.