All files / tcb-admin-node/src/utils httpRequest.js

82.61% Statements 38/46
71.79% Branches 28/39
100% Functions 3/3
82.61% Lines 38/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 11114x 14x   14x 74x 74x 74x   74x   74x           74x 488x         74x 74x 2x 2x     74x             74x                           74x   74x 74x 74x     74x                 74x 2x 2x           72x 72x 72x         74x 1x     74x       74x     74x 74x   74x   74x   74x 74x       74x              
var request = require("request");
var auth = require("./auth.js");
 
module.exports = function (args) {
  var config = args.config,
    params = args.params,
    method = args.method || "get";
 
  const eventId = (new Date()).valueOf() + '_' + Math.random().toString().substr(2,5)
 
  params = Object.assign({}, params, {
    envName: config.envName,
    timestamp: new Date().valueOf(),
    eventId
  });
 
  for (let key in params) {
    Iif (params[key] === undefined) {
      delete params[key];
    }
  }
 
  let file = null;
  if (params.action === "storage.uploadFile") {
    file = params["file"];
    delete params["file"];
  }
 
  Iif (!config.secretId || !config.secretKey) {
    if (process.env.TENCENTCLOUD_RUNENV === 'SCF') {
      throw Error("missing authoration key, redeploy the function")
    }
    throw Error("missing secretId or secretKey of tencent cloud");
  }
 
  const authObj = {
    SecretId: config.secretId,
    SecretKey: config.secretKey,
    Method: method,
    pathname: "/admin",
    Query: params,
    Headers: Object.assign(
      {
        "user-agent": "tcb-admin-sdk"
      },
      args.headers || {}
    )
  };
 
  var authorization = auth.getAuth(authObj);
 
  params.authorization = authorization;
  file && (params.file = file);
  config.sessionToken && (params.sessionToken = config.sessionToken);
 
  // console.log(params);
  var opts = {
    // url: 'http://localhost:8002/admin',
    url: "http://tcb-admin.tencentcloudapi.com/admin",
    method: args.method || "get",
    timeout: args.timeout || 50000,
    headers: authObj.Headers,
    proxy: config.proxy
  };
 
  if (params.action === "storage.uploadFile") {
    opts.formData = params;
    opts.formData.file = {
      value: params.file,
      options: {
        filename: params.path
      }
    };
  } else Eif (args.method == "post") {
    opts.body = params;
    opts.json = true;
  } else {
    opts.qs = params;
  }
 
  if (params.action === 'wx.api') {
    opts.url = 'https://tcb-open.tencentcloudapi.com/admin'
  }
 
  Iif (args.proxy) {
    opts.proxy = args.proxy;
  }
 
  opts.url = `${opts.url}?eventId=${eventId}`
 
  // console.log(JSON.stringify(opts));
  return new Promise(function (resolve, reject) {
    request(opts, function (err, response, body) {
      // console.log(err, body);
      args && args.callback && args.callback(response)
 
      Eif (err === null && response.statusCode == 200) {
        let res;
        try {
          res = typeof body === "string" ? JSON.parse(body) : body;
        } catch (e) {
          res = body;
        }
        return resolve(res);
      } else {
        return reject(err);
      }
    });
  });
};