UNPKG

@cloudbase/lowcode-deployer

Version:

deploy weda app

116 lines (115 loc) 4.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TCECloudApiService = void 0; const AbstractClient = require('./abstract_client'); const AbstractModel = require('./abstract_model'); const Credential = require('./credential'); const { HttpProfile, ClientProfile } = require('./index'); function isObject(x) { return typeof x === 'object' && !Array.isArray(x) && x !== null; } // 移除对象中的空值,防止调用云 API 失败 function deepRemoveVoid(obj) { if (Array.isArray(obj)) { return obj.map(deepRemoveVoid); } else if (isObject(obj)) { let result = {}; for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { const value = obj[key]; if (typeof value !== 'undefined' && value !== null) { result[key] = deepRemoveVoid(value); } } } return result; } else { return obj; } } class CommonResponseModel extends AbstractModel { deserialize(params) { for (let key in params) { this[key] = params[key]; } } } class CommonError extends Error { constructor(message, code = '-1') { if (typeof message === 'object') { message.code = code; return message; } else { super(message); this.code = code; } } } class TCECloudApiService { constructor(options) { const { service, version, proxy = '', region = process.env.PRIVATE_CLOUD_API_REGION || 'chongqing', credential, getCredential, timeout = 60000, baseUrl = 'tencentcloudapi.com', endpoint = '', } = options; this.service = service; this.timeout = timeout; this.proxy = proxy; this.region = region; this.getCredential = getCredential; this.version = version; if (!this.getCredential) { if (!credential) { throw new Error('invalid credential'); } this.credential = new Credential(credential.secretId, credential.secretKey, credential.token); } else { // 初始化一个 this.credential = new Credential('', ''); } const host = `${this.service}.${baseUrl}`; let protocol = 'http://'; let httpEndpoint = host; const headers = {}; if (endpoint) { const url = new URL(endpoint); protocol = `${url.protocol}//`; httpEndpoint = url.host; headers.host = host; } this._instance = new AbstractClient(baseUrl, // http profile 存在时,http profile 优先 version, this.credential, region, new ClientProfile('HmacSHA256', new HttpProfile(protocol, httpEndpoint, 'POST', timeout, headers))); } // overload async request(action, data = {}) { var _a; if (this.getCredential) { this._instance.credential = await this.getCredential(); } const reqData = deepRemoveVoid({ ...data }); // 不存在密钥,或临时密钥过期 if (!((_a = this.credential) === null || _a === void 0 ? void 0 : _a.secretId)) { if (!this.getCredential) { throw new CommonError('You must provide credential info!'); } if (typeof this.getCredential !== 'function') { throw new CommonError('The getCredential option must be a function!'); } const credential = await this.getCredential(); if (!credential) { throw new CommonError('Calling getCredential function get no credential info!'); } this.credential = credential; this._instance.credential = credential; } return new Promise((resolve, reject) => { this._instance.request(action, reqData, new CommonResponseModel(), function (err, response) { if (err) { reject(err); } resolve(response); }); }); } } exports.TCECloudApiService = TCECloudApiService;