use-hooks/react-hooks-axios

Provide factory to create hook bound to a custom axios instance

ThiefMaster opened this issue · 3 comments

My project currently contains this code to set up an axios instance with some customizations:

export const myAxios = axios.create({
    paramsSerializer: params => qs.stringify(params, {arrayFormat: 'repeat'}),
    xsrfCookieName: null,
    xsrfHeaderName: null,
});

myAxios.interceptors.request.use(config => {
    if (isURLSameOrigin(config.url)) {
        config.headers.common['X-Requested-With'] = 'XMLHttpRequest';  // needed for `request.is_xhr`
        config.headers.common['X-CSRF-Token'] = document.getElementById('csrf-token').getAttribute('content');
    }
    return config;
});

As far as I can see, there's no way to use the hook with this custom instance. It would be nice if I could simply do const useAxios = makeUseAxios(myAxios); in my project and then use that as the hook.

Good proposal

Could this solve a problem i have with unit testing a component with this hook?

I tried to mock axios with mockaxios but its not working. Could be error from my side. But how do you other peps test your apps that use this hook? I can for sure mock the entire hook in jest. But i would rather mock axios.

The high level of obfuscation is also unnecessary. I mean its literally impossible to read the distribution...