import axios from 'axios';
import config from '../config';

if (config.env === 'test') {
  const httpAdapter = require('axios/lib/adapters/http');
  axios.defaults.host = config.testHost;
  axios.defaults.adapter = httpAdapter; // eslint-disable-line
}

const methods = ['get', 'post', 'put', 'patch', 'delete'];
function formatUrl(path, directUrl = false) {
  if (directUrl) return path;
  const adjustedPath = path[0] !== '/' ? `/${path}` : path;
  if (path.indexOf('http') === 0) {
    return path;
  }
  return config.apiHost + adjustedPath;
}

class _ApiClient {
  constructor() { // we can get an access to req
    const _this = this;
    methods.forEach((method) => {
      _this[method] = (path, {
        params,
        data,
        headers,
        attachments,
        directUrl,
        handleProgress,
      } = {}) => {
        const requestConfig = {
          method,
          url: formatUrl(path, directUrl)
        };

        if (params) {
          requestConfig.params = { ...params };
        }

        if (headers) {
          requestConfig.headers = { ...headers };
        }

        if (attachments) {
          if (attachments && typeof attachments === 'object') {
            const _data = new FormData();
            Object.keys(attachments).forEach((c) => {
              _data.append(c, attachments[c]);
            });
            requestConfig.data = _data;
          }
        }

        if (data && !attachments) {
          requestConfig.data = { ...data };
        }

        if (handleProgress) {
          requestConfig.onUploadProgress = handleProgress;
        }

        return axios(requestConfig);
      };
    });
  }
}

const ApiClient = _ApiClient;

export default ApiClient;

@@ -810,16 +810,36 @@
 ogress,%0A
+        cancelToken%0A
       %7D 
@@ -1614,32 +1614,121 @@
 ess;%0A        %7D%0A%0A
+        if (cancelToken) %7B%0A          requestConfig.cancelToken = cancelToken;%0A        %7D%0A%0A
         return a

add cancel token to axios (#107)