import request from "../../../core/utils/request";
import '../hooks';
/*
* 处理proxy
* */

const restUrl = url => {
  if (url.indexOf('.') < 0) {
    return url;
  }

  const hasProxy = /\/proxy\//.test(url);
  return `/api/v1${hasProxy ? '' : '/proxy'}${url}`;
};

const proxy = async (url, data, method = 'GET') => {
  try {
    const [err, result] = await request({
      url: restUrl(url),
      data,
      method
    });

    if (err && err.code !== 200) {
      return Promise.reject(err);
    } else {
      return Promise.resolve(result);
    }
  } catch (e) {
    return Promise.reject(e);
  }
};

export default {
  get: (url, data) => proxy(url, data),
  post: (url, data) => proxy(url, data, 'POST')
};