dist/useAxios.js
/**
 * Hooks up an axios instance to the component's state
 * @param {UseAxiosConfig} config the configuration for useAxios
 */
Hooks up an axios instance to the component's state
@param {UseAxiosConfig} config the configuration for useAxios
Hooks up an axios instance to the component ' s state @param { UseAxiosConfig } config the configuration for useAxios
function useAxios(initialConfig) {
    var _this = this;
    if (initialConfig === void 0) { initialConfig = undefined; }
    var configRef = (0,external_react_.useRef)(initialConfig);
    var config = configRef.current;
    if (!config)
        config = {};
    if (!config.axiosInstance)
        config.axiosInstance = (external_axios_default());
    var cancelTokenSource = (0,external_react_.useRef)(undefined);
    var _a = (0,external_react_.useState)(null), response = _a[0], setResponse = _a[1];
    var _b = (0,external_react_.useState)(null), error = _b[0], setError = _b[1];
    var _c = (0,external_react_.useState)(false), isLoading = _c[0], setIsLoading = _c[1];
    var execute = (0,external_react_.useCallback)(function (param1, param2) { return __awaiter(_this, void 0, void 0, function () {
        var requestConfig, r, e_1;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    setIsLoading(true);
                    if (typeof param1 === 'string') {
                        requestConfig = param2 || {};
                        requestConfig.url = param1;
                    }
                    else {
                        requestConfig = param1 || {};
                    }
                    if (config.cancelLastRequestOnExecute && cancelTokenSource.current) {
                        cancelTokenSource.current.cancel("Request cancelled because a new one was executed");
                    }
                    cancelTokenSource.current = external_axios_default().CancelToken.source();
                    requestConfig.cancelToken = cancelTokenSource.current.token;
                    _a.label = 1;
                case 1:
                    _a.trys.push([1, 3, , 4]);
                    return [4 , config.axiosInstance(requestConfig)];
                case 2:
                    r = _a.sent();
                    setResponse(r);
                    setError(null);
                    setIsLoading(false);
                    if (config.callbackOnSuccess)
                        config.callbackOnSuccess(r);
                    return [2 , r];
                case 3:
                    e_1 = _a.sent();
                    setIsLoading(false);
                    if (!external_axios_default().isCancel(e_1)) {
                        setResponse(null);
                        setError(e_1);
                    }
                    if (config.callbackOnError)
                        config.callbackOnError(e_1);
                    throw e_1; 
                case 4: return [2 ];
            }
        });
    }); }, [config]);
    var cancel = function (message) {
        if (cancelTokenSource.current) {
            cancelTokenSource.current.cancel(message);
        }
    };
    (0,external_react_.useEffect)(function () {
        return function () {
            var _a;
            (_a = cancelTokenSource.current) === null || _a === void 0 ? void 0 : _a.cancel();
        };
    }, []);
    (0,external_react_.useEffect)(function () {
        if (config.loadEagerly && config.defaultAxiosConfig) {
            execute(config.defaultAxiosConfig);
        }
        else if (config.loadEagerly && !config.defaultAxiosConfig) {
            console.warn("useAxios warning: Eager loading is enabled, but no default axios configuration is provided. Data will not be loaded eagerly");
        }
    }, []);
    function reset(dontCancelRequests) {
        if (!dontCancelRequests)
            cancel();
        setResponse(undefined);
        setError(undefined);
        setIsLoading(false);
    }
    return {
        execute: execute,
        data: response && response.data ? response.data : null,
        response: response,
        error: error,
        isLoading: isLoading,
        cancel: cancel,
        reset: reset
    };
}
function useAxios ( initialConfig ) { var _this = this ; if ( initialConfig === void 0 ) { initialConfig = undefined ; } var configRef = ( 0 , external_react_ . useRef ) ( initialConfig ) ; var config = configRef . current ; if ( ! config ) config = { } ; if ( ! config . axiosInstance ) config . axiosInstance = ( external_axios_default ( ) ) ; var cancelTokenSource = ( 0 , external_react_ . useRef ) ( undefined ) ; var _a = ( 0 , external_react_ . useState ) ( null ) , response = _a [ 0 ] , setResponse = _a [ 1 ] ; var _b = ( 0 , external_react_ . useState ) ( null ) , error = _b [ 0 ] , setError = _b [ 1 ] ; var _c = ( 0 , external_react_ . useState ) ( false ) , isLoading = _c [ 0 ] , setIsLoading = _c [ 1 ] ; var execute = ( 0 , external_react_ . useCallback ) ( function ( param1 , param2 ) { return __awaiter ( _this , void 0 , void 0 , function ( ) { var requestConfig , r , e_1 ; return __generator ( this , function ( _a ) { switch ( _a . label ) { case 0 : setIsLoading ( true ) ; if ( typeof param1 === 'string' ) { requestConfig = param2 || { } ; requestConfig . url = param1 ; } else { requestConfig = param1 || { } ; } if ( config . cancelLastRequestOnExecute && cancelTokenSource . current ) { cancelTokenSource . current . cancel ( "Request cancelled because a new one was executed" ) ; } cancelTokenSource . current = external_axios_default ( ) . CancelToken . source ( ) ; requestConfig . cancelToken = cancelTokenSource . current . token ; _a . label = 1 ; case 1 : _a . trys . push ( [ 1 , 3 , , 4 ] ) ; return [ 4 , config . axiosInstance ( requestConfig ) ] ; case 2 : r = _a . sent ( ) ; setResponse ( r ) ; setError ( null ) ; setIsLoading ( false ) ; if ( config . callbackOnSuccess ) config . callbackOnSuccess ( r ) ; return [ 2 , r ] ; case 3 : e_1 = _a . sent ( ) ; setIsLoading ( false ) ; if ( ! external_axios_default ( ) . isCancel ( e_1 ) ) { setResponse ( null ) ; setError ( e_1 ) ; } if ( config . callbackOnError ) config . callbackOnError ( e_1 ) ; throw e_1 ; case 4 : return [ 2 ] ; } } ) ; } ) ; } , [ config ] ) ; var cancel = function ( message ) { if ( cancelTokenSource . current ) { cancelTokenSource . current . cancel ( message ) ; } } ; ( 0 , external_react_ . useEffect ) ( function ( ) { return function ( ) { var _a ; ( _a = cancelTokenSource . current ) === null || _a === void 0 ? void 0 : _a . cancel ( ) ; } ; } , [ ] ) ; ( 0 , external_react_ . useEffect ) ( function ( ) { if ( config . loadEagerly && config . defaultAxiosConfig ) { execute ( config . defaultAxiosConfig ) ; } else if ( config . loadEagerly && ! config . defaultAxiosConfig ) { console . warn ( "useAxios warning: Eager loading is enabled, but no default axios configuration is provided. Data will not be loaded eagerly" ) ; } } , [ ] ) ; function reset ( dontCancelRequests ) { if ( ! dontCancelRequests ) cancel ( ) ; setResponse ( undefined ) ; setError ( undefined ) ; setIsLoading ( false ) ; } return { execute : execute , data : response && response . data ? response . data : null , response : response , error : error , isLoading : isLoading , cancel : cancel , reset : reset } ; }
Hooks up an axios instance to the component's state
 @param {UseAxiosConfig} config the configuration for useAxios
Hooks up an axios instance to the component ' s state @param { UseAxiosConfig } config the configuration for useAxios
//Set the default axios instance if a custom one hasn't been passed in /**
     * Executes an axios request with the given options
     * Takes in the same arguments as Axios()
     */ /*yield*/ /*return*/ //Rethrow it so that we might do something with it down the line /*return*/ //Automatically cancel the request if we remove the component // eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps