@cloudbase/app 2.21.2 → 2.21.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/src/index.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  KV,
9
9
  ICloudbasePlatformInfo,
10
10
  EndPointKey,
11
+ ICloudbaseApis,
11
12
  } from '@cloudbase/types'
12
13
  import { ICloudbaseAuth } from '@cloudbase/types/auth'
13
14
  import adapterForWxMp from '@cloudbase/adapter-wx_mp'
@@ -30,7 +31,7 @@ import {
30
31
  getSdkVersion,
31
32
  } from './constants/common'
32
33
  import { i18nProxy, LANGS } from './libs/lang'
33
- import { generateApis, generateCallApis } from './libs/callApis'
34
+ import { generateApis } from './libs/callApis'
34
35
  export { getBaseEndPoint } from './constants/common'
35
36
  export { LANGS } from './libs/lang'
36
37
  const { useAdapters, useDefaultAdapter } = adapters
@@ -88,7 +89,7 @@ class Cloudbase implements ICloudbase {
88
89
  return getRequestByEnvId(this.cloudbaseConfig.env)
89
90
  }
90
91
 
91
- get apis(): { [key: string]: typeof generateCallApis } {
92
+ get apis(): ICloudbaseApis {
92
93
  return generateApis.call(this)
93
94
  }
94
95
 
@@ -140,7 +141,15 @@ class Cloudbase implements ICloudbase {
140
141
  setGatewayEndPointWithEnv(env)
141
142
 
142
143
  const app = new Cloudbase(this.cloudbaseConfig)
143
- initRequest({ env, region: config.region || '', timeout, oauthClient, _fromApp: app, i18n, endPointMode: config.endPointMode })
144
+ initRequest({
145
+ env,
146
+ region: config.region || '',
147
+ timeout,
148
+ oauthClient,
149
+ _fromApp: app,
150
+ i18n,
151
+ endPointMode: config.endPointMode,
152
+ })
144
153
  app.requestClient = this.requestClient
145
154
  ;(this as any)?.fire?.('cloudbase_init', app)
146
155
  this.try2InitAuth(config, app)
@@ -34,7 +34,7 @@ export async function callApi(callApiOptions: ICallApiOptions, opts: KV<any>):
34
34
 
35
35
  const SUPPORT_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH', 'REQUEST']
36
36
 
37
- export function generateCallApis(apiName: string): { [apiName: string]: typeof callApi} {
37
+ export function generateCallApis(apiName: string): { [method: string]: typeof callApi} {
38
38
  return new Proxy({}, {
39
39
  get: (_, method) => {
40
40
  if (typeof method !== 'string') {
@@ -9,6 +9,7 @@ import {
9
9
  } from '@cloudbase/adapter-interface'
10
10
  import { utils, constants, langEvent } from '@cloudbase/utilities'
11
11
  import { EndPointKey, KV } from '@cloudbase/types'
12
+ import { ICustomReqOpts } from '@cloudbase/types/functions'
12
13
  import {
13
14
  IGetAccessTokenResult,
14
15
  ICloudbaseRequestConfig,
@@ -125,8 +126,8 @@ export class CloudbaseRequest implements ICloudbaseRequest {
125
126
  return { [this.config.i18n?.LANG_HEADER_KEY]: this.config.i18n?.lang }
126
127
  }
127
128
 
128
- public async post(options: IRequestOptions): Promise<ResponseObject> {
129
- const res = await this.reqClass.post({ ...options, headers: { ...options.headers, ...this.getDefaultHeaders() } })
129
+ public async post(options: IRequestOptions, customReqOpts?: ICustomReqOpts): Promise<ResponseObject> {
130
+ const res = await this.reqClass.post({ ...options, headers: { ...options.headers, ...this.getDefaultHeaders() }, customReqOpts })
130
131
  return res
131
132
  }
132
133
  public async upload(options: IUploadRequestOptions): Promise<ResponseObject> {
@@ -163,6 +164,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
163
164
  search?: string
164
165
  defaultQuery?: KV<any>
165
166
  },
167
+ customReqOpts?: ICustomReqOpts
166
168
  ): Promise<ResponseObject> {
167
169
  const tcbTraceKey = `x-tcb-trace_${this.config.env}`
168
170
  let contentType = 'application/x-www-form-urlencoded'
@@ -243,13 +245,22 @@ export class CloudbaseRequest implements ICloudbaseRequest {
243
245
  ...formatQuery,
244
246
  })
245
247
 
246
- const { baseUrl: BASE_URL, protocol: PROTOCOL } = getEndPointInfo(this.config.env, this.config.endPointMode || 'CLOUD_API')
248
+ const endPointMode = this.config.endPointMode || 'CLOUD_API'
249
+
250
+ const url = getEndPointInfo(this.config.env, endPointMode)
251
+ let BASE_URL = url.baseUrl
252
+ const PROTOCOL = url.protocol
253
+
254
+ if ((action === 'functions.invokeFunction' || /^(storage|database)\./.test(action)) && endPointMode === 'GATEWAY') {
255
+ BASE_URL = `${BASE_URL.match(/\/\/([^/?#]*)/)[0]}/web`
256
+ }
257
+
247
258
  // 生成请求 url
248
259
  let newUrl
249
260
  if (options.pathname) {
250
261
  newUrl = formatUrl(
251
262
  PROTOCOL,
252
- `${getBaseEndPoint(this.config.env, this.config.endPointMode || 'CLOUD_API')?.replace(/^https?:/, '')}/${options.pathname}`,
263
+ `${getBaseEndPoint(this.config.env, endPointMode)?.replace(/^https?:/, '')}/${options.pathname}`,
253
264
  formatQuery,
254
265
  )
255
266
  } else {
@@ -264,7 +275,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
264
275
  url: newUrl,
265
276
  data: payload,
266
277
  ...opts,
267
- })
278
+ }, customReqOpts)
268
279
 
269
280
  // 保存 trace header
270
281
  const resTraceHeader = res.header?.['x-tcb-trace']
@@ -327,8 +338,8 @@ export class CloudbaseRequest implements ICloudbaseRequest {
327
338
  }
328
339
  }
329
340
 
330
- public async send(action: string, data: KV<any> = {}, options: KV<any> = {}): Promise<any> {
331
- const response = await this.request(action, data, { ...options, onUploadProgress: data.onUploadProgress })
341
+ public async send(action: string, data: KV<any> = {}, options: KV<any> = {}, customReqOpts?: ICustomReqOpts): Promise<any> {
342
+ const response = await this.request(action, data, { ...options, onUploadProgress: data.onUploadProgress }, customReqOpts)
332
343
 
333
344
  if (response.data.code && this.throwWhenRequestFail) {
334
345
  throw new Error(JSON.stringify({