@cloudbase/app 2.22.1 → 2.22.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/app",
3
- "version": "2.22.1",
3
+ "version": "2.22.3",
4
4
  "description": "cloudbase javascript sdk core",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -31,8 +31,8 @@
31
31
  "dependencies": {
32
32
  "@cloudbase/adapter-interface": "^0.7.1",
33
33
  "@cloudbase/adapter-wx_mp": "^1.2.1",
34
- "@cloudbase/types": "2.22.1",
35
- "@cloudbase/utilities": "2.22.1"
34
+ "@cloudbase/types": "2.22.3",
35
+ "@cloudbase/utilities": "2.22.3"
36
36
  },
37
- "gitHead": "46ebb16422f02fb8cef279ad16d590ad6115abe1"
37
+ "gitHead": "2e0a972561cc2505e9a2a1cbe92804b40de1ee7f"
38
38
  }
@@ -49,6 +49,7 @@ const END_POINT_INFO_SEARCH_KEYS = ['env', 'endPointKey', 'region']
49
49
  export const DEFAULT_PROTOCOL: 'http:' | 'https:' = 'https:'
50
50
 
51
51
  function findMatchedInfo(info: EndPointInfo) {
52
+ // eslint-disable-next-line max-len, eqeqeq
52
53
  return END_POINT_INFO_ARR.find(targetInfo => END_POINT_INFO_SEARCH_KEYS.filter(searchKey => info[searchKey] != null).every(searchKey => targetInfo[searchKey] === info[searchKey],),)
53
54
  }
54
55
 
@@ -59,9 +60,11 @@ export function setEndPointInfo(newInfo: EndPointInfo) {
59
60
 
60
61
  const endPointInfo = findMatchedInfo(newInfo)
61
62
  if (endPointInfo) {
63
+ // eslint-disable-next-line eqeqeq
62
64
  if (newInfo.baseUrl != null) {
63
65
  endPointInfo.baseUrl = newInfo.baseUrl
64
66
  }
67
+ // eslint-disable-next-line eqeqeq
65
68
  if (newInfo.protocol != null) {
66
69
  endPointInfo.protocol = newInfo.protocol
67
70
  }
package/src/index.ts CHANGED
@@ -247,6 +247,7 @@ class Cloudbase implements ICloudbase {
247
247
  let queryObj: any = {}
248
248
  const matched = url.match(/^(data:.*?)(\?[^#\s]*)?$/)
249
249
  if (matched) {
250
+ // eslint-disable-next-line prefer-destructuring
250
251
  url = matched[1]
251
252
  const search = matched[2]
252
253
  if (search) {
@@ -73,12 +73,14 @@ export const getWxDefaultAdapter = () => {
73
73
  wsClass: WxMpWebSocket,
74
74
  captchaOptions: {
75
75
  openURIWithCallback: (_url: string) => {
76
+ // eslint-disable-next-line @typescript-eslint/naming-convention
76
77
  const { EventBus } = options
77
78
  let queryObj: Record<string, string> = {}
78
79
  let url = _url
79
80
  console.log('openURIWithCallback', _url)
80
81
  const matched = _url.match(/^(data:.*?)(\?[^#\s]*)?$/)
81
82
  if (matched) {
83
+ // eslint-disable-next-line prefer-destructuring
82
84
  url = matched[1]
83
85
  console.log('openURIWithCallback url', url)
84
86
  const search = matched[2]
@@ -2,7 +2,7 @@ import { ERRORS } from '@cloudbase/utilities/dist/cjs/constants'
2
2
  import { ICallApiOptions, KV } from '@cloudbase/types'
3
3
  import { ResponseObject } from '@cloudbase/adapter-interface'
4
4
 
5
- export async function callApi(callApiOptions: ICallApiOptions, opts: KV<any>): Promise<ResponseObject['data']> {
5
+ export async function callApi(callApiOptions: ICallApiOptions, opts?: KV<any>): Promise<ResponseObject['data']> {
6
6
  const { name, body, path = '', method = 'POST', headers = {}, token } = callApiOptions || {}
7
7
 
8
8
  if (!name) {
@@ -33,9 +33,10 @@ export async function callApi(callApiOptions: ICallApiOptions, opts: KV<any>):
33
33
  }
34
34
 
35
35
  const SUPPORT_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH', 'REQUEST']
36
+ declare type MethodType = 'request' | 'post' | 'get' | 'head' | 'patch' | 'delete' | 'put'
36
37
 
37
- export function generateCallApis(apiName: string): { [method: string]: typeof callApi} {
38
- return new Proxy({}, {
38
+ export function generateCallApis(apiName: string): { [method in MethodType]: typeof callApi } {
39
+ return new Proxy({} as any, {
39
40
  get: (_, method) => {
40
41
  if (typeof method !== 'string') {
41
42
  throw new Error('[apis] method must be string')
@@ -47,21 +48,28 @@ export function generateCallApis(apiName: string): { [method: string]: typeof ca
47
48
  throw new Error(`[apis] invalid method: ${method}`)
48
49
  }
49
50
 
50
- return async (callApiOptions: ICallApiOptions, opts: KV<any>) => await callApi.call(this, {
51
- name: apiName,
52
- method: (upMethod === 'REQUEST' ? callApiOptions.method : upMethod) || 'POST',
53
- ...callApiOptions,
54
- }, opts)
51
+ return async (callApiOptions: ICallApiOptions, opts: KV<any>) => await callApi.call(
52
+ this,
53
+ {
54
+ name: apiName,
55
+ method: (upMethod === 'REQUEST' ? callApiOptions.method : upMethod) || 'POST',
56
+ ...callApiOptions,
57
+ },
58
+ opts,
59
+ )
55
60
  },
56
61
  })
57
62
  }
58
63
 
59
- export function generateApis(): { [apiName: string]: typeof generateCallApis} {
60
- return new Proxy({}, {
61
- get: (_, apiName) => {
62
- if (typeof apiName === 'string') {
63
- return generateCallApis.call(this, apiName)
64
- }
64
+ export function generateApis(): { [apiName: string]: { [method in MethodType]: typeof callApi } } {
65
+ return new Proxy(
66
+ {},
67
+ {
68
+ get: (_, apiName) => {
69
+ if (typeof apiName === 'string') {
70
+ return generateCallApis.call(this, apiName)
71
+ }
72
+ },
65
73
  },
66
- })
74
+ )
67
75
  }
@@ -120,17 +120,39 @@ export class CloudbaseRequest implements ICloudbaseRequest {
120
120
  this.reqClass = new Platform.adapter.reqClass(reqConfig)
121
121
  this.throwWhenRequestFail = config.throw || false
122
122
  this.localCache = getLocalCache(this.config.env)
123
- bindHooks(this.reqClass, 'post', [beforeEach])
124
- bindHooks(this.reqClass, 'upload', [beforeEach])
125
- bindHooks(this.reqClass, 'download', [beforeEach])
123
+
124
+ if (this.config.endPointMode !== 'GATEWAY') {
125
+ bindHooks(this.reqClass, 'post', [beforeEach])
126
+ bindHooks(this.reqClass, 'upload', [beforeEach])
127
+ bindHooks(this.reqClass, 'download', [beforeEach])
128
+ }
126
129
 
127
130
  langEvent.bus.on(langEvent.LANG_CHANGE_EVENT, (params) => {
128
131
  this.config.i18n = params.data?.i18n || this.config.i18n
129
132
  })
130
133
  }
131
134
 
135
+ public async getAccessToken(token = null) {
136
+ // eslint-disable-next-line eqeqeq
137
+ if (token != null) {
138
+ return token
139
+ }
140
+ const app = this.config._fromApp
141
+
142
+ if (!app.oauthInstance) {
143
+ throw new Error('you can\'t request without auth')
144
+ }
145
+
146
+ const { oauthInstance } = app
147
+ const oauthClient = oauthInstance.oauth2client
148
+ return (await this.getOauthAccessTokenV2(oauthClient)).accessToken
149
+ }
150
+
132
151
  public getDefaultHeaders() {
133
- return { [this.config.i18n?.LANG_HEADER_KEY]: this.config.i18n?.lang }
152
+ return {
153
+ [this.config.i18n?.LANG_HEADER_KEY]: this.config.i18n?.lang,
154
+ 'X-SDK-Version': `@cloudbase/js-sdk/${getSdkVersion()}`,
155
+ }
134
156
  }
135
157
 
136
158
  public async post(options: IRequestOptions, customReqOpts?: ICustomReqOpts): Promise<ResponseObject> {
@@ -265,8 +287,12 @@ export class CloudbaseRequest implements ICloudbaseRequest {
265
287
  let BASE_URL = url.baseUrl
266
288
  const PROTOCOL = url.protocol
267
289
 
268
- if ((action === 'functions.invokeFunction' || /^(storage|database)\./.test(action)) && endPointMode === 'GATEWAY') {
269
- BASE_URL = `${BASE_URL.match(/\/\/([^/?#]*)/)[0]}/web`
290
+ if (endPointMode === 'GATEWAY') {
291
+ // opts.headers.Authorization = `Bearer ${await this.getAccessToken()}`
292
+
293
+ if (action === 'functions.invokeFunction' || /^(storage|database)\./.test(action)) {
294
+ BASE_URL = `${BASE_URL.match(/\/\/([^/?#]*)/)[0]}/web`
295
+ }
270
296
  }
271
297
 
272
298
  // 生成请求 url
@@ -309,20 +335,6 @@ export class CloudbaseRequest implements ICloudbaseRequest {
309
335
 
310
336
  public async fetch(options: IFetchOptions & { token?: string; customReqOpts?: ICustomReqOpts },): Promise<ResponseObject> {
311
337
  const { token, headers = {}, ...restOptions } = options
312
- const getAccessToken = async () => {
313
- if (token != null) {
314
- return token
315
- }
316
- const app = this.config._fromApp
317
-
318
- if (!app.oauthInstance) {
319
- throw new Error('you can\'t request without auth')
320
- }
321
-
322
- const { oauthInstance } = app
323
- const oauthClient = oauthInstance.oauth2client
324
- return (await this.getOauthAccessTokenV2(oauthClient)).accessToken
325
- }
326
338
 
327
339
  const doFetch = async () => this.reqClass.fetch({
328
340
  headers: {
@@ -330,7 +342,7 @@ export class CloudbaseRequest implements ICloudbaseRequest {
330
342
  // 'X-Request-Id': `${utils.generateRequestId()}`,
331
343
  // 'X-Request-Timestamp': `${Date.now()}`,
332
344
  'X-SDK-Version': `@cloudbase/js-sdk/${getSdkVersion()}`,
333
- Authorization: `Bearer ${await getAccessToken()}`,
345
+ Authorization: `Bearer ${await this.getAccessToken(token)}`,
334
346
  ...this.getDefaultHeaders(),
335
347
  ...headers,
336
348
  },