@cloudbase/app 2.21.5 → 2.21.7
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/constants/common.d.ts +1 -1
- package/dist/cjs/constants/common.js +10 -3
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/libs/request.d.ts +13 -0
- package/dist/cjs/libs/request.js +44 -1
- package/dist/esm/constants/common.d.ts +1 -1
- package/dist/esm/constants/common.js +10 -3
- package/dist/esm/index.js +2 -2
- package/dist/esm/libs/request.d.ts +13 -0
- package/dist/esm/libs/request.js +44 -1
- package/dist/miniprogram/index.js +1 -1
- package/package.json +4 -4
- package/src/constants/common.ts +11 -2
- package/src/index.ts +1 -1
- package/src/libs/request.ts +75 -11
package/src/constants/common.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { EndPointKey } from '@cloudbase/types'
|
|
2
2
|
import { constants } from '@cloudbase/utilities'
|
|
3
3
|
|
|
4
|
+
const ZONE_CHINA = ['ap-shanghai', 'ap-guangzhou', 'ap-shenzhen-fsi', 'ap-shanghai-fsi', 'ap-nanjing', 'ap-beijing', 'ap-chengdu', 'ap-chongqing', 'ap-hongkong']
|
|
5
|
+
|
|
4
6
|
// @ts-ignore
|
|
5
7
|
const { setSdkName: setUtilitiesSdkName, setProtocol: setUtilitiesProtocol } = constants
|
|
6
8
|
/**
|
|
@@ -76,8 +78,15 @@ export interface ISetEndPointWithKey {
|
|
|
76
78
|
protocol?: 'http' | 'https'
|
|
77
79
|
}
|
|
78
80
|
|
|
79
|
-
export function setGatewayEndPointWithEnv(env: string, protocol?: 'http' | 'https') {
|
|
80
|
-
|
|
81
|
+
export function setGatewayEndPointWithEnv(env: string, protocol?: 'http' | 'https', region = 'ap-shanghai') {
|
|
82
|
+
region = region || 'ap-shanghai'
|
|
83
|
+
let baseUrl = `//${env}.api.tcloudbasegateway.com/v1`
|
|
84
|
+
|
|
85
|
+
if (!ZONE_CHINA.includes(region)) {
|
|
86
|
+
baseUrl = `//${env}.api.intl.tcloudbasegateway.com/v1`
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
setEndPointInfo({ endPointKey: 'GATEWAY', env, baseUrl, protocol })
|
|
81
90
|
}
|
|
82
91
|
export function setRegionLevelEndpoint(env: string, region: string, protocol?: 'http' | 'https') {
|
|
83
92
|
const baseUrl = `//${env}.${region || 'ap-shanghai'}.tcb-api.tencentcloudapi.com/web`
|
package/src/index.ts
CHANGED
|
@@ -138,7 +138,7 @@ class Cloudbase implements ICloudbase {
|
|
|
138
138
|
initCache({ env, persistence, debug, platformInfo: this.platform })
|
|
139
139
|
|
|
140
140
|
setRegionLevelEndpoint(env, config.region || '')
|
|
141
|
-
setGatewayEndPointWithEnv(env)
|
|
141
|
+
setGatewayEndPointWithEnv(env, 'https', config.region || '')
|
|
142
142
|
|
|
143
143
|
const app = new Cloudbase(this.cloudbaseConfig)
|
|
144
144
|
initRequest({
|
package/src/libs/request.ts
CHANGED
|
@@ -80,6 +80,13 @@ function beforeEach(): IAppendedRequestInfo {
|
|
|
80
80
|
},
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
+
export interface IGateWayOptions {
|
|
84
|
+
name: string
|
|
85
|
+
data?: any
|
|
86
|
+
path: string
|
|
87
|
+
method: string
|
|
88
|
+
header?: {}
|
|
89
|
+
}
|
|
83
90
|
export interface ICloudbaseRequest {
|
|
84
91
|
post: (options: IRequestOptions) => Promise<ResponseObject>
|
|
85
92
|
upload: (options: IUploadRequestOptions) => Promise<ResponseObject>
|
|
@@ -127,7 +134,11 @@ export class CloudbaseRequest implements ICloudbaseRequest {
|
|
|
127
134
|
}
|
|
128
135
|
|
|
129
136
|
public async post(options: IRequestOptions, customReqOpts?: ICustomReqOpts): Promise<ResponseObject> {
|
|
130
|
-
const res = await this.reqClass.post({
|
|
137
|
+
const res = await this.reqClass.post({
|
|
138
|
+
...options,
|
|
139
|
+
headers: { ...options.headers, ...this.getDefaultHeaders() },
|
|
140
|
+
customReqOpts,
|
|
141
|
+
})
|
|
131
142
|
return res
|
|
132
143
|
}
|
|
133
144
|
public async upload(options: IUploadRequestOptions): Promise<ResponseObject> {
|
|
@@ -135,7 +146,10 @@ export class CloudbaseRequest implements ICloudbaseRequest {
|
|
|
135
146
|
return res
|
|
136
147
|
}
|
|
137
148
|
public async download(options: IRequestOptions): Promise<ResponseObject> {
|
|
138
|
-
const res = await this.reqClass.download({
|
|
149
|
+
const res = await this.reqClass.download({
|
|
150
|
+
...options,
|
|
151
|
+
headers: { ...options.headers, ...this.getDefaultHeaders() },
|
|
152
|
+
})
|
|
139
153
|
return res
|
|
140
154
|
}
|
|
141
155
|
|
|
@@ -164,7 +178,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
|
|
|
164
178
|
search?: string
|
|
165
179
|
defaultQuery?: KV<any>
|
|
166
180
|
},
|
|
167
|
-
customReqOpts?: ICustomReqOpts
|
|
181
|
+
customReqOpts?: ICustomReqOpts,
|
|
168
182
|
): Promise<ResponseObject> {
|
|
169
183
|
const tcbTraceKey = `x-tcb-trace_${this.config.env}`
|
|
170
184
|
let contentType = 'application/x-www-form-urlencoded'
|
|
@@ -271,11 +285,14 @@ export class CloudbaseRequest implements ICloudbaseRequest {
|
|
|
271
285
|
newUrl += search
|
|
272
286
|
}
|
|
273
287
|
|
|
274
|
-
const res: ResponseObject = await this.post(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
288
|
+
const res: ResponseObject = await this.post(
|
|
289
|
+
{
|
|
290
|
+
url: newUrl,
|
|
291
|
+
data: payload,
|
|
292
|
+
...opts,
|
|
293
|
+
},
|
|
294
|
+
customReqOpts,
|
|
295
|
+
)
|
|
279
296
|
|
|
280
297
|
// 保存 trace header
|
|
281
298
|
const resTraceHeader = res.header?.['x-tcb-trace']
|
|
@@ -290,7 +307,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
|
|
|
290
307
|
return res
|
|
291
308
|
}
|
|
292
309
|
|
|
293
|
-
public async fetch(options: IFetchOptions & { token?: string }): Promise<ResponseObject> {
|
|
310
|
+
public async fetch(options: IFetchOptions & { token?: string; customReqOpts?: ICustomReqOpts },): Promise<ResponseObject> {
|
|
294
311
|
const { token, headers = {}, ...restOptions } = options
|
|
295
312
|
const getAccessToken = async () => {
|
|
296
313
|
if (token != null) {
|
|
@@ -338,8 +355,18 @@ export class CloudbaseRequest implements ICloudbaseRequest {
|
|
|
338
355
|
}
|
|
339
356
|
}
|
|
340
357
|
|
|
341
|
-
public async send(
|
|
342
|
-
|
|
358
|
+
public async send(
|
|
359
|
+
action: string,
|
|
360
|
+
data: KV<any> = {},
|
|
361
|
+
options: KV<any> = {},
|
|
362
|
+
customReqOpts?: ICustomReqOpts,
|
|
363
|
+
): Promise<any> {
|
|
364
|
+
const response = await this.request(
|
|
365
|
+
action,
|
|
366
|
+
data,
|
|
367
|
+
{ ...options, onUploadProgress: data.onUploadProgress },
|
|
368
|
+
customReqOpts,
|
|
369
|
+
)
|
|
343
370
|
|
|
344
371
|
if (response.data.code && this.throwWhenRequestFail) {
|
|
345
372
|
throw new Error(JSON.stringify({
|
|
@@ -350,6 +377,43 @@ export class CloudbaseRequest implements ICloudbaseRequest {
|
|
|
350
377
|
|
|
351
378
|
return response.data
|
|
352
379
|
}
|
|
380
|
+
|
|
381
|
+
public async gateWay(options: IGateWayOptions, customReqOpts?: ICustomReqOpts) {
|
|
382
|
+
const { name, data, path = '', method, header = {} } = options
|
|
383
|
+
|
|
384
|
+
if (!name || !path) {
|
|
385
|
+
throw new Error(JSON.stringify({
|
|
386
|
+
code: ERRORS.INVALID_PARAMS,
|
|
387
|
+
msg: '[gateWay] invalid function name or path',
|
|
388
|
+
}),)
|
|
389
|
+
}
|
|
390
|
+
let jsonData
|
|
391
|
+
try {
|
|
392
|
+
jsonData = data ? JSON.stringify(data) : ''
|
|
393
|
+
} catch (e) {
|
|
394
|
+
throw new Error(JSON.stringify({
|
|
395
|
+
code: ERRORS.INVALID_PARAMS,
|
|
396
|
+
msg: '[gateWay] invalid data',
|
|
397
|
+
}),)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const requestId = utils.generateRequestId()
|
|
401
|
+
const { baseUrl, protocol } = getEndPointInfo(this.config.env, 'GATEWAY')
|
|
402
|
+
const endpoint = `${protocol}${baseUrl}/${path}/${name}`
|
|
403
|
+
const response = await this.fetch({
|
|
404
|
+
method: method || 'POST',
|
|
405
|
+
headers: {
|
|
406
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
407
|
+
'X-Request-Id': requestId,
|
|
408
|
+
...header,
|
|
409
|
+
},
|
|
410
|
+
body: jsonData,
|
|
411
|
+
url: endpoint,
|
|
412
|
+
customReqOpts,
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
return { requestId, ...response, data: await response.data }
|
|
416
|
+
}
|
|
353
417
|
}
|
|
354
418
|
|
|
355
419
|
const requestMap: KV<CloudbaseRequest> = {}
|