@cloudbase/manager-node 4.5.0 → 4.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.FunctionService = void 0;
13
13
  const fs_1 = __importDefault(require("fs"));
14
14
  const path_1 = __importDefault(require("path"));
15
+ const lodash_1 = __importDefault(require("lodash"));
15
16
  const packer_1 = require("./packer");
16
17
  const error_1 = require("../error");
17
18
  const utils_1 = require("../utils");
@@ -733,12 +734,34 @@ class FunctionService {
733
734
  TriggerDesc: item.config
734
735
  };
735
736
  });
736
- return this.scfService.request('BatchCreateTrigger', {
737
- FunctionName: name,
738
- Namespace: namespace,
739
- Triggers: JSON.stringify(parsedTriggers),
740
- Count: parsedTriggers.length
741
- });
737
+ try {
738
+ return await this.scfService.request('BatchCreateTrigger', {
739
+ FunctionName: name,
740
+ Namespace: namespace,
741
+ Triggers: JSON.stringify(parsedTriggers),
742
+ Count: parsedTriggers.length
743
+ });
744
+ }
745
+ catch (e) {
746
+ throw new error_1.CloudBaseError(`[${name}] 创建触发器失败:${e.message}`, {
747
+ action: e.action,
748
+ code: e.code
749
+ });
750
+ }
751
+ }
752
+ // 批量部署函数触发器
753
+ async batchCreateTriggers(options) {
754
+ const { functions, envId } = options;
755
+ const promises = functions.map(func => (async () => {
756
+ try {
757
+ await this.createFunctionTriggers(func.name, func.triggers);
758
+ (0, utils_1.successLog)(`[${func.name}] 创建云函数触发器成功!`);
759
+ }
760
+ catch (e) {
761
+ throw new error_1.CloudBaseError(e.message);
762
+ }
763
+ })());
764
+ await Promise.all(promises);
742
765
  }
743
766
  /**
744
767
  * 删除云函数触发器
@@ -748,12 +771,32 @@ class FunctionService {
748
771
  */
749
772
  async deleteFunctionTrigger(name, triggerName) {
750
773
  const { namespace } = this.getFunctionConfig();
751
- return this.scfService.request('DeleteTrigger', {
752
- FunctionName: name,
753
- Namespace: namespace,
754
- TriggerName: triggerName,
755
- Type: 'timer'
756
- });
774
+ try {
775
+ await this.scfService.request('DeleteTrigger', {
776
+ FunctionName: name,
777
+ Namespace: namespace,
778
+ TriggerName: triggerName,
779
+ Type: 'timer'
780
+ });
781
+ (0, utils_1.successLog)(`[${name}] 删除云函数触发器 ${triggerName} 成功!`);
782
+ }
783
+ catch (e) {
784
+ throw new error_1.CloudBaseError(`[${name}] 删除触发器失败:${e.message}`);
785
+ }
786
+ }
787
+ async batchDeleteTriggers(options) {
788
+ const { functions, envId } = options;
789
+ const promises = functions.map(func => (async () => {
790
+ try {
791
+ func.triggers.forEach(async (trigger) => {
792
+ await this.deleteFunctionTrigger(func.name, trigger.name);
793
+ });
794
+ }
795
+ catch (e) {
796
+ throw new error_1.CloudBaseError(e.message);
797
+ }
798
+ })());
799
+ await Promise.all(promises);
757
800
  }
758
801
  /**
759
802
  * 下载云函数代码
@@ -797,6 +840,68 @@ class FunctionService {
797
840
  throw new error_1.CloudBaseError(`[${functionName}] 获取函数代码下载链接失败:\n${e.message}`);
798
841
  }
799
842
  }
843
+ // 函数绑定文件层
844
+ async attachLayer(options) {
845
+ const { envId, functionName, layerName, layerVersion, codeSecret } = options;
846
+ let { Layers = [] } = await this.getFunctionDetail(functionName, codeSecret);
847
+ Layers = Layers.map(item => lodash_1.default.pick(item, ['LayerName', 'LayerVersion']));
848
+ // 新加的文件层添加到最后
849
+ Layers.push({
850
+ LayerName: layerName,
851
+ LayerVersion: layerVersion
852
+ });
853
+ const res = await this.scfService.request('UpdateFunctionConfiguration', {
854
+ Layers,
855
+ Namespace: envId,
856
+ FunctionName: functionName
857
+ });
858
+ return res;
859
+ }
860
+ // 函数解绑文件层
861
+ async unAttachLayer(options) {
862
+ const { envId, functionName, layerName, layerVersion, codeSecret } = options;
863
+ let { Layers } = await this.getFunctionDetail(functionName, codeSecret);
864
+ Layers = Layers.map(item => lodash_1.default.pick(item, ['LayerName', 'LayerVersion']));
865
+ const index = Layers.findIndex(item => item.LayerName === layerName && item.LayerVersion === layerVersion);
866
+ if (index === -1) {
867
+ throw new error_1.CloudBaseError('层不存在');
868
+ }
869
+ // 删除指定的层
870
+ Layers.splice(index, 1);
871
+ const apiParams = {
872
+ Namespace: envId,
873
+ FunctionName: functionName,
874
+ Layers: Layers.length > 0 ? Layers : [{
875
+ LayerName: '',
876
+ LayerVersion: 0
877
+ }]
878
+ };
879
+ return this.scfService.request('UpdateFunctionConfiguration', apiParams);
880
+ }
881
+ // 更新云函数层
882
+ async updateFunctionLayer(options) {
883
+ const { envId, functionName, layers } = options;
884
+ return this.scfService.request('UpdateFunctionConfiguration', {
885
+ Layers: layers,
886
+ Namespace: envId,
887
+ FunctionName: functionName
888
+ });
889
+ }
890
+ // 下载文件层 ZIP 文件
891
+ async downloadLayer(options) {
892
+ const { name, version, destPath } = options;
893
+ const res = await this.scfService.request('GetLayerVersion', {
894
+ LayerName: name,
895
+ LayerVersion: version
896
+ });
897
+ const url = res === null || res === void 0 ? void 0 : res.Location;
898
+ const zipPath = path_1.default.join(destPath, `${name}-${version}.zip`);
899
+ if ((0, utils_1.checkFullAccess)(zipPath)) {
900
+ throw new error_1.CloudBaseError(`文件已存在:${zipPath}`);
901
+ }
902
+ // 下载文件
903
+ return (0, utils_1.downloadAndExtractRemoteZip)(url, destPath);
904
+ }
800
905
  // 创建文件层版本
801
906
  async createLayer(options) {
802
907
  const { env } = this.getFunctionConfig();
@@ -869,7 +974,7 @@ class FunctionService {
869
974
  Limit: limit,
870
975
  Offset: offset,
871
976
  SearchKey: searchKey,
872
- SearchSrc: `TCB_${env}`
977
+ // SearchSrc: `TCB_${env}`
873
978
  };
874
979
  if (runtime) {
875
980
  param.CompatibleRuntime = runtime;
@@ -1246,6 +1351,18 @@ __decorate([
1246
1351
  __decorate([
1247
1352
  (0, utils_1.preLazy)()
1248
1353
  ], FunctionService.prototype, "getFunctionDownloadUrl", null);
1354
+ __decorate([
1355
+ (0, utils_1.preLazy)()
1356
+ ], FunctionService.prototype, "attachLayer", null);
1357
+ __decorate([
1358
+ (0, utils_1.preLazy)()
1359
+ ], FunctionService.prototype, "unAttachLayer", null);
1360
+ __decorate([
1361
+ (0, utils_1.preLazy)()
1362
+ ], FunctionService.prototype, "updateFunctionLayer", null);
1363
+ __decorate([
1364
+ (0, utils_1.preLazy)()
1365
+ ], FunctionService.prototype, "downloadLayer", null);
1249
1366
  __decorate([
1250
1367
  (0, utils_1.preLazy)()
1251
1368
  ], FunctionService.prototype, "createLayer", null);
@@ -38,7 +38,6 @@ const path_1 = __importDefault(require("path"));
38
38
  const unzipper_1 = __importDefault(require("unzipper"));
39
39
  const constant_1 = require("../constant");
40
40
  const http_request_1 = require("./http-request");
41
- const log_symbols_1 = __importDefault(require("log-symbols"));
42
41
  __exportStar(require("./auth"), exports);
43
42
  __exportStar(require("./cloud-api-request"), exports);
44
43
  __exportStar(require("./cloudbase-request"), exports);
@@ -311,5 +310,5 @@ const getCompleteTimeRange = (timeRange) => {
311
310
  exports.getCompleteTimeRange = getCompleteTimeRange;
312
311
  function successLog(msg) {
313
312
  // 空格,兼容中文字符编码长度问题
314
- console.log(`${log_symbols_1.default.success} ${msg}`);
313
+ console.log(`${msg}`);
315
314
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/manager-node",
3
- "version": "4.5.0",
3
+ "version": "4.6.1",
4
4
  "description": "The node manage service api for cloudbase.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -198,6 +198,28 @@ export interface IFunctionCodeOptions {
198
198
  functionName: string;
199
199
  codeSecret?: string;
200
200
  }
201
+ export interface ILayerDownloadOptions {
202
+ name: string;
203
+ version: number;
204
+ destPath: string;
205
+ force?: boolean;
206
+ }
207
+ export interface IAttachOptions {
208
+ envId: string;
209
+ layerName: string;
210
+ layerVersion: number;
211
+ functionName: string;
212
+ codeSecret?: string;
213
+ }
214
+ export interface ISortOptions {
215
+ envId: string;
216
+ functionName: string;
217
+ layers: ILayer[];
218
+ }
219
+ export interface ILayer {
220
+ LayerName: string;
221
+ LayerVersion: number;
222
+ }
201
223
  export declare class FunctionService {
202
224
  private environment;
203
225
  private vpcService;
@@ -371,13 +393,15 @@ export declare class FunctionService {
371
393
  * @returns {Promise<IResponseInfo>}
372
394
  */
373
395
  createFunctionTriggers(name: string, triggers?: ICloudFunctionTrigger[]): Promise<IResponseInfo>;
396
+ batchCreateTriggers(options: IFunctionBatchOptions): Promise<void>;
374
397
  /**
375
398
  * 删除云函数触发器
376
399
  * @param {string} name 云函数名称
377
400
  * @param {string} triggerName 云函数触发器名称
378
401
  * @returns {Promise<IResponseInfo>}
379
402
  */
380
- deleteFunctionTrigger(name: string, triggerName: string): Promise<IResponseInfo>;
403
+ deleteFunctionTrigger(name: string, triggerName: string): Promise<void>;
404
+ batchDeleteTriggers(options: IFunctionBatchOptions): Promise<void>;
381
405
  /**
382
406
  * 下载云函数代码
383
407
  * @param {IFunctionCodeOptions} options
@@ -392,6 +416,10 @@ export declare class FunctionService {
392
416
  * @memberof FunctionService
393
417
  */
394
418
  getFunctionDownloadUrl(functionName: string, codeSecret?: string): Promise<IFunctionDownloadUrlRes>;
419
+ attachLayer(options: IAttachOptions): Promise<IResponseInfo>;
420
+ unAttachLayer(options: IAttachOptions): Promise<IResponseInfo>;
421
+ updateFunctionLayer(options: ISortOptions): Promise<IResponseInfo>;
422
+ downloadLayer(options: ILayerDownloadOptions): Promise<void>;
395
423
  createLayer(options: IFunctionLayerOptions): Promise<ICreateLayerResponse>;
396
424
  deleteLayerVersion(options: ILayerOptions): Promise<IResponseInfo>;
397
425
  listLayerVersions(options: IVersionListOptions): Promise<IListLayerVersionsRes>;