@cloudbase/app 2.21.9 → 2.22.0

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.21.9",
3
+ "version": "2.22.0",
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.21.9",
35
- "@cloudbase/utilities": "2.21.9"
34
+ "@cloudbase/types": "2.22.0",
35
+ "@cloudbase/utilities": "2.22.0"
36
36
  },
37
- "gitHead": "c671de51da2e4fa309c35277dc36b28a724b75ff"
37
+ "gitHead": "ec37fade5ab4e50bda976e37512aa08fbc02fca3"
38
38
  }
package/src/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import cloudbase from '@cloudbase/app';
2
2
  import { ICloudbase } from '@cloudbase/types';
3
3
  declare global {
4
- interface Window {
5
- cloudbase: ICloudbase;
6
- }
4
+ interface Window {
5
+ cloudbase: ICloudbase;
6
+ }
7
7
  }
8
+ // @ts-ignore
8
9
  export = cloudbase;
9
10
  export default cloudbase;
package/src/index.ts CHANGED
@@ -11,9 +11,8 @@ import {
11
11
  ICloudbaseApis,
12
12
  } from '@cloudbase/types'
13
13
  import { ICloudbaseAuth } from '@cloudbase/types/auth'
14
- import adapterForWxMp from '@cloudbase/adapter-wx_mp'
15
14
  import { registerComponent, registerHook } from './libs/component'
16
- import { Platform } from './libs/adapter'
15
+ import { getWxDefaultAdapter, Platform } from './libs/adapter'
17
16
  import { ICloudbaseComponent, ICloudbaseHook } from '@cloudbase/types/component'
18
17
  import { ICloudbaseCache } from '@cloudbase/types/cache'
19
18
  import { initCache, getCacheByEnvId, getLocalCache } from './libs/cache'
@@ -305,7 +304,13 @@ class Cloudbase implements ICloudbase {
305
304
  }
306
305
  }
307
306
 
307
+ // 类型导出
308
+ export type { Cloudbase }
309
+
310
+ // 值导出
308
311
  export const cloudbase: ICloudbase = new Cloudbase()
309
- cloudbase.useAdapters(adapterForWxMp)
310
312
 
313
+ cloudbase.useAdapters(getWxDefaultAdapter())
314
+
315
+ // 默认导出实例
311
316
  export default cloudbase
@@ -1,3 +1,130 @@
1
1
  import { ICloudbasePlatformInfo } from '@cloudbase/types'
2
+ import adapterForWxMp, { WxRequest, WxMpWebSocket, wxMpStorage, parseQueryString } from '@cloudbase/adapter-wx_mp'
3
+ import { IUploadRequestOptions, StorageType } from '@cloudbase/adapter-interface'
4
+
5
+ declare const wx: any
6
+ declare const App: any
7
+ declare const getApp: any
2
8
 
3
9
  export const Platform: ICloudbasePlatformInfo = {}
10
+
11
+ export const getWxDefaultAdapter = () => {
12
+ WxRequest.prototype.upload = function (options: IUploadRequestOptions) {
13
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
14
+ const self = this
15
+ return new Promise((resolve) => {
16
+ const { url, file, data, headers } = options
17
+ const fs = wx.getFileSystemManager() // 读取文件 二进制内容
18
+ const task = wx.request({
19
+ url,
20
+ method: options.method,
21
+ header: {
22
+ 'content-type': ' ', // 小程序 content-type 默认为 application/json, 这里一定要强制为 空, 否则签名错误
23
+ ...headers,
24
+ },
25
+ data: fs.readFileSync(file), // 将二进制文件转为字符串直接赋值到 request payload, 不要以 form 的方式传输
26
+ timeout: this._timeout,
27
+ success(res: { statusCode: number; data: unknown }) {
28
+ const result = {
29
+ statusCode: res.statusCode,
30
+ data: res.data || {},
31
+ }
32
+ if (res.statusCode === 200 && data?.success_action_status) {
33
+ result.statusCode = parseInt(data.success_action_status, 10)
34
+ }
35
+ resolve(result)
36
+ },
37
+ fail(err: unknown) {
38
+ resolve(err)
39
+ },
40
+ complete(err: { errMsg: string }) {
41
+ if (!err?.errMsg) {
42
+ return
43
+ }
44
+ if (!self._timeout || self._restrictedMethods.indexOf('upload') === -1) {
45
+ return
46
+ }
47
+ const { errMsg } = err
48
+ if (errMsg === 'request:fail timeout') {
49
+ console.warn(self._timeoutMsg)
50
+ try {
51
+ task.abort()
52
+ } catch (e) {}
53
+ }
54
+ },
55
+ })
56
+ })
57
+ }
58
+
59
+ function isPlugin() {
60
+ return (
61
+ typeof App === 'undefined'
62
+ && typeof getApp === 'undefined'
63
+ && !wx.onAppHide
64
+ && !wx.offAppHide
65
+ && !wx.onAppShow
66
+ && !wx.offAppShow
67
+ )
68
+ }
69
+ adapterForWxMp.genAdapter = function genAdapter(options) {
70
+ const adapter = {
71
+ root: { globalThis: {} },
72
+ reqClass: WxRequest,
73
+ wsClass: WxMpWebSocket,
74
+ captchaOptions: {
75
+ openURIWithCallback: (_url: string) => {
76
+ const { EventBus } = options
77
+ let queryObj: Record<string, string> = {}
78
+ let url = _url
79
+ console.log('openURIWithCallback', _url)
80
+ const matched = _url.match(/^(data:.*?)(\?[^#\s]*)?$/)
81
+ if (matched) {
82
+ url = matched[1]
83
+ console.log('openURIWithCallback url', url)
84
+ const search = matched[2]
85
+ if (search) {
86
+ queryObj = parseQueryString(search)
87
+ }
88
+ }
89
+ console.log('openURIWithCallback queryObj', queryObj)
90
+ const { token, ...restQueryObj } = queryObj
91
+ if (/^data:/.test(url) && !token) {
92
+ // 如果是 data: 开头的 URL 且没有 token,则直接返回
93
+ return Promise.reject({
94
+ error: 'invalid_argument',
95
+ error_description: `invalie captcha data: ${_url}`,
96
+ })
97
+ }
98
+ if (!token) {
99
+ return Promise.reject({
100
+ error: 'unimplemented',
101
+ error_description: 'need to impl captcha data',
102
+ })
103
+ }
104
+ return new Promise((resolve) => {
105
+ console.log('wait for captcha...')
106
+ EventBus.$emit('CAPTCHA_DATA_CHANGE', { ...restQueryObj, token, url })
107
+
108
+ // 监听事件总线,等待验证码数据变化
109
+ EventBus.$once('RESOLVE_CAPTCHA_DATA', (res: { captcha_token: string; expires_in: number }) => {
110
+ resolve(res)
111
+ })
112
+ })
113
+ },
114
+ },
115
+ localStorage: wxMpStorage,
116
+ primaryStorage: StorageType.local,
117
+ getAppSign() {
118
+ const info = wx.getAccountInfoSync()
119
+ if (isPlugin()) {
120
+ // 插件环境返回插件appid
121
+ return info && info.plugin ? info.plugin.appId : ''
122
+ }
123
+ return info && info.miniProgram ? info.miniProgram.appId : ''
124
+ },
125
+ }
126
+ return adapter
127
+ }
128
+
129
+ return adapterForWxMp
130
+ }