@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/dist/cjs/libs/request.js +32 -18
- package/dist/esm/libs/request.js +32 -18
- package/dist/miniprogram/index.js +1 -1
- package/package.json +4 -4
- package/src/libs/request.ts +122 -112
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudbase/app",
|
|
3
|
-
"version": "3.1.
|
|
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.
|
|
47
|
-
"@cloudbase/utilities": "3.1.
|
|
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": "
|
|
65
|
+
"gitHead": "63822375a03bda6d80761231321860f2c9ea3980"
|
|
66
66
|
}
|
package/src/libs/request.ts
CHANGED
|
@@ -206,135 +206,142 @@ export class CloudbaseRequest implements ICloudbaseRequest {
|
|
|
206
206
|
},
|
|
207
207
|
customReqOpts?: ICustomReqOpts,
|
|
208
208
|
): Promise<ResponseObject> {
|
|
209
|
-
|
|
210
|
-
|
|
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
|
-
|
|
213
|
-
|
|
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
|
-
|
|
220
|
-
|
|
223
|
+
if (!app.oauthInstance) {
|
|
224
|
+
throw new Error('you can\'t request without auth')
|
|
225
|
+
}
|
|
221
226
|
|
|
222
|
-
|
|
223
|
-
|
|
227
|
+
const { oauthInstance } = app
|
|
228
|
+
const oauthClient = oauthInstance.oauth2client
|
|
229
|
+
tmpObj.access_token = (await this.getOauthAccessTokenV2(oauthClient)).accessToken
|
|
224
230
|
}
|
|
225
231
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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
|
-
|
|
262
|
-
|
|
263
|
-
|
|
266
|
+
const traceHeader = this.localCache.getStore(tcbTraceKey)
|
|
267
|
+
if (traceHeader) {
|
|
268
|
+
opts.headers['X-TCB-Trace'] = traceHeader
|
|
269
|
+
}
|
|
264
270
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
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
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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
|
-
|
|
315
|
-
|
|
316
|
-
|
|
319
|
+
const res: ResponseObject = await this.post(
|
|
320
|
+
{
|
|
321
|
+
url: newUrl,
|
|
322
|
+
data: payload,
|
|
323
|
+
...opts,
|
|
324
|
+
},
|
|
325
|
+
customReqOpts,
|
|
326
|
+
)
|
|
317
327
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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
|
-
|
|
334
|
-
|
|
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') {
|