@cloudbase/app 2.21.3 → 2.21.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/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'
@@ -249,7 +251,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
249
251
  let BASE_URL = url.baseUrl
250
252
  const PROTOCOL = url.protocol
251
253
 
252
- if (action === 'functions.invokeFunction' && endPointMode === 'GATEWAY') {
254
+ if ((action === 'functions.invokeFunction' || /^(storage|database)\./.test(action)) && endPointMode === 'GATEWAY') {
253
255
  BASE_URL = `${BASE_URL.match(/\/\/([^/?#]*)/)[0]}/web`
254
256
  }
255
257
 
@@ -273,7 +275,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
273
275
  url: newUrl,
274
276
  data: payload,
275
277
  ...opts,
276
- })
278
+ }, customReqOpts)
277
279
 
278
280
  // 保存 trace header
279
281
  const resTraceHeader = res.header?.['x-tcb-trace']
@@ -336,8 +338,8 @@ export class CloudbaseRequest implements ICloudbaseRequest {
336
338
  }
337
339
  }
338
340
 
339
- public async send(action: string, data: KV<any> = {}, options: KV<any> = {}): Promise<any> {
340
- 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)
341
343
 
342
344
  if (response.data.code && this.throwWhenRequestFail) {
343
345
  throw new Error(JSON.stringify({