All files / tcb-admin-node/lib/utils cloudApiRequest.js

88% Statements 22/25
50% Branches 2/4
100% Functions 3/3
88% Lines 22/25
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  8x 8x 8x 8x 8x 8x                       23x 23x     23x 23x 23x 23x 23x 23x 23x 23x 23x 23x       23x 23x       23x       8x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tencentcloud = require("tencentcloud-sdk-nodejs");
const actionName = require("../constants/actionName");
const { Client, Models } = tencentcloud.tcb.v20180608;
const { Credential, ClientProfile, HttpProfile } = tencentcloud.common;
const actionMapping = {
    [actionName.InvokeFunction]: 'functions',
    [actionName.GetUploadFileUrl]: 'storage',
    [actionName.GetDownloadUrls]: 'storage',
    [actionName.DeleteFiles]: 'storage',
    [actionName.CreateDocument]: 'database',
    [actionName.DeleteDocument]: 'database',
    [actionName.DescribeDocument]: 'database',
    [actionName.ModifyDocument]: 'database',
    [actionName.CreateCollection]: 'database'
};
function cloudApiRequest(args) {
    const { config, params, action } = args;
    Iif (!actionName[action]) {
        throw new Error(`invalid action: ${action}`);
    }
    let cred = new Credential(config.secretId, config.secretKey);
    let httpProfile = new HttpProfile();
    httpProfile.endpoint = "tcb.tencentcloudapi.com";
    httpProfile.reqMethod = "POST";
    let clientProfile = new ClientProfile();
    clientProfile.httpProfile = httpProfile;
    let client = new Client(cred, "ap-shanghai", clientProfile);
    return new Promise((resolve, reject) => {
        let req = new Models[`${action}Request`]();
        req.deserialize(Object.assign({}, params, { CommParam: {
                EnvName: config.envName,
                Module: actionMapping[action]
            } }));
        client[action](req, function (errMsg, response) {
            Iif (errMsg) {
                reject(errMsg);
                return;
            }
            resolve(response);
        });
    });
}
exports.default = cloudApiRequest;