All files / tcb-admin-node/src/utils cloudApiRequest.ts

87.5% Statements 21/24
50% Branches 2/4
100% Functions 3/3
87.5% Lines 21/24
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 894x 4x                                 4x 4x   4x                                               4x 13x 13x       13x 12x 12x 12x 12x 12x       12x   12x   12x   12x               12x   12x         12x              
import * as tencentcloud from "tencentcloud-sdk-nodejs";
import * as actionName from '../constants/actionName'
import {
    DescribeDocumentParams,
    ModifyDocumentParams,
    DeleteDocumentParams,
    ModifyDocumentResponse
} from '../interface/cloudApi/database'
import {
    InvokeFunctionParams
} from '../interface/cloudApi/functions'
import {
    GetDownloadUrlsParams,
    DeleteFilesParams
} from '../interface/cloudApi/storage'
import {
    CloudApiResponse,
} from '../interface/cloudApi'
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'
}
 
interface tcbConfig {
    secretId: string
    secretKey: string
    envName: string
}
 
interface cloudApiRequestArgs {
    config: tcbConfig
    params: DescribeDocumentParams | ModifyDocumentParams | DeleteDocumentParams | ModifyDocumentResponse | InvokeFunctionParams | GetDownloadUrlsParams | DeleteFilesParams
    action: string
}
 
export default function cloudApiRequest(args: cloudApiRequestArgs): Promise<CloudApiResponse> {
    const { config, params, action } = args
    Iif (!actionName[action]) {
        throw new Error(`invalid action: ${action}`)
    }
    // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    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;
 
 
    // 实例化要请求产品(以cvm为例)的client对象
    let client = new Client(cred, "ap-shanghai", clientProfile);
 
    return new Promise((resolve, reject) => {
        // 实例化一个请求对象
        let req = new Models[`${action}Request`]();
 
        req.deserialize({
            ...params,
            CommParam: {
                EnvName: config.envName,
                Module: actionMapping[action]
            }
        })
        // 通过client对象调用想要访问的接口,需要传入请求对象以及响应回调函数
        client[action](req, function (errMsg, response) {
            // 请求异常返回,打印异常信息
            Iif (errMsg) {
                reject(errMsg);
                return;
            }
            // 请求正常返回,打印response对象
            resolve(response);
        });
    })
}