@cloudbase/manager-node 5.5.0 → 5.5.1-beta.0

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.
package/lib/context.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CloudBaseContext = void 0;
4
4
  const constant_1 = require("./constant");
5
5
  class CloudBaseContext {
6
- constructor({ secretId = '', secretKey = '', token = '', proxy = '', region = '', envType = '', useInternalEndpoint = undefined, internalEndpointRegion = undefined }) {
6
+ constructor({ secretId = '', secretKey = '', token = '', proxy = '', region = '', envType = '', useInternalEndpoint = undefined, internalEndpointRegion = undefined, requestFn = undefined }) {
7
7
  this.secretId = secretId;
8
8
  this.secretKey = secretKey;
9
9
  this.token = token;
@@ -12,6 +12,7 @@ class CloudBaseContext {
12
12
  this.envType = envType;
13
13
  this.useInternalEndpoint = useInternalEndpoint;
14
14
  this.internalEndpointRegion = internalEndpointRegion;
15
+ this.requestFn = requestFn;
15
16
  }
16
17
  isInternalEndpoint() {
17
18
  return this.useInternalEndpoint !== undefined ? this.useInternalEndpoint : constant_1.USE_INTERNAL_ENDPOINT;
package/lib/index.js CHANGED
@@ -19,9 +19,9 @@ class CloudBase {
19
19
  }
20
20
  constructor(config = {}) {
21
21
  this.cloudBaseConfig = {};
22
- let { secretId, secretKey, token, envId, proxy, region, envType, useInternalEndpoint, internalEndpointRegion } = config;
23
- // config 中传入的 secretId secretkey 必须同时存在
24
- if ((secretId && !secretKey) || (!secretId && secretKey)) {
22
+ let { secretId, secretKey, token, envId, proxy, region, envType, useInternalEndpoint, internalEndpointRegion, requestFn } = config;
23
+ // config 中传入的 secretId secretkey 必须同时存在(有 requestFn 时可以不传)
24
+ if (!requestFn && ((secretId && !secretKey) || (!secretId && secretKey))) {
25
25
  throw new Error('secretId and secretKey must be a pair');
26
26
  }
27
27
  this.cloudBaseConfig = {
@@ -34,6 +34,7 @@ class CloudBase {
34
34
  region,
35
35
  useInternalEndpoint,
36
36
  internalEndpointRegion,
37
+ requestFn,
37
38
  };
38
39
  // 初始化 context
39
40
  this.context = new context_1.CloudBaseContext(this.cloudBaseConfig);
@@ -53,12 +53,13 @@ function getDate(timestamp) {
53
53
  }
54
54
  class CloudService {
55
55
  /* eslint-disable-next-line */
56
- constructor(context, service, version, baseParams) {
56
+ constructor(context, service, version, baseParams, requestFn) {
57
57
  this.service = service;
58
58
  this.version = version;
59
59
  this.timeout = 60000;
60
60
  this.baseParams = baseParams || {};
61
61
  this.cloudBaseContext = context;
62
+ this.requestFn = requestFn !== null && requestFn !== void 0 ? requestFn : context.requestFn;
62
63
  }
63
64
  get baseUrl() {
64
65
  const internalEndpoint = this.cloudBaseContext.isInternalEndpoint();
@@ -90,8 +91,31 @@ class CloudService {
90
91
  }
91
92
  }
92
93
  async request(action, data = {}, method = 'POST') {
94
+ var _a;
93
95
  this.action = action;
94
96
  this.data = deepRemoveVoid(Object.assign(Object.assign({}, data), this.baseParams));
97
+ // 如果注入了 requestFn,直接委托,跳过 TC3 签名逻辑
98
+ if (this.requestFn) {
99
+ try {
100
+ const result = await this.requestFn({
101
+ service: this.service,
102
+ action,
103
+ version: this.version,
104
+ region: (_a = this.cloudBaseContext.region) !== null && _a !== void 0 ? _a : '',
105
+ payload: this.data,
106
+ });
107
+ return result;
108
+ }
109
+ catch (e) {
110
+ if (e.name === 'CloudBaseError') {
111
+ throw e;
112
+ }
113
+ throw new error_1.CloudBaseError(e.message, {
114
+ action,
115
+ code: e.code
116
+ });
117
+ }
118
+ }
95
119
  this.method = method;
96
120
  this.url = this.baseUrl;
97
121
  let { secretId, secretKey, token } = this.cloudBaseContext;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/manager-node",
3
- "version": "5.5.0",
3
+ "version": "5.5.1-beta.0",
4
4
  "description": "The node manage service api for cloudbase.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -1,3 +1,15 @@
1
+ /**
2
+ * 自定义 API 请求函数,由外部注入,替代 TC3 签名发请求。
3
+ * 入参对齐 CAPI 模式:service + action + version + region + payload。
4
+ * 返回腾讯云 API 响应中 Response 字段的内容(已解包,不含 Response 包装层)。
5
+ */
6
+ export type CloudApiRequestFn = (params: {
7
+ service: string;
8
+ action: string;
9
+ version: string;
10
+ region: string;
11
+ payload: Record<string, any>;
12
+ }) => Promise<any>;
1
13
  export declare class CloudBaseContext {
2
14
  readonly secretId: string;
3
15
  readonly secretKey: string;
@@ -8,15 +20,17 @@ export declare class CloudBaseContext {
8
20
  readonly envType: string;
9
21
  readonly useInternalEndpoint?: boolean;
10
22
  readonly internalEndpointRegion?: string;
11
- constructor({ secretId, secretKey, token, proxy, region, envType, useInternalEndpoint, internalEndpointRegion }: {
23
+ readonly requestFn?: CloudApiRequestFn;
24
+ constructor({ secretId, secretKey, token, proxy, region, envType, useInternalEndpoint, internalEndpointRegion, requestFn }: {
12
25
  secretId?: string;
13
26
  secretKey?: string;
14
27
  token?: string;
15
28
  proxy?: string;
16
29
  region?: string;
17
30
  envType?: string;
18
- useInternalEndpoint?: any;
19
- internalEndpointRegion?: any;
31
+ useInternalEndpoint?: boolean;
32
+ internalEndpointRegion?: string;
33
+ requestFn?: CloudApiRequestFn;
20
34
  });
21
35
  isInternalEndpoint(): boolean;
22
36
  getInternalEndpointRegion(): string;
package/types/index.d.ts CHANGED
@@ -5,6 +5,7 @@ import { AgentService } from './agent';
5
5
  import { StorageService } from './storage';
6
6
  import { DatabaseService } from './database';
7
7
  import { LogService } from './log';
8
+ import { CloudApiRequestFn } from './context';
8
9
  import { CommonService } from './common';
9
10
  import { HostingService } from './hosting';
10
11
  import { Environment } from './environment';
@@ -29,6 +30,7 @@ interface CloudBaseConfig {
29
30
  envType?: string;
30
31
  useInternalEndpoint?: boolean;
31
32
  internalEndpointRegion?: string;
33
+ requestFn?: CloudApiRequestFn;
32
34
  }
33
35
  declare class CloudBase {
34
36
  private static cloudBase;
@@ -89,4 +91,7 @@ declare class CloudBase {
89
91
  getManagerConfig(): CloudBaseConfig;
90
92
  get isInternalEndpoint(): Boolean;
91
93
  }
94
+ declare namespace CloudBase {
95
+ type RequestFn = CloudApiRequestFn;
96
+ }
92
97
  export = CloudBase;
@@ -1,4 +1,4 @@
1
- import { CloudBaseContext } from '../context';
1
+ import { CloudBaseContext, CloudApiRequestFn } from '../context';
2
2
  export declare class CloudService {
3
3
  service: string;
4
4
  version: string;
@@ -13,7 +13,8 @@ export declare class CloudService {
13
13
  payload: Record<string, any>;
14
14
  baseParams: Record<string, any>;
15
15
  cloudBaseContext: CloudBaseContext;
16
- constructor(context: CloudBaseContext, service: string, version: string, baseParams?: Record<string, any>);
16
+ private requestFn?;
17
+ constructor(context: CloudBaseContext, service: string, version: string, baseParams?: Record<string, any>, requestFn?: CloudApiRequestFn);
17
18
  get baseUrl(): any;
18
19
  request<T extends {}>(action: string, data?: Record<string, any>, method?: 'POST' | 'GET'): Promise<T>;
19
20
  requestWithSign(): Promise<any>;