'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var axios = require('axios');
var rainbow_helper_helper = require('./helper.js');
var rainbow_helper_rainbowSignature = require('./rainbow-signature.js');
require('../../nodejs/crypto.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);

/**
 * 基本请求
 * @private
 * @param {object} config - 配置信息
 * @returns {Promise} 请求Promise
 * @example
 * ```ts
 * baseRequestRainbow({
 *   url: '/api/some-path',
 *   data: { foo: 1 },
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'yyy',
 *     secretKey: 'zzz',
 *     envName: 'Default',
 *     groupName: 'group',
 *   },
 * }).then(res => console.log(res));
 * ```
 */
function baseRequestRainbow(_ref) {
  var url = _ref.url,
    reqData = _ref.data,
    secretInfo = _ref.secretInfo;
  return new Promise(function (resolve, reject) {
    var baseUrl = rainbow_helper_helper.BASE_URL;
    var appID = secretInfo.appID,
      appId = secretInfo.appId,
      userID = secretInfo.userID,
      userId = secretInfo.userId,
      secretKey = secretInfo.secretKey,
      envName = secretInfo.envName,
      groupName = secretInfo.groupName;
    var realSig = rainbow_helper_rainbowSignature.genRainbowHeaderSignature({
      appID: appID,
      appId: appId,
      userID: userID,
      userId: userId,
      secretKey: secretKey
    });
    axios__default["default"]({
      url: "".concat(baseUrl).concat(url),
      data: Object.assign({
        app_id: appId || appID,
        env_name: envName,
        group_name: groupName
      }, reqData),
      method: 'POST',
      headers: Object.assign({
        'Content-Type': 'application/json'
      }, realSig)
    }).then(function (res) {
      if (!res.data.ret_code) {
        resolve(res.data);
      } else {
        reject(res.data);
      }
    })["catch"](function (e) {
      reject(e);
    });
  });
}

exports.baseRequestRainbow = baseRequestRainbow;
