@cloudbase/app 2.21.4 → 2.21.6
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 +3 -1
- package/dist/cjs/constants/common.js +13 -7
- package/dist/cjs/index.js +3 -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 +3 -1
- package/dist/esm/constants/common.js +10 -4
- package/dist/esm/index.js +4 -3
- 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 +8 -3
- package/src/index.ts +3 -1
- package/src/libs/request.ts +75 -11
package/src/constants/common.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EndPointKey } from '@cloudbase/types'
|
|
2
2
|
import { constants } from '@cloudbase/utilities'
|
|
3
|
+
import { LANGS } from '../libs/lang'
|
|
3
4
|
|
|
4
5
|
// @ts-ignore
|
|
5
6
|
const { setSdkName: setUtilitiesSdkName, setProtocol: setUtilitiesProtocol } = constants
|
|
@@ -41,7 +42,7 @@ const END_POINT_INFO_ARR: Array<EndPointInfo> = []
|
|
|
41
42
|
/** 用来查找 endPoint 的字段 */
|
|
42
43
|
const END_POINT_INFO_SEARCH_KEYS = ['env', 'endPointKey', 'region']
|
|
43
44
|
|
|
44
|
-
const DEFAULT_PROTOCOL = 'https:'
|
|
45
|
+
export const DEFAULT_PROTOCOL = 'https:'
|
|
45
46
|
|
|
46
47
|
function findMatchedInfo(info: EndPointInfo) {
|
|
47
48
|
return END_POINT_INFO_ARR.find(targetInfo => END_POINT_INFO_SEARCH_KEYS.filter(searchKey => info[searchKey] != null).every(searchKey => targetInfo[searchKey] === info[searchKey],),)
|
|
@@ -76,8 +77,12 @@ export interface ISetEndPointWithKey {
|
|
|
76
77
|
protocol?: 'http' | 'https'
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
export function setGatewayEndPointWithEnv(env: string, protocol?: 'http' | 'https') {
|
|
80
|
-
|
|
80
|
+
export function setGatewayEndPointWithEnv(env: string, protocol?: 'http' | 'https', lang?: LANGS) {
|
|
81
|
+
const url = {
|
|
82
|
+
[LANGS.ZH]: `//${env}.api.tcloudbasegateway.com/v1`,
|
|
83
|
+
[LANGS.EN]: `//${env}.api.intl.tcloudbasegateway.com/v1`,
|
|
84
|
+
}
|
|
85
|
+
setEndPointInfo({ endPointKey: 'GATEWAY', env, baseUrl: url[lang] || url[LANGS.ZH], protocol })
|
|
81
86
|
}
|
|
82
87
|
export function setRegionLevelEndpoint(env: string, region: string, protocol?: 'http' | 'https') {
|
|
83
88
|
const baseUrl = `//${env}.${region || 'ap-shanghai'}.tcb-api.tencentcloudapi.com/web`
|
package/src/index.ts
CHANGED
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
setEndPointInfo,
|
|
30
30
|
getEndPointInfo,
|
|
31
31
|
getSdkVersion,
|
|
32
|
+
DEFAULT_PROTOCOL,
|
|
32
33
|
} from './constants/common'
|
|
33
34
|
import { i18nProxy, LANGS } from './libs/lang'
|
|
34
35
|
import { generateApis } from './libs/callApis'
|
|
@@ -138,7 +139,7 @@ class Cloudbase implements ICloudbase {
|
|
|
138
139
|
initCache({ env, persistence, debug, platformInfo: this.platform })
|
|
139
140
|
|
|
140
141
|
setRegionLevelEndpoint(env, config.region || '')
|
|
141
|
-
setGatewayEndPointWithEnv(env)
|
|
142
|
+
setGatewayEndPointWithEnv(env, DEFAULT_PROTOCOL as any, this.cloudbaseConfig.i18n.lang)
|
|
142
143
|
|
|
143
144
|
const app = new Cloudbase(this.cloudbaseConfig)
|
|
144
145
|
initRequest({
|
|
@@ -169,6 +170,7 @@ class Cloudbase implements ICloudbase {
|
|
|
169
170
|
if (!lang || lang === this.cloudbaseConfig.i18n?.lang) return
|
|
170
171
|
|
|
171
172
|
this.cloudbaseConfig.i18n.lang = lang
|
|
173
|
+
setGatewayEndPointWithEnv(this.cloudbaseConfig.env, 'https', lang)
|
|
172
174
|
}
|
|
173
175
|
|
|
174
176
|
public registerExtension(ext: ICloudbaseExtension) {
|
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> = {}
|