import Taro from "@tarojs/taro";

const request = (options) => {
  const {method, url, data} = options;
  return new Promise((resolve, reject) => {
    Taro.request({
      url: process.env.TARO_APP_API + url,
      method: method || 'GET',
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

const interceptor = function (chain) {
  const requestParams = chain.requestParams
  const {method, data, url} = requestParams

  console.table(Object.entries({ ['接口名称']: url, ['请求方式']: method, ['携带参数']: data }).map(([key, value]) => ({ Property: key, Value: value })));

  return chain.proceed(requestParams)
    .then(res => {
      console.log(`接口返回:`, res)
      return res.data
    })
}

Taro.addInterceptor(interceptor);

export default request;
