@cloudbase/app 3.0.3 → 3.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/app",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "description": "cloudbase javascript sdk core",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -31,8 +31,8 @@
31
31
  "dependencies": {
32
32
  "@cloudbase/adapter-interface": "^0.7.1",
33
33
  "@cloudbase/adapter-wx_mp": "^1.2.1",
34
- "@cloudbase/types": "3.0.3",
35
- "@cloudbase/utilities": "3.0.3"
34
+ "@cloudbase/types": "3.0.4",
35
+ "@cloudbase/utilities": "3.0.4"
36
36
  },
37
- "gitHead": "f636691d8dd8edef8c44e788512fa5a1bbbb11af"
37
+ "gitHead": "b5f5f8541b7e0d944bf345f8aa23c4e0ccb42b4f"
38
38
  }
package/src/index.ts CHANGED
@@ -101,12 +101,6 @@ class Cloudbase implements ICloudbase {
101
101
  ],
102
102
  })
103
103
  public init(config: ICloudbaseConfig & { lang?: LANGS }): Cloudbase {
104
- if (!config.env) {
105
- throw new Error(JSON.stringify({
106
- code: ERRORS.INVALID_PARAMS,
107
- msg: 'env must not be specified',
108
- }),)
109
- }
110
104
  config.endPointMode = config.endPointMode || 'GATEWAY'
111
105
 
112
106
  // 初始化时若未兼容平台,则使用默认adapter
@@ -114,11 +108,22 @@ class Cloudbase implements ICloudbase {
114
108
  this.useDefaultAdapter()
115
109
  }
116
110
 
117
- const reqConfig: IRequestConfig = {
111
+ // 在判断config.env之前,先处理node环境的配置
112
+ config = this.dealNodeAdapterConfig(config)
113
+
114
+ if (!config.env) {
115
+ throw new Error(JSON.stringify({
116
+ code: ERRORS.INVALID_PARAMS,
117
+ msg: 'env must not be specified',
118
+ }),)
119
+ }
120
+
121
+ const reqConfig: IRequestConfig & { auth: ICloudbaseConfig['auth'] } = {
118
122
  timeout: config.timeout || 5000,
119
123
  timeoutMsg: `[${getSdkName()}][REQUEST TIMEOUT] request had been abort since didn't finished within${
120
124
  config.timeout / 1000
121
125
  }s`,
126
+ auth: config.auth,
122
127
  }
123
128
 
124
129
  this.requestClient = new Platform.adapter.reqClass(reqConfig)
@@ -148,12 +153,18 @@ class Cloudbase implements ICloudbase {
148
153
  _fromApp: app,
149
154
  i18n,
150
155
  endPointMode: config.endPointMode,
156
+ auth: config.auth,
151
157
  })
152
158
  app.requestClient = this.requestClient
153
159
  ;(this as any)?.fire?.('cloudbase_init', app)
154
160
 
155
161
  this.try2InitAuth(app)
156
162
 
163
+ // node环境可以从adapter获取nodeTool的方法
164
+ if (Platform.adapter?.nodeTool) {
165
+ Platform.adapter?.nodeTool(app, this.cloudbaseConfig)
166
+ }
167
+
157
168
  return app
158
169
  }
159
170
 
@@ -273,6 +284,39 @@ class Cloudbase implements ICloudbase {
273
284
  console.log('try2InitAuth error:', error)
274
285
  }
275
286
  }
287
+
288
+ /**
289
+ * 处理 Node 环境下 adapter 的配置信息
290
+ * 在 Node 环境中,通过 adapter 自动获取密钥、环境ID、accessKey 等配置,
291
+ * 减少用户手动传入配置的负担
292
+ * @param config - 云开发初始化配置对象
293
+ * @returns 补充了 Node adapter 信息后的配置对象
294
+ */
295
+ private dealNodeAdapterConfig(config: ICloudbaseConfig) {
296
+ // node环境可以从adapter获取默认的secretId和secretKey
297
+ if (Platform.adapter?.getSecretInfo) {
298
+ const secretInfo = Platform.adapter?.getSecretInfo(config)
299
+ // 将 adapter 提供的密钥信息写入 config.auth
300
+ config.auth = {
301
+ secretId: secretInfo.secretId,
302
+ secretKey: secretInfo.secretKey,
303
+ sessionToken: secretInfo.sessionToken,
304
+ secretType: secretInfo.secretType as any,
305
+ }
306
+
307
+ // node 环境可能可以从上下文获取env,用户未显式指定时自动填充
308
+ if (!config.env) {
309
+ config.env = secretInfo.env
310
+ }
311
+
312
+ // node adapter环境能读取到 process.env.CLOUDBASE_APIKEY 时,在没有传入accessKey时,使用 node adapter 提供的 accessKey(process.env.CLOUDBASE_APIKEY)
313
+ if (!config.accessKey && secretInfo.accessKey) {
314
+ config.accessKey = secretInfo.accessKey
315
+ }
316
+ }
317
+
318
+ return config
319
+ }
276
320
  }
277
321
 
278
322
  // 类型导出
@@ -113,10 +113,11 @@ export class CloudbaseRequest implements ICloudbaseRequest {
113
113
  */
114
114
  constructor(config: ICloudbaseRequestConfig & { throw?: boolean }) {
115
115
  this.config = config
116
- const reqConfig: IRequestConfig = {
116
+ const reqConfig: IRequestConfig & { auth: ICloudbaseRequestConfig['auth'] } = {
117
117
  timeout: this.config.timeout,
118
118
  timeoutMsg: `[@cloudbase/js-sdk] 请求在${this.config.timeout / 1000}s内未完成,已中断`,
119
119
  restrictedMethods: ['post', 'put'],
120
+ auth: config.auth,
120
121
  }
121
122
  this.reqClass = new Platform.adapter.reqClass(reqConfig)
122
123
  this.throwWhenRequestFail = config.throw || false
@@ -201,6 +202,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
201
202
  search?: string
202
203
  defaultQuery?: KV<any>
203
204
  headers?: KV<string>
205
+ endPointMode?: 'CLOUD_API' | 'GATEWAY'
204
206
  },
205
207
  customReqOpts?: ICustomReqOpts,
206
208
  ): Promise<ResponseObject> {
@@ -284,7 +286,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
284
286
  ...formatQuery,
285
287
  })
286
288
 
287
- const endPointMode = this.config.endPointMode || 'CLOUD_API'
289
+ const endPointMode = options?.endPointMode || this.config.endPointMode || 'CLOUD_API'
288
290
 
289
291
  const url = getEndPointInfo(this.config.env, endPointMode)
290
292
  let BASE_URL = url.baseUrl