All files / tcb-admin-node/src/functions index.js

80% Statements 12/15
50% Branches 3/6
100% Functions 2/2
80% Lines 12/15
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 5414x                 1x 1x       1x               1x           1x                 1x     1x 1x 1x   1x               14x  
const httpRequest = require("../utils/httpRequest");
 
/**
 * 调用云函数
 * @param {String} name  函数名
 * @param {Object} functionParam 函数参数
 * @return {Promise}
 */
function callFunction({ name, data }) {
  try {
    data = data ? JSON.stringify(data) : "";
  } catch (e) {
    return Promise.reject(e);
  }
  Iif (!name) {
    return Promise.reject(
      new Error({
        message: "函数名不能为空"
      })
    );
  }
 
  let params = {
    action: "functions.invokeFunction",
    function_name: name,
    request_data: data
  };
 
  return httpRequest({
    config: this.config,
    params,
    method: "post",
    headers: {
      "content-type": "application/json"
    }
  }).then(res => {
    // console.log(res);
    Iif (res.code) {
      return res;
    } else {
      let result = res.data.response_data
      try {
        result = JSON.parse(res.data.response_data)
      } catch (e) { }
      return {
        result,
        requestId: res.requestId
      };
    }
  });
}
 
exports.callFunction = callFunction;