@mpxjs/api-proxy 2.10.17 → 2.10.18

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/@types/index.d.ts CHANGED
@@ -45,9 +45,36 @@ export const offError: WechatMiniprogram.Wx['offError']
45
45
  export const createInnerAudioContext: WechatMiniprogram.Wx['createInnerAudioContext']
46
46
  export const base64ToArrayBuffer: WechatMiniprogram.Wx['base64ToArrayBuffer']
47
47
  export const arrayBufferToBase64: WechatMiniprogram.Wx['arrayBufferToBase64']
48
+ export const openBluetoothAdapter: WechatMiniprogram.Wx['openBluetoothAdapter']
49
+ export const closeBluetoothAdapter: WechatMiniprogram.Wx['closeBluetoothAdapter']
50
+ export const startBluetoothDevicesDiscovery: WechatMiniprogram.Wx['startBluetoothDevicesDiscovery']
51
+ export const stopBluetoothDevicesDiscovery: WechatMiniprogram.Wx['stopBluetoothDevicesDiscovery']
52
+ export const onBluetoothDeviceFound: WechatMiniprogram.Wx['onBluetoothDeviceFound']
53
+ export const offBluetoothDeviceFound: WechatMiniprogram.Wx['offBluetoothDeviceFound']
54
+ export const getConnectedBluetoothDevices: WechatMiniprogram.Wx['getConnectedBluetoothDevices']
55
+ export const getBluetoothAdapterState: WechatMiniprogram.Wx['getBluetoothAdapterState']
56
+ export const onBluetoothAdapterStateChange: WechatMiniprogram.Wx['onBluetoothAdapterStateChange']
57
+ export const offBluetoothAdapterStateChange: WechatMiniprogram.Wx['offBluetoothAdapterStateChange']
58
+ export const getBluetoothDevices: WechatMiniprogram.Wx['getBluetoothDevices']
59
+ export const writeBLECharacteristicValue: WechatMiniprogram.Wx['writeBLECharacteristicValue']
60
+ export const readBLECharacteristicValue: WechatMiniprogram.Wx['readBLECharacteristicValue']
61
+ export const notifyBLECharacteristicValueChange: WechatMiniprogram.Wx['notifyBLECharacteristicValueChange']
62
+ export const onBLECharacteristicValueChange: WechatMiniprogram.Wx['onBLECharacteristicValueChange']
63
+ export const offBLECharacteristicValueChange: WechatMiniprogram.Wx['offBLECharacteristicValueChange']
64
+ export const setBLEMTU: WechatMiniprogram.Wx['setBLEMTU']
65
+ export const getBLEDeviceRSSI: WechatMiniprogram.Wx['getBLEDeviceRSSI']
66
+ export const getBLEDeviceServices: WechatMiniprogram.Wx['getBLEDeviceServices']
67
+ export const getBLEDeviceCharacteristics: WechatMiniprogram.Wx['getBLEDeviceCharacteristics']
48
68
  export const closeBLEConnection: WechatMiniprogram.Wx['closeBLEConnection']
49
69
  export const createBLEConnection: WechatMiniprogram.Wx['createBLEConnection']
50
70
  export const onBLEConnectionStateChange: WechatMiniprogram.Wx['onBLEConnectionStateChange']
71
+ export const offBLEConnectionStateChange: WechatMiniprogram.Wx['offBLEConnectionStateChange']
72
+ export const startWifi: WechatMiniprogram.Wx['startWifi']
73
+ export const stopWifi: WechatMiniprogram.Wx['stopWifi']
74
+ export const getWifiList: WechatMiniprogram.Wx['getWifiList']
75
+ export const onGetWifiList: WechatMiniprogram.Wx['onGetWifiList']
76
+ export const offGetWifiList: WechatMiniprogram.Wx['offGetWifiList']
77
+ export const getConnectedWifi: WechatMiniprogram.Wx['getConnectedWifi']
51
78
  export const createCanvasContext: WechatMiniprogram.Wx['createCanvasContext']
52
79
  export const canvasToTempFilePath: WechatMiniprogram.Wx['canvasToTempFilePath']
53
80
  export const canvasGetImageData: WechatMiniprogram.Wx['canvasGetImageData']
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/api-proxy",
3
- "version": "2.10.17",
3
+ "version": "2.10.18",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -37,16 +37,18 @@
37
37
  },
38
38
  "homepage": "https://github.com/didi/mpx#readme",
39
39
  "dependencies": {
40
- "@mpxjs/utils": "^2.10.17",
40
+ "@mpxjs/utils": "^2.10.18",
41
41
  "axios": "^1.7.3"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@react-native-async-storage/async-storage": "*",
45
45
  "@react-native-community/netinfo": "*",
46
+ "react-native-ble-manager": "*",
46
47
  "react-native-device-info": "*",
47
48
  "react-native-get-location": "*",
48
49
  "react-native-haptic-feedback": "*",
49
- "react-native-safe-area-context": "*"
50
+ "react-native-safe-area-context": "*",
51
+ "react-native-wifi-reborn": "*"
50
52
  },
51
53
  "peerDependenciesMeta": {
52
54
  "@react-native-async-storage/async-storage": {
@@ -66,7 +68,13 @@
66
68
  },
67
69
  "react-native-haptic-feedback": {
68
70
  "optional": true
71
+ },
72
+ "react-native-ble-manager": {
73
+ "optional": true
74
+ },
75
+ "react-native-wifi-reborn": {
76
+ "optional": true
69
77
  }
70
78
  },
71
- "gitHead": "33fbd0bb8d4b83555239a8931fabb23022af0866"
79
+ "gitHead": "8ce11a81959cb4b807d683b14c2d0b72c3991da3"
72
80
  }
@@ -1 +1,2 @@
1
1
  export * from './index.web'
2
+ export { canIUse } from './rnCanIUse'
@@ -1,10 +1,7 @@
1
1
  import { fromByteArray, toByteArray } from './base64'
2
2
 
3
3
  function base64ToArrayBuffer (base64) {
4
- if (__mpx_mode__ === 'web') {
5
- return toByteArray(base64)?.buffer
6
- }
7
- return toByteArray(base64)
4
+ return toByteArray(base64)?.buffer // 判断了一下,RN下也是要.buffer才是buffer类型
8
5
  }
9
6
 
10
7
  function arrayBufferToBase64 (arrayBuffer) {
@@ -0,0 +1,93 @@
1
+ import { SUPPORTED_APIS as API_LIST, SUPPORTED_OBJECTS as OBJECT_CONFIG } from './rnCanIUseConfig'
2
+
3
+ let SUPPORTED_APIS = null
4
+ let SUPPORTED_OBJECTS = null
5
+ let OBJECT_METHODS = null
6
+
7
+ /**
8
+ * 初始化支持的 API 列表
9
+ *
10
+ * 使用静态配置而不是动态导入,避免加载原生模块
11
+ * 这样 canIUse 只做判断,不触发任何模块的实际加载
12
+ */
13
+ function initSupportedApis () {
14
+ if (SUPPORTED_APIS !== null) {
15
+ return
16
+ }
17
+
18
+ // 从静态配置中获取 API 列表
19
+ SUPPORTED_APIS = new Set(API_LIST)
20
+
21
+ // 从静态配置中获取对象和方法
22
+ SUPPORTED_OBJECTS = new Set(Object.keys(OBJECT_CONFIG))
23
+ OBJECT_METHODS = OBJECT_CONFIG
24
+ }
25
+
26
+ /**
27
+ * 判断小程序的API、回调、参数、组件等是否在当前版本可用
28
+ * @param {string} schema - 使用 ${API}.${method}.${param}.${option} 或者 ${component}.${attribute}.${option} 方式来调用
29
+ * @returns {boolean} 是否支持
30
+ */
31
+ function canIUse (schema) {
32
+ // 延迟初始化,确保在首次调用时才加载 API 列表
33
+ if (SUPPORTED_APIS === null) {
34
+ initSupportedApis()
35
+ }
36
+
37
+ if (typeof schema !== 'string') {
38
+ return false
39
+ }
40
+
41
+ // 检查是否包含 ${} 语法,这种语法是无效的,应该返回 false
42
+ if (schema.includes('${') || schema.includes('}')) {
43
+ return false
44
+ }
45
+
46
+ const parts = schema.split('.')
47
+ const [first, second] = parts
48
+
49
+ // 情况1: 只有一个部分,检查 API 或对象
50
+ if (parts.length === 1) {
51
+ return SUPPORTED_APIS.has(first) || SUPPORTED_OBJECTS.has(first)
52
+ }
53
+
54
+ // 情况2: 两个部分 - API.method 或 Object.property
55
+ if (parts.length === 2) {
56
+ // 检查是否是支持的 API
57
+ if (SUPPORTED_APIS.has(first)) {
58
+ return true // 在 RN 环境下,如果 API 支持,默认其方法也支持
59
+ }
60
+ // 检查对象的方法/属性
61
+ if (SUPPORTED_OBJECTS.has(first)) {
62
+ return checkObjectMethod(first, second)
63
+ }
64
+ return false
65
+ }
66
+
67
+ // 情况3: 三个或更多部分 - API.method.param 等
68
+ if (parts.length >= 3) {
69
+ // 检查基础 API 是否支持
70
+ if (SUPPORTED_APIS.has(first)) {
71
+ return true // 简化处理,如果 API 支持则认为其参数也支持
72
+ }
73
+ return false
74
+ }
75
+
76
+ return false
77
+ }
78
+
79
+ /**
80
+ * 检查对象的方法是否支持
81
+ */
82
+ function checkObjectMethod (objectName, methodName) {
83
+ const methods = OBJECT_METHODS[objectName]
84
+ if (!methods) {
85
+ return false
86
+ }
87
+
88
+ return methods.includes(methodName)
89
+ }
90
+
91
+ export {
92
+ canIUse
93
+ }
@@ -0,0 +1,231 @@
1
+ /**
2
+ * React Native 平台支持的 API 和类配置
3
+ *
4
+ * 此文件用于 canIUse 功能,只声明支持的 API,不导入任何实现模块
5
+ * 避免在判断 API 可用性时触发原生模块的加载
6
+ * 对应 platform/index.js 中的所有导出,且在 RN 平台有实际实现
7
+ */
8
+ export const SUPPORTED_APIS = [
9
+ // action-sheet
10
+ 'showActionSheet',
11
+
12
+ // animation
13
+ 'createAnimation',
14
+
15
+ // app
16
+ 'onAppShow',
17
+ 'onAppHide',
18
+ 'offAppShow',
19
+ 'offAppHide',
20
+ 'onError',
21
+ 'offError',
22
+
23
+ // base
24
+ 'base64ToArrayBuffer',
25
+ 'arrayBufferToBase64',
26
+
27
+ // create-intersection-observer
28
+ 'createIntersectionObserver',
29
+
30
+ // create-selector-query
31
+ 'createSelectorQuery',
32
+
33
+ // device/network
34
+ 'getNetworkType',
35
+ 'onNetworkStatusChange',
36
+ 'offNetworkStatusChange',
37
+
38
+ // image
39
+ 'previewImage',
40
+ 'compressImage',
41
+ 'getImageInfo',
42
+ 'chooseMedia',
43
+
44
+ // keyboard
45
+ 'onKeyboardHeightChange',
46
+ 'offKeyboardHeightChange',
47
+ 'hideKeyboard',
48
+
49
+ // location
50
+ 'getLocation',
51
+ 'openLocation',
52
+ 'chooseLocation',
53
+ 'onLocationChange',
54
+ 'offLocationChange',
55
+ 'startLocationUpdate',
56
+ 'stopLocationUpdate',
57
+
58
+ // make-phone-call
59
+ 'makePhoneCall',
60
+
61
+ // modal
62
+ 'showModal',
63
+
64
+ // next-tick
65
+ 'nextTick',
66
+
67
+ // request
68
+ 'request',
69
+
70
+ // route
71
+ 'redirectTo',
72
+ 'navigateTo',
73
+ 'navigateBack',
74
+ 'reLaunch',
75
+ 'switchTab',
76
+
77
+ // set-navigation-bar
78
+ 'setNavigationBarTitle',
79
+ 'setNavigationBarColor',
80
+
81
+ // socket
82
+ 'connectSocket',
83
+
84
+ // storage
85
+ 'setStorage',
86
+ 'setStorageSync',
87
+ 'getStorage',
88
+ 'getStorageSync',
89
+ 'getStorageInfo',
90
+ 'getStorageInfoSync',
91
+ 'removeStorage',
92
+ 'removeStorageSync',
93
+ 'clearStorage',
94
+ 'clearStorageSync',
95
+
96
+ // system
97
+ 'getSystemInfo',
98
+ 'getSystemInfoSync',
99
+
100
+ // setting
101
+ 'getMenuButtonBoundingClientRect',
102
+
103
+ // toast
104
+ 'showToast',
105
+ 'hideToast',
106
+ 'showLoading',
107
+ 'hideLoading',
108
+
109
+ // vibrate
110
+ 'vibrateShort',
111
+ 'vibrateLong',
112
+
113
+ // window
114
+ 'onWindowResize',
115
+ 'offWindowResize',
116
+
117
+ // bluetooth
118
+ 'openBluetoothAdapter',
119
+ 'closeBluetoothAdapter',
120
+ 'startBluetoothDevicesDiscovery',
121
+ 'stopBluetoothDevicesDiscovery',
122
+ 'onBluetoothDeviceFound',
123
+ 'offBluetoothDeviceFound',
124
+ 'getConnectedBluetoothDevices',
125
+ 'getBluetoothAdapterState',
126
+ 'onBluetoothAdapterStateChange',
127
+ 'offBluetoothAdapterStateChange',
128
+ 'getBluetoothDevices',
129
+ 'writeBLECharacteristicValue',
130
+ 'readBLECharacteristicValue',
131
+ 'notifyBLECharacteristicValueChange',
132
+ 'onBLECharacteristicValueChange',
133
+ 'offBLECharacteristicValueChange',
134
+ 'setBLEMTU',
135
+ 'getBLEDeviceRSSI',
136
+ 'getBLEDeviceServices',
137
+ 'getBLEDeviceCharacteristics',
138
+ 'createBLEConnection',
139
+ 'closeBLEConnection',
140
+ 'onBLEConnectionStateChange',
141
+ 'offBLEConnectionStateChange',
142
+
143
+ // wifi
144
+ 'startWifi',
145
+ 'stopWifi',
146
+ 'getWifiList',
147
+ 'onGetWifiList',
148
+ 'offGetWifiList',
149
+ 'getConnectedWifi'
150
+ ]
151
+
152
+ /**
153
+ * 支持的类及其方法
154
+ * 对应各个类文件中定义的类和方法
155
+ */
156
+ export const SUPPORTED_OBJECTS = {
157
+ // SelectorQuery 相关
158
+ SelectorQuery: [
159
+ 'in',
160
+ 'select',
161
+ 'selectAll',
162
+ 'selectViewport',
163
+ 'exec'
164
+ ],
165
+
166
+ NodesRef: [
167
+ 'boundingClientRect',
168
+ 'scrollOffset',
169
+ 'fields',
170
+ 'context',
171
+ 'node'
172
+ ],
173
+
174
+ // IntersectionObserver
175
+ IntersectionObserver: [
176
+ 'relativeTo',
177
+ 'relativeToViewport',
178
+ 'observe',
179
+ 'disconnect'
180
+ ],
181
+
182
+ // Animation
183
+ Animation: [
184
+ 'opacity',
185
+ 'backgroundColor',
186
+ 'width',
187
+ 'height',
188
+ 'top',
189
+ 'left',
190
+ 'bottom',
191
+ 'right',
192
+ 'rotate',
193
+ 'rotateX',
194
+ 'rotateY',
195
+ 'rotateZ',
196
+ 'rotate3d',
197
+ 'scale',
198
+ 'scaleX',
199
+ 'scaleY',
200
+ 'scaleZ',
201
+ 'scale3d',
202
+ 'translate',
203
+ 'translateX',
204
+ 'translateY',
205
+ 'translateZ',
206
+ 'translate3d',
207
+ 'skew',
208
+ 'skewX',
209
+ 'skewY',
210
+ 'matrix',
211
+ 'matrix3d',
212
+ 'step',
213
+ 'export'
214
+ ],
215
+
216
+ // Task 相关
217
+ SocketTask: [
218
+ 'send',
219
+ 'close',
220
+ 'onOpen',
221
+ 'onClose',
222
+ 'onError',
223
+ 'onMessage'
224
+ ],
225
+
226
+ RequestTask: [
227
+ 'abort',
228
+ 'onHeadersReceived',
229
+ 'offHeadersReceived'
230
+ ]
231
+ }
@@ -1,4 +1,4 @@
1
- import { ENV_OBJ } from '../../../common/js'
1
+ import { ENV_OBJ, envError } from '../../../common/js'
2
2
 
3
3
  function closeBLEConnection (options = {}) {
4
4
  return ENV_OBJ.disconnectBLEDevice(options)
@@ -12,8 +12,73 @@ function onBLEConnectionStateChange (callback) {
12
12
  return ENV_OBJ.onBLEConnectionStateChanged(callback)
13
13
  }
14
14
 
15
+ function offBLEConnectionStateChange (callback) {
16
+ return ENV_OBJ.offBLEConnectionStateChanged(callback)
17
+ }
18
+
19
+ const openBluetoothAdapter = ENV_OBJ.openBluetoothAdapter || envError('openBluetoothAdapter')
20
+
21
+ const closeBluetoothAdapter = ENV_OBJ.closeBluetoothAdapter || envError('closeBluetoothAdapter')
22
+
23
+ const startBluetoothDevicesDiscovery = ENV_OBJ.startBluetoothDevicesDiscovery || envError('startBluetoothDevicesDiscovery')
24
+
25
+ const stopBluetoothDevicesDiscovery = ENV_OBJ.stopBluetoothDevicesDiscovery || envError('stopBluetoothDevicesDiscovery')
26
+
27
+ const onBluetoothDeviceFound = ENV_OBJ.onBluetoothDeviceFound || envError('onBluetoothDeviceFound')
28
+
29
+ const offBluetoothDeviceFound = ENV_OBJ.offBluetoothDeviceFound || envError('offBluetoothDeviceFound')
30
+
31
+ const getConnectedBluetoothDevices = ENV_OBJ.getConnectedBluetoothDevices || envError('getConnectedBluetoothDevices')
32
+
33
+ const getBluetoothAdapterState = ENV_OBJ.getBluetoothAdapterState || envError('getBluetoothAdapterState')
34
+
35
+ const onBluetoothAdapterStateChange = ENV_OBJ.onBluetoothAdapterStateChange || envError('onBluetoothAdapterStateChange')
36
+
37
+ const offBluetoothAdapterStateChange = ENV_OBJ.offBluetoothAdapterStateChange || envError('offBluetoothAdapterStateChange')
38
+
39
+ const getBluetoothDevices = ENV_OBJ.getBluetoothDevices || envError('getBluetoothDevices')
40
+
41
+ const writeBLECharacteristicValue = ENV_OBJ.writeBLECharacteristicValue || envError('writeBLECharacteristicValue')
42
+
43
+ const readBLECharacteristicValue = ENV_OBJ.readBLECharacteristicValue || envError('readBLECharacteristicValue')
44
+
45
+ const notifyBLECharacteristicValueChange = ENV_OBJ.notifyBLECharacteristicValueChange || envError('notifyBLECharacteristicValueChange')
46
+
47
+ const onBLECharacteristicValueChange = ENV_OBJ.onBLECharacteristicValueChange || envError('onBLECharacteristicValueChange')
48
+
49
+ const offBLECharacteristicValueChange = ENV_OBJ.offBLECharacteristicValueChange || envError('offBLECharacteristicValueChange')
50
+
51
+ const setBLEMTU = ENV_OBJ.setBLEMTU || envError('setBLEMTU')
52
+
53
+ const getBLEDeviceRSSI = ENV_OBJ.getBLEDeviceRSSI || envError('getBLEDeviceRSSI')
54
+
55
+ const getBLEDeviceServices = ENV_OBJ.getBLEDeviceServices || envError('getBLEDeviceServices')
56
+
57
+ const getBLEDeviceCharacteristics = ENV_OBJ.getBLEDeviceCharacteristics || envError('getBLEDeviceCharacteristics')
58
+
15
59
  export {
16
60
  closeBLEConnection,
17
61
  createBLEConnection,
18
- onBLEConnectionStateChange
62
+ onBLEConnectionStateChange,
63
+ offBLEConnectionStateChange,
64
+ openBluetoothAdapter,
65
+ closeBluetoothAdapter,
66
+ startBluetoothDevicesDiscovery,
67
+ stopBluetoothDevicesDiscovery,
68
+ onBluetoothDeviceFound,
69
+ offBluetoothDeviceFound,
70
+ getConnectedBluetoothDevices,
71
+ getBluetoothAdapterState,
72
+ onBluetoothAdapterStateChange,
73
+ offBluetoothAdapterStateChange,
74
+ getBluetoothDevices,
75
+ writeBLECharacteristicValue,
76
+ readBLECharacteristicValue,
77
+ notifyBLECharacteristicValueChange,
78
+ onBLECharacteristicValueChange,
79
+ offBLECharacteristicValueChange,
80
+ setBLEMTU,
81
+ getBLEDeviceRSSI,
82
+ getBLEDeviceServices,
83
+ getBLEDeviceCharacteristics
19
84
  }