@cloudbase/app 3.1.3 → 3.1.5

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.1.3",
3
+ "version": "3.1.5",
4
4
  "description": "cloudbase javascript sdk core",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -43,8 +43,8 @@
43
43
  "dependencies": {
44
44
  "@cloudbase/adapter-interface": "^0.7.1",
45
45
  "@cloudbase/adapter-wx_mp": "^1.3.1",
46
- "@cloudbase/types": "3.1.3",
47
- "@cloudbase/utilities": "3.1.3"
46
+ "@cloudbase/types": "3.1.5",
47
+ "@cloudbase/utilities": "3.1.5"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "@cloudbase/signature-nodejs": ">=1.0.0",
@@ -62,5 +62,5 @@
62
62
  "optional": true
63
63
  }
64
64
  },
65
- "gitHead": "63a9f4c3588fa05f12f225b26750c3133766132b"
65
+ "gitHead": "63822375a03bda6d80761231321860f2c9ea3980"
66
66
  }
@@ -206,135 +206,142 @@ export class CloudbaseRequest implements ICloudbaseRequest {
206
206
  },
207
207
  customReqOpts?: ICustomReqOpts,
208
208
  ): Promise<ResponseObject> {
209
- const tcbTraceKey = `x-tcb-trace_${this.config.env}`
210
- let contentType = 'application/x-www-form-urlencoded'
209
+ try {
210
+ const tcbTraceKey = `x-tcb-trace_${this.config.env}`
211
+ let contentType = 'application/x-www-form-urlencoded'
212
+
213
+ const tmpObj: KV<any> = {
214
+ action,
215
+ dataVersion: DATA_VERSION,
216
+ env: this.config.env,
217
+ ...params,
218
+ }
211
219
 
212
- const tmpObj: KV<any> = {
213
- action,
214
- dataVersion: DATA_VERSION,
215
- env: this.config.env,
216
- ...params,
217
- }
220
+ if (ACTIONS_WITHOUT_ACCESSTOKEN.indexOf(action) === -1) {
221
+ const app = this.config._fromApp
218
222
 
219
- if (ACTIONS_WITHOUT_ACCESSTOKEN.indexOf(action) === -1) {
220
- const app = this.config._fromApp
223
+ if (!app.oauthInstance) {
224
+ throw new Error('you can\'t request without auth')
225
+ }
221
226
 
222
- if (!app.oauthInstance) {
223
- throw new Error('you can\'t request without auth')
227
+ const { oauthInstance } = app
228
+ const oauthClient = oauthInstance.oauth2client
229
+ tmpObj.access_token = (await this.getOauthAccessTokenV2(oauthClient)).accessToken
224
230
  }
225
231
 
226
- const { oauthInstance } = app
227
- const oauthClient = oauthInstance.oauth2client
228
- tmpObj.access_token = (await this.getOauthAccessTokenV2(oauthClient)).accessToken
229
- }
232
+ // 拼body和content-type
233
+ let payload
234
+ if (action === 'storage.uploadFile') {
235
+ payload = new FormData()
236
+ Object.keys(payload).forEach((key) => {
237
+ if (Object.prototype.hasOwnProperty.call(payload, key) && payload[key] !== undefined) {
238
+ payload.append(key, tmpObj[key])
239
+ }
240
+ })
241
+ contentType = 'multipart/form-data'
242
+ } else {
243
+ contentType = 'application/json;charset=UTF-8'
244
+ payload = {}
245
+ Object.keys(tmpObj).forEach((key) => {
246
+ if (tmpObj[key] !== undefined) {
247
+ payload[key] = tmpObj[key]
248
+ }
249
+ })
250
+ }
251
+ const opts: any = {
252
+ headers: {
253
+ 'content-type': contentType,
254
+ ...this.getDefaultHeaders(),
255
+ ...options?.headers,
256
+ },
257
+ }
258
+ if (options?.onUploadProgress) {
259
+ opts.onUploadProgress = options.onUploadProgress
260
+ }
230
261
 
231
- // 拼body和content-type
232
- let payload
233
- if (action === 'storage.uploadFile') {
234
- payload = new FormData()
235
- Object.keys(payload).forEach((key) => {
236
- if (Object.prototype.hasOwnProperty.call(payload, key) && payload[key] !== undefined) {
237
- payload.append(key, tmpObj[key])
238
- }
239
- })
240
- contentType = 'multipart/form-data'
241
- } else {
242
- contentType = 'application/json;charset=UTF-8'
243
- payload = {}
244
- Object.keys(tmpObj).forEach((key) => {
245
- if (tmpObj[key] !== undefined) {
246
- payload[key] = tmpObj[key]
247
- }
248
- })
249
- }
250
- const opts: any = {
251
- headers: {
252
- 'content-type': contentType,
253
- ...this.getDefaultHeaders(),
254
- ...options?.headers,
255
- },
256
- }
257
- if (options?.onUploadProgress) {
258
- opts.onUploadProgress = options.onUploadProgress
259
- }
262
+ if (this.config.region) {
263
+ opts.headers['X-TCB-Region'] = this.config.region
264
+ }
260
265
 
261
- if (this.config.region) {
262
- opts.headers['X-TCB-Region'] = this.config.region
263
- }
266
+ const traceHeader = this.localCache.getStore(tcbTraceKey)
267
+ if (traceHeader) {
268
+ opts.headers['X-TCB-Trace'] = traceHeader
269
+ }
264
270
 
265
- const traceHeader = this.localCache.getStore(tcbTraceKey)
266
- if (traceHeader) {
267
- opts.headers['X-TCB-Trace'] = traceHeader
268
- }
271
+ // 发出请求
272
+ // 新的 url 需要携带 env 参数进行 CORS 校验
273
+ // 请求链接支持添加动态 query 参数,方便用户调试定位请求
274
+ const parse = options?.parse !== undefined ? options.parse : params.parse
275
+ const inQuery = options?.inQuery !== undefined ? options.inQuery : params.inQuery
276
+ const search = options?.search !== undefined ? options.search : params.search
269
277
 
270
- // 发出请求
271
- // 新的 url 需要携带 env 参数进行 CORS 校验
272
- // 请求链接支持添加动态 query 参数,方便用户调试定位请求
273
- const parse = options?.parse !== undefined ? options.parse : params.parse
274
- const inQuery = options?.inQuery !== undefined ? options.inQuery : params.inQuery
275
- const search = options?.search !== undefined ? options.search : params.search
278
+ let formatQuery: Record<string, any> = {
279
+ ...(options?.defaultQuery || {}),
280
+ env: this.config.env,
281
+ }
282
+ // 尝试解析响应数据为 JSON
283
+ parse && (formatQuery.parse = true)
284
+ inQuery
285
+ && (formatQuery = {
286
+ ...inQuery,
287
+ ...formatQuery,
288
+ })
289
+
290
+ const endPointMode = options?.endPointMode || this.config.endPointMode || 'CLOUD_API'
291
+
292
+ const url = getEndPointInfo(this.config.env, endPointMode)
293
+ let BASE_URL = url.baseUrl
294
+ const PROTOCOL = url.protocol
295
+
296
+ if (endPointMode === 'GATEWAY') {
297
+ if (/^((database)\.)|(auth\.wsWebSign)/.test(action)) {
298
+ const url = getEndPointInfo(this.config.env, 'CLOUD_API')
299
+ BASE_URL = `${url.baseUrl.match(/\/\/([^/?#]*)/)[0]}/web`
300
+ }
301
+ }
276
302
 
277
- let formatQuery: Record<string, any> = {
278
- ...(options?.defaultQuery || {}),
279
- env: this.config.env,
280
- }
281
- // 尝试解析响应数据为 JSON
282
- parse && (formatQuery.parse = true)
283
- inQuery
284
- && (formatQuery = {
285
- ...inQuery,
286
- ...formatQuery,
287
- })
288
-
289
- const endPointMode = options?.endPointMode || this.config.endPointMode || 'CLOUD_API'
290
-
291
- const url = getEndPointInfo(this.config.env, endPointMode)
292
- let BASE_URL = url.baseUrl
293
- const PROTOCOL = url.protocol
294
-
295
- if (endPointMode === 'GATEWAY') {
296
- if (/^((database)\.)|(auth\.wsWebSign)/.test(action)) {
297
- const url = getEndPointInfo(this.config.env, 'CLOUD_API')
298
- BASE_URL = `${url.baseUrl.match(/\/\/([^/?#]*)/)[0]}/web`
303
+ // 生成请求 url
304
+ let newUrl
305
+ if (options.pathname) {
306
+ newUrl = formatUrl(
307
+ PROTOCOL,
308
+ `${getBaseEndPoint(this.config.env, endPointMode)?.replace(/^https?:/, '')}/${options.pathname}`,
309
+ formatQuery,
310
+ )
311
+ } else {
312
+ newUrl = formatUrl(PROTOCOL, BASE_URL, formatQuery)
299
313
  }
300
- }
301
314
 
302
- // 生成请求 url
303
- let newUrl
304
- if (options.pathname) {
305
- newUrl = formatUrl(
306
- PROTOCOL,
307
- `${getBaseEndPoint(this.config.env, endPointMode)?.replace(/^https?:/, '')}/${options.pathname}`,
308
- formatQuery,
309
- )
310
- } else {
311
- newUrl = formatUrl(PROTOCOL, BASE_URL, formatQuery)
312
- }
315
+ if (search) {
316
+ newUrl += search
317
+ }
313
318
 
314
- if (search) {
315
- newUrl += search
316
- }
319
+ const res: ResponseObject = await this.post(
320
+ {
321
+ url: newUrl,
322
+ data: payload,
323
+ ...opts,
324
+ },
325
+ customReqOpts,
326
+ )
317
327
 
318
- const res: ResponseObject = await this.post(
319
- {
320
- url: newUrl,
321
- data: payload,
322
- ...opts,
323
- },
324
- customReqOpts,
325
- )
328
+ // 保存 trace header
329
+ const resTraceHeader = res.header?.['x-tcb-trace']
330
+ if (resTraceHeader) {
331
+ this.localCache.setStore(tcbTraceKey, resTraceHeader)
332
+ }
326
333
 
327
- // 保存 trace header
328
- const resTraceHeader = res.header?.['x-tcb-trace']
329
- if (resTraceHeader) {
330
- this.localCache.setStore(tcbTraceKey, resTraceHeader)
331
- }
334
+ if ((Number(res.status) !== 200 && Number(res.statusCode) !== 200) || !res.data) {
335
+ throw new Error('network request error')
336
+ }
332
337
 
333
- if ((Number(res.status) !== 200 && Number(res.statusCode) !== 200) || !res.data) {
334
- throw new Error('network request error')
338
+ return res
339
+ } catch (err) {
340
+ try {
341
+ err.requestId = err.requestId || options?.headers['x-request-id'] || options?.headers['X-Request-Id'] || ''
342
+ } catch (error) {}
343
+ throw err
335
344
  }
336
-
337
- return res
338
345
  }
339
346
 
340
347
  public async fetch(options: IFetchOptions & { token?: string; customReqOpts?: ICustomReqOpts },): Promise<ResponseObject> {
@@ -357,6 +364,9 @@ export class CloudbaseRequest implements ICloudbaseRequest {
357
364
  const result = await doFetch()
358
365
  return result
359
366
  } catch (err) {
367
+ try {
368
+ err.requestId = err.requestId || headers['x-request-id'] || headers['X-Request-Id'] || ''
369
+ } catch (error) {}
360
370
  if (err?.code === 'ACCESS_TOKEN_EXPIRED') {
361
371
  // 如果是因为 token 过期失败,刷 token 后再试一次
362
372
  if (typeof this.config?._fromApp?.oauthInstance?.authApi?.refreshTokenForce !== 'function') {