@cloudbase/manager-node 4.11.0-alpha.7 → 4.11.0-alpha.9

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.
@@ -0,0 +1,9 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(TCB_BASE_URL=https://tcb.pre.tencentcloudapi.woa.com npm test -- --testPathPattern=cloudApp 2>&1)",
5
+ "Bash(TCB_BASE_URL=https://tcb.pre.tencentcloudapi.woa.com npm test 2>&1)",
6
+ "Bash(TCB_BASE_URL=https://tcb.pre.tencentcloudapi.woa.com npm test -- --testPathPattern=cloudApp --testNamePattern=\"获取版本列表\")"
7
+ ]
8
+ }
9
+ }
@@ -0,0 +1,243 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
14
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
16
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
17
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
18
+ };
19
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
20
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.CloudAppService = void 0;
27
+ const fs_1 = __importDefault(require("fs"));
28
+ const os_1 = __importDefault(require("os"));
29
+ const path_1 = __importDefault(require("path"));
30
+ const error_1 = require("../error");
31
+ const utils_1 = require("../utils");
32
+ const http_request_1 = require("../utils/http-request");
33
+ __exportStar(require("./types"), exports);
34
+ /**
35
+ * CloudApp 统一部署服务
36
+ * 提供 Web 应用的创建、查询、删除、版本管理等功能
37
+ */
38
+ class CloudAppService {
39
+ constructor(environment) {
40
+ this.environment = environment;
41
+ this.tcbService = new utils_1.CloudService(environment.cloudBaseContext, 'tcb', '2018-06-08');
42
+ }
43
+ // =========== 应用管理 API ===========
44
+ /**
45
+ * 创建/更新 CloudApp 应用
46
+ * @param params 创建参数
47
+ */
48
+ async createApp(params) {
49
+ const { envId } = this.getEnvInfo();
50
+ this.validateRequiredParams(params, ['serviceName', 'deployType']);
51
+ return this.tcbService.request('CreateCloudApp', (0, utils_1.upperCaseObjKey)(Object.assign(Object.assign({}, params), { envId: params.envId || envId })));
52
+ }
53
+ /**
54
+ * 获取 CloudApp 应用列表
55
+ * @param params 查询参数
56
+ */
57
+ async describeAppList(params) {
58
+ const { envId } = this.getEnvInfo();
59
+ return this.tcbService.request('DescribeCloudAppList', (0, utils_1.upperCaseObjKey)(Object.assign(Object.assign({}, params), { envId: params.envId || envId })));
60
+ }
61
+ /**
62
+ * 获取 CloudApp 应用详情
63
+ * @param params 查询参数
64
+ */
65
+ async describeAppInfo(params) {
66
+ const { envId } = this.getEnvInfo();
67
+ this.validateRequiredParams(params, ['serviceName', 'deployType']);
68
+ return this.tcbService.request('DescribeCloudAppInfo', (0, utils_1.upperCaseObjKey)(Object.assign(Object.assign({}, params), { envId: params.envId || envId })));
69
+ }
70
+ /**
71
+ * 删除 CloudApp 应用
72
+ * @param params 删除参数
73
+ */
74
+ async deleteApp(params) {
75
+ const { envId } = this.getEnvInfo();
76
+ this.validateRequiredParams(params, ['serviceName', 'deployType']);
77
+ return this.tcbService.request('DeleteCloudApp', (0, utils_1.upperCaseObjKey)(Object.assign(Object.assign({}, params), { envId: params.envId || envId })));
78
+ }
79
+ // =========== 版本管理 API ===========
80
+ /**
81
+ * 获取 CloudApp 版本详情
82
+ * 不指定 versionName 或 buildId 时返回最新版本
83
+ * @param params 查询参数
84
+ */
85
+ async describeAppVersion(params) {
86
+ const { envId } = this.getEnvInfo();
87
+ this.validateRequiredParams(params, ['serviceName', 'deployType']);
88
+ return this.tcbService.request('DescribeCloudAppVersion', (0, utils_1.upperCaseObjKey)(Object.assign(Object.assign({}, params), { envId: params.envId || envId })));
89
+ }
90
+ /**
91
+ * 获取 CloudApp 版本列表
92
+ * @param params 查询参数
93
+ */
94
+ async describeAppVersionList(params) {
95
+ const { envId } = this.getEnvInfo();
96
+ this.validateRequiredParams(params, ['serviceName', 'deployType']);
97
+ return this.tcbService.request('DescribeCloudAppVersionList', (0, utils_1.upperCaseObjKey)(Object.assign(Object.assign({}, params), { envId: params.envId || envId })));
98
+ }
99
+ /**
100
+ * 删除 CloudApp 版本
101
+ * @param params 删除参数
102
+ */
103
+ async deleteAppVersion(params) {
104
+ const { envId } = this.getEnvInfo();
105
+ this.validateRequiredParams(params, ['serviceName', 'deployType', 'versionName']);
106
+ return this.tcbService.request('DeleteCloudAppVersion', (0, utils_1.upperCaseObjKey)(Object.assign(Object.assign({}, params), { envId: params.envId || envId })));
107
+ }
108
+ // =========== COS 上传相关 API ===========
109
+ /**
110
+ * 获取 CloudApp COS 上传信息(预签名 URL)
111
+ * @param params 查询参数
112
+ */
113
+ async describeCosInfo(params) {
114
+ const { envId } = this.getEnvInfo();
115
+ this.validateRequiredParams(params, ['serviceName', 'deployType']);
116
+ return this.tcbService.request('DescribeCloudAppCosInfo', (0, utils_1.upperCaseObjKey)(Object.assign(Object.assign({}, params), { envId: params.envId || envId, suffix: params.suffix || '.zip' })));
117
+ }
118
+ // =========== 辅助方法 ===========
119
+ /**
120
+ * 上传本地代码到 COS
121
+ * 自动打包 ZIP 并上传到预签名 URL
122
+ *
123
+ * 注意:此方法不依赖应用是否已创建,可在创建应用前调用
124
+ * 返回的 cosTimestamp 用于 createApp 时传入 staticConfig.cosTimestamp
125
+ *
126
+ * @param params 上传参数
127
+ */
128
+ async uploadCode(params) {
129
+ const { serviceName, localPath, ignore = [] } = params;
130
+ // 1. 验证本地路径
131
+ if (!fs_1.default.existsSync(localPath)) {
132
+ throw new error_1.CloudBaseError(`本地路径不存在: ${localPath}`);
133
+ }
134
+ const stat = fs_1.default.statSync(localPath);
135
+ if (!stat.isDirectory()) {
136
+ throw new error_1.CloudBaseError(`localPath 必须是目录: ${localPath}`);
137
+ }
138
+ // 2. 打包本地文件为 ZIP
139
+ const tmpZipPath = path_1.default.join(os_1.default.tmpdir(), `cloudapp-${Date.now()}-${Math.random().toString(36).slice(2)}.zip`);
140
+ const defaultIgnore = ['node_modules/**', '.git/**', '.DS_Store', '**/.DS_Store'];
141
+ await (0, utils_1.compressToZip)({
142
+ dirPath: localPath,
143
+ outputPath: tmpZipPath,
144
+ ignore: [...defaultIgnore, ...ignore]
145
+ });
146
+ try {
147
+ // 3. 获取上传凭证
148
+ const cosInfo = await this.describeCosInfo({
149
+ deployType: params.deployType,
150
+ serviceName,
151
+ suffix: '.zip'
152
+ });
153
+ // 4. 读取 ZIP 文件
154
+ const zipBuffer = fs_1.default.readFileSync(tmpZipPath);
155
+ // 5. 构建请求头
156
+ const headers = {
157
+ 'Content-Type': 'application/zip'
158
+ };
159
+ if (cosInfo.UploadHeaders && cosInfo.UploadHeaders.length > 0) {
160
+ for (const h of cosInfo.UploadHeaders) {
161
+ headers[h.Key] = h.Value;
162
+ }
163
+ }
164
+ // 6. 使用预签名 URL 上传
165
+ const response = await (0, http_request_1.fetchStream)(cosInfo.UploadUrl, {
166
+ method: 'PUT',
167
+ body: zipBuffer,
168
+ headers
169
+ });
170
+ if (!response.ok) {
171
+ let errorDetail = '';
172
+ try {
173
+ const responseText = await response.text();
174
+ errorDetail = responseText ? ` - ${responseText.substring(0, 500)}` : '';
175
+ }
176
+ catch (_a) {
177
+ // 解析失败时 errorDetail 保持空串,不影响主错误抛出
178
+ }
179
+ throw new error_1.CloudBaseError(`上传失败: ${response.status} ${response.statusText}${errorDetail}`);
180
+ }
181
+ // 7. 返回结果(返回 cosTimestamp,用于创建应用)
182
+ return {
183
+ cosTimestamp: cosInfo.UnixTimestamp,
184
+ unixTimestamp: cosInfo.UnixTimestamp
185
+ };
186
+ }
187
+ finally {
188
+ // 8. 清理临时文件
189
+ if (fs_1.default.existsSync(tmpZipPath)) {
190
+ fs_1.default.unlinkSync(tmpZipPath);
191
+ }
192
+ }
193
+ }
194
+ /**
195
+ * 获取环境信息
196
+ */
197
+ getEnvInfo() {
198
+ const envConfig = this.environment.lazyEnvironmentConfig;
199
+ return {
200
+ envId: envConfig.EnvId
201
+ };
202
+ }
203
+ /**
204
+ * 校验必填参数
205
+ * @param params 参数对象
206
+ * @param fields 必填字段列表
207
+ */
208
+ validateRequiredParams(params, fields) {
209
+ for (const field of fields) {
210
+ if (!params[field]) {
211
+ throw new error_1.CloudBaseError(`${field} 是必填参数`);
212
+ }
213
+ }
214
+ }
215
+ }
216
+ exports.CloudAppService = CloudAppService;
217
+ __decorate([
218
+ (0, utils_1.preLazy)()
219
+ ], CloudAppService.prototype, "createApp", null);
220
+ __decorate([
221
+ (0, utils_1.preLazy)()
222
+ ], CloudAppService.prototype, "describeAppList", null);
223
+ __decorate([
224
+ (0, utils_1.preLazy)()
225
+ ], CloudAppService.prototype, "describeAppInfo", null);
226
+ __decorate([
227
+ (0, utils_1.preLazy)()
228
+ ], CloudAppService.prototype, "deleteApp", null);
229
+ __decorate([
230
+ (0, utils_1.preLazy)()
231
+ ], CloudAppService.prototype, "describeAppVersion", null);
232
+ __decorate([
233
+ (0, utils_1.preLazy)()
234
+ ], CloudAppService.prototype, "describeAppVersionList", null);
235
+ __decorate([
236
+ (0, utils_1.preLazy)()
237
+ ], CloudAppService.prototype, "deleteAppVersion", null);
238
+ __decorate([
239
+ (0, utils_1.preLazy)()
240
+ ], CloudAppService.prototype, "describeCosInfo", null);
241
+ __decorate([
242
+ (0, utils_1.preLazy)()
243
+ ], CloudAppService.prototype, "uploadCode", null);
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ /**
3
+ * CloudApp 统一部署服务类型定义
4
+ * 支持 static-hosting(静态托管)和 http-function(预留)两种部署类型
5
+ *
6
+ * 命名规范:
7
+ * - 入参接口(IXxxParams):camelCase,通过 upperCaseObjKey 转换后透传给 API
8
+ * - 返回值接口(IXxxResult):PascalCase,与 API 响应字段保持一致
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -18,6 +18,8 @@ const access_1 = require("./access");
18
18
  const user_1 = require("./user");
19
19
  const cloudBaseRun_1 = require("./cloudBaseRun");
20
20
  const mysql_1 = require("./mysql");
21
+ const cloudApp_1 = require("./cloudApp");
22
+ const permission_1 = require("./permission");
21
23
  class Environment {
22
24
  constructor(context, envId) {
23
25
  this.inited = false;
@@ -39,6 +41,8 @@ class Environment {
39
41
  this.userService = new user_1.UserService(this);
40
42
  this.cloudBaseRunService = new cloudBaseRun_1.CloudBaseRunService(this);
41
43
  this.mysqlService = new mysql_1.MysqlService(this);
44
+ this.permissionService = new permission_1.PermissionService(this);
45
+ this.cloudAppService = new cloudApp_1.CloudAppService(this);
42
46
  }
43
47
  async lazyInit() {
44
48
  if (!this.inited) {
@@ -101,6 +105,12 @@ class Environment {
101
105
  getMysqlService() {
102
106
  return this.mysqlService;
103
107
  }
108
+ getCloudAppService() {
109
+ return this.cloudAppService;
110
+ }
111
+ getPermissionService() {
112
+ return this.permissionService;
113
+ }
104
114
  getCommonService(serviceType = 'tcb', serviceVersion) {
105
115
  return new common_1.CommonService(this, serviceType, serviceVersion);
106
116
  }
package/lib/index.js CHANGED
@@ -70,9 +70,34 @@ class CloudBase {
70
70
  get mysql() {
71
71
  return this.currentEnvironment().getMysqlService();
72
72
  }
73
+ /**
74
+ * 云托管服务(CloudBaseRun)
75
+ * 提供云托管版本流量配置等能力
76
+ * @deprecated 请使用 cloudBaseRun 代替,避免与 cloudAppService 混淆
77
+ */
73
78
  get cloudApp() {
74
79
  return this.currentEnvironment().getCloudBaseRunService();
75
80
  }
81
+ /**
82
+ * 云托管服务(CloudBaseRun)
83
+ * 提供云托管版本流量配置等能力
84
+ */
85
+ get cloudBaseRun() {
86
+ return this.currentEnvironment().getCloudBaseRunService();
87
+ }
88
+ /**
89
+ * 云应用服务(CloudApp 统一部署)
90
+ * 提供 Web 应用的创建、部署、版本管理等能力
91
+ */
92
+ get cloudAppService() {
93
+ return this.currentEnvironment().getCloudAppService();
94
+ }
95
+ /**
96
+ * 获取云应用服务
97
+ */
98
+ getCloudAppService() {
99
+ return this.currentEnvironment().getCloudAppService();
100
+ }
76
101
  commonService(service, version) {
77
102
  return this.currentEnvironment().getCommonService(service, version);
78
103
  }
@@ -88,6 +113,9 @@ class CloudBase {
88
113
  get user() {
89
114
  return this.currentEnvironment().getUserService();
90
115
  }
116
+ get permission() {
117
+ return this.currentEnvironment().getPermissionService();
118
+ }
91
119
  get docs() {
92
120
  if (!this.docsService) {
93
121
  this.docsService = new docs_1.DocsService();
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /**
18
+ * CloudApp 统一部署服务接口导出
19
+ */
20
+ __exportStar(require("../cloudApp/types"), exports);
@@ -21,3 +21,4 @@ __exportStar(require("./base.interface"), exports);
21
21
  __exportStar(require("./storage.interface"), exports);
22
22
  __exportStar(require("./cam.interface"), exports);
23
23
  __exportStar(require("./billing.interface"), exports);
24
+ __exportStar(require("./cloudApp.interface"), exports);
package/lib/log/index.js CHANGED
@@ -15,14 +15,62 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.LogService = void 0;
18
+ exports.isLogServiceEnabled = isLogServiceEnabled;
18
19
  const utils_1 = require("../utils");
19
20
  __exportStar(require("./types"), exports);
21
+ /**
22
+ * 纯函数:根据 DescribeEnvs 返回的 LogServices 列表判断日志服务是否已开通。
23
+ * 已开通条件:至少一个条目有有效的 TopicId。
24
+ */
25
+ function isLogServiceEnabled(logServices) {
26
+ if (!logServices || logServices.length === 0)
27
+ return false;
28
+ return logServices.some(svc => !!svc.TopicId);
29
+ }
20
30
  class LogService {
21
31
  constructor(environment) {
22
32
  this.envId = environment.getEnvId();
23
33
  this.cloudService = new utils_1.CloudService(environment.cloudBaseContext, 'tcb', '2018-06-08');
24
34
  this.tcbrService = new utils_1.CloudService(environment.cloudBaseContext, 'tcbr', '2022-02-17');
25
35
  }
36
+ /**
37
+ * 检查当前环境的日志服务是否已开通
38
+ *
39
+ * 通过 DescribeEnvs 接口获取环境信息,判断 LogServices 字段中是否包含有效的
40
+ * 日志主题(TopicId 不为空)。
41
+ *
42
+ * @returns true 表示已开通,false 表示未开通或开通中
43
+ */
44
+ async checkLogServiceEnabled() {
45
+ var _a;
46
+ const res = await this.cloudService.request('DescribeEnvs', {
47
+ EnvId: this.envId
48
+ });
49
+ const envInfo = (_a = res === null || res === void 0 ? void 0 : res.EnvList) === null || _a === void 0 ? void 0 : _a[0];
50
+ if (!envInfo) {
51
+ return false;
52
+ }
53
+ const logServices = envInfo.LogServices;
54
+ if (!logServices || logServices.length === 0) {
55
+ return false;
56
+ }
57
+ // 已开通:LogServiceInfo 中存在有效的日志主题 ID
58
+ return isLogServiceEnabled(logServices);
59
+ }
60
+ /**
61
+ * 开通环境日志服务(异步操作)
62
+ *
63
+ * 调用 CreateEnvResource 接口开通日志资源。
64
+ * 注意:接口调用成功不代表日志资源立即可用,需通过 checkLogServiceEnabled() 轮询确认。
65
+ *
66
+ * @returns 请求 ID
67
+ */
68
+ async createLogService() {
69
+ return this.cloudService.request('CreateEnvResource', {
70
+ EnvId: this.envId,
71
+ Resources: ['log']
72
+ });
73
+ }
26
74
  /**
27
75
  * 搜索 CLS 日志
28
76
  *