@mpxjs/api-proxy 2.8.63 → 2.8.64-bridgetest

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": "@mpxjs/api-proxy",
3
- "version": "2.8.63",
3
+ "version": "2.8.64-bridgetest",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "promise"
32
32
  ],
33
33
  "author": "httpsxiao",
34
- "license": "ISC",
34
+ "license": "Apache-2.0",
35
35
  "bugs": {
36
36
  "url": "https://github.com/didi/mpx/issues"
37
37
  },
@@ -39,5 +39,5 @@
39
39
  "dependencies": {
40
40
  "axios": "^0.21.1"
41
41
  },
42
- "gitHead": "c4c20d476f874c8d5b6f6b987e4748444a28b3c5"
42
+ "gitHead": "4080623bc3f493d1cfb7a506dffaec0f79084af6"
43
43
  }
@@ -100,6 +100,9 @@ function makeMap (arr) {
100
100
 
101
101
  const isBrowser = typeof window !== 'undefined'
102
102
 
103
+ function throwSSRWarning (info) {
104
+ console.error(`[Mpx runtime error]: Dangerous API! ${info}, It may cause some problems, please use this method with caution`)
105
+ }
103
106
  export {
104
107
  changeOpts,
105
108
  handleSuccess,
@@ -110,5 +113,6 @@ export {
110
113
  noop,
111
114
  makeMap,
112
115
  isBrowser,
113
- hasOwn
116
+ hasOwn,
117
+ throwSSRWarning
114
118
  }
package/src/index.web.js CHANGED
@@ -1,12 +1,9 @@
1
1
  import * as allApi from './web/api'
2
- import { EventChannel } from './web/api/event-channel'
3
2
  import { genFromMap } from './common/js'
4
3
 
5
4
  export default function install (target) {
6
5
  const fromMap = genFromMap()
7
6
 
8
- global.EventChannel = new EventChannel()
9
-
10
7
  Object.keys(allApi).forEach(api => {
11
8
  target[api] = function (...args) {
12
9
  if (args.length > 0) {
@@ -1,8 +1,13 @@
1
1
  import ActionSheet from './ActionSheet'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  let actionSheet = null
4
5
 
5
6
  function showActionSheet (options = { itemList: [] }) {
7
+ if (!isBrowser) {
8
+ throwSSRWarning('showActionSheet API is running in non browser environments')
9
+ return
10
+ }
6
11
  if (!actionSheet) { actionSheet = new ActionSheet() }
7
12
  return actionSheet.show(options)
8
13
  }
@@ -1,3 +1,5 @@
1
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
+
1
3
  class Animation {
2
4
  constructor (options) {
3
5
  this._actions = []
@@ -5,11 +7,15 @@ class Animation {
5
7
  this._options = options
6
8
  }
7
9
 
8
- _processSize (size) {
10
+ _processSize (size, type) {
9
11
  if (typeof size === 'number') {
10
12
  return `${size}px`
11
13
  } else {
12
14
  if (size.indexOf('rpx') !== -1) {
15
+ if (!isBrowser) {
16
+ throwSSRWarning(`Animation's ${type} API cannot use rpx in non browser environments`)
17
+ return
18
+ }
13
19
  // 计算rpx折算回px
14
20
  const rs = parseInt(size, 10)
15
21
  const width = window.screen.width
@@ -29,7 +35,7 @@ class Animation {
29
35
  case 'bottom':
30
36
  case 'width':
31
37
  case 'height':
32
- value = this._processSize(value)
38
+ value = this._processSize(value, type)
33
39
  this._propMaps[type] = {
34
40
  args: [type, value],
35
41
  type: 'style'
@@ -1,3 +1,4 @@
1
+ import { isBrowser } from '../../../common/js'
1
2
  global.__mpxAppCbs = global.__mpxAppCbs || {
2
3
  show: [],
3
4
  hide: [],
@@ -6,7 +7,9 @@ global.__mpxAppCbs = global.__mpxAppCbs || {
6
7
  }
7
8
 
8
9
  function onError (callback) {
9
- global.__mpxAppCbs.error.push(callback)
10
+ if (isBrowser) {
11
+ global.__mpxAppCbs.error.push(callback)
12
+ }
10
13
  }
11
14
 
12
15
  function offError (callback) {
@@ -16,7 +19,9 @@ function offError (callback) {
16
19
  }
17
20
 
18
21
  function onAppShow (callback) {
19
- global.__mpxAppCbs.show.push(callback)
22
+ if (isBrowser) {
23
+ global.__mpxAppCbs.show.push(callback)
24
+ }
20
25
  }
21
26
 
22
27
  function offAppShow (callback) {
@@ -26,7 +31,9 @@ function offAppShow (callback) {
26
31
  }
27
32
 
28
33
  function onAppHide (callback) {
29
- global.__mpxAppCbs.hide.push(callback)
34
+ if (isBrowser) {
35
+ global.__mpxAppCbs.hide.push(callback)
36
+ }
30
37
  }
31
38
 
32
39
  function offAppHide (callback) {
@@ -1,4 +1,9 @@
1
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
1
2
  export const createInnerAudioContext = () => {
3
+ if (!isBrowser) {
4
+ throwSSRWarning('createInnerAudioContext API is running in non browser environments')
5
+ return
6
+ }
2
7
  // eslint-disable-next-line no-undef
3
8
  const audio = new Audio()
4
9
  const __audio = {}
@@ -1,6 +1,11 @@
1
1
  import WebIntersectionObserver from './IntersectionObserver'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  function createIntersectionObserver (component, options) {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('createIntersectionObserver API is running in non browser environments')
7
+ return
8
+ }
4
9
  return new WebIntersectionObserver(component, options)
5
10
  }
6
11
 
@@ -1,6 +1,11 @@
1
1
  import SelectQuery from './SelectQuery'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  function createSelectorQuery () {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('createSelectorQuery API is running in non browser environments')
7
+ return
8
+ }
4
9
  return new SelectQuery()
5
10
  }
6
11
 
@@ -1,11 +1,15 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, isBrowser, throwSSRWarning } from '../../../../common/js'
2
2
 
3
3
  export function getNetworkType ({ success, fail = () => {}, complete = () => {} }) {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('getNetworkType API is running in non browser environments')
6
+ return
7
+ }
4
8
  try {
5
9
  if (navigator.connection) {
6
10
  webHandleSuccess({ networkType: navigator.connection.effectiveType }, success, complete)
7
11
  } else {
8
- webHandleSuccess({ networkType: 'unknow' }, success, complete)
12
+ webHandleSuccess({ networkType: 'unknown' }, success, complete)
9
13
  }
10
14
  } catch (err) {
11
15
  webHandleFail(err, fail, complete)
@@ -1,4 +1,4 @@
1
- import { isBrowser } from '../../../../common/js/utils'
1
+ import { isBrowser, throwSSRWarning } from '../../../../common/js/utils'
2
2
  const fnMap = new Map()
3
3
 
4
4
  const oldObserveList = new Set()
@@ -13,6 +13,10 @@ if (isBrowser) {
13
13
  }
14
14
 
15
15
  export function onNetworkStatusChange (callbackFn) {
16
+ if (!isBrowser) {
17
+ throwSSRWarning('onNetworkStatusChange API is running in non browser environments')
18
+ return
19
+ }
16
20
  if (navigator.connection) {
17
21
  const proxyCallback = evt => {
18
22
  const isConnected = navigator.onLine
@@ -29,6 +33,10 @@ export function onNetworkStatusChange (callbackFn) {
29
33
  }
30
34
 
31
35
  export function offNetworkStatusChange (callbackFn) {
36
+ if (!isBrowser) {
37
+ throwSSRWarning('offNetworkStatusChange API is running in non browser environments')
38
+ return
39
+ }
32
40
  if (navigator.connection) {
33
41
  navigator.connection.removeEventListener('change', fnMap.get(callbackFn))
34
42
  } else {
@@ -1,8 +1,13 @@
1
1
  import Modal from './Modal'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  let modal = null
4
5
 
5
6
  function showModal (options = {}) {
7
+ if (!isBrowser) {
8
+ throwSSRWarning('showModal API is running in non browser environments')
9
+ return
10
+ }
6
11
  if (!modal) { modal = new Modal() }
7
12
  return modal.show(options)
8
13
  }
@@ -1,7 +1,11 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, isBrowser, throwSSRWarning } from '../../../common/js'
2
2
  import { nextTick } from '../next-tick'
3
3
 
4
4
  export function pageScrollTo (options) {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('pageScrollTo API is running in non browser environments')
7
+ return
8
+ }
5
9
  nextTick(() => {
6
10
  const ms = global.__ms
7
11
  const { success, fail, complete } = options
@@ -1,6 +1,10 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, throwSSRWarning, isBrowser } from '../../../common/js'
2
2
 
3
3
  function stopPullDownRefresh (options = {}) {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('stopPullDownRefresh API is running in non browser environments')
6
+ return
7
+ }
4
8
  const router = global.__mpxRouter
5
9
  if (router) {
6
10
  let err
@@ -31,6 +35,10 @@ function stopPullDownRefresh (options = {}) {
31
35
  }
32
36
 
33
37
  function startPullDownRefresh (options = {}) {
38
+ if (!isBrowser) {
39
+ throwSSRWarning('startPullDownRefresh API is running in non browser environments')
40
+ return
41
+ }
34
42
  const router = global.__mpxRouter
35
43
  if (router) {
36
44
  let err
@@ -1,9 +1,13 @@
1
- import { webHandleSuccess, webHandleFail, isTabBarPage } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, isTabBarPage, throwSSRWarning, isBrowser } from '../../../common/js'
2
2
  import { EventChannel } from '../event-channel'
3
3
 
4
4
  let routeCount = 0
5
5
 
6
6
  function redirectTo (options = {}) {
7
+ if (!isBrowser) {
8
+ throwSSRWarning('redirectTo API is running in non browser environments')
9
+ return
10
+ }
7
11
  const router = global.__mpxRouter
8
12
  if (router) {
9
13
  if (isTabBarPage(options.url, router)) {
@@ -39,6 +43,10 @@ function redirectTo (options = {}) {
39
43
  }
40
44
 
41
45
  function navigateTo (options = {}) {
46
+ if (!isBrowser) {
47
+ throwSSRWarning('navigateTo API is running in non browser environments')
48
+ return
49
+ }
42
50
  const router = global.__mpxRouter
43
51
  if (router) {
44
52
  if (isTabBarPage(options.url, router)) {
@@ -79,6 +87,10 @@ function navigateTo (options = {}) {
79
87
  }
80
88
 
81
89
  function navigateBack (options = {}) {
90
+ if (!isBrowser) {
91
+ throwSSRWarning('navigateBack API is running in non browser environments')
92
+ return
93
+ }
82
94
  const router = global.__mpxRouter
83
95
  if (router) {
84
96
  let delta = options.delta || 1
@@ -98,6 +110,10 @@ function navigateBack (options = {}) {
98
110
  }
99
111
 
100
112
  function reLaunch (options = {}) {
113
+ if (!isBrowser) {
114
+ throwSSRWarning('reLaunch API is running in non browser environments')
115
+ return
116
+ }
101
117
  const router = global.__mpxRouter
102
118
  if (router) {
103
119
  if (routeCount === 0 && router.currentRoute.query.routeCount) routeCount = router.currentRoute.query.routeCount
@@ -141,6 +157,10 @@ function reLaunch (options = {}) {
141
157
  }
142
158
 
143
159
  function switchTab (options = {}) {
160
+ if (!isBrowser) {
161
+ throwSSRWarning('switchTab API is running in non browser environments')
162
+ return
163
+ }
144
164
  const router = global.__mpxRouter
145
165
  if (router) {
146
166
  const toRoute = router.match(options.url, router.history.current)
@@ -1,6 +1,10 @@
1
- import { webHandleSuccess } from '../../../common/js'
1
+ import { isBrowser, throwSSRWarning, webHandleSuccess } from '../../../common/js'
2
2
 
3
3
  function setNavigationBarTitle (options = {}) {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('setNavigationBarTitle API is running in non browser environments')
6
+ return
7
+ }
4
8
  const { title, success, complete } = options
5
9
 
6
10
  if (document.title !== title) {
@@ -11,6 +15,10 @@ function setNavigationBarTitle (options = {}) {
11
15
  }
12
16
 
13
17
  function setNavigationBarColor (options = {}) {
18
+ if (!isBrowser) {
19
+ throwSSRWarning('setNavigationBarColor API is running in non browser environments')
20
+ return
21
+ }
14
22
  const { backgroundColor, success, complete } = options
15
23
  const meta = document.createElement('meta')
16
24
  meta.setAttribute('name', 'theme-color')
@@ -1,7 +1,11 @@
1
- import { warn, webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { warn, webHandleSuccess, webHandleFail, isBrowser, throwSSRWarning } from '../../../common/js'
2
2
  import SocketTask from './SocketTask'
3
3
 
4
4
  function connectSocket (options = { url: '' }) {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('connectSocket API is running in non browser environments')
7
+ return
8
+ }
5
9
  const { url, protocols, success, fail, complete } = options
6
10
 
7
11
  try {
@@ -1,6 +1,10 @@
1
- import { webHandleSuccess, webHandleFail, hasOwn } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, hasOwn, isBrowser, throwSSRWarning } from '../../../common/js'
2
2
 
3
3
  function setStorage (options = {}) {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('setStorage API is running in non browser environments')
6
+ return
7
+ }
4
8
  const { key, data, success, fail, complete } = options
5
9
 
6
10
  try {
@@ -17,6 +21,10 @@ function setStorage (options = {}) {
17
21
  }
18
22
 
19
23
  function setStorageSync (key = '', data) {
24
+ if (!isBrowser) {
25
+ throwSSRWarning('setStorageSync API is running in non browser environments')
26
+ return
27
+ }
20
28
  let obj = {}
21
29
 
22
30
  if (typeof data === 'symbol') {
@@ -28,6 +36,10 @@ function setStorageSync (key = '', data) {
28
36
  }
29
37
 
30
38
  function getStorage (options = {}) {
39
+ if (!isBrowser) {
40
+ throwSSRWarning('getStorage API is running in non browser environments')
41
+ return
42
+ }
31
43
  const { key, success, fail, complete } = options
32
44
  const { result, data } = getItem(key)
33
45
 
@@ -43,6 +55,10 @@ function getStorage (options = {}) {
43
55
  }
44
56
 
45
57
  function getStorageSync (key) {
58
+ if (!isBrowser) {
59
+ throwSSRWarning('getStorageSync API is running in non browser environments')
60
+ return
61
+ }
46
62
  const res = getItem(key)
47
63
  if (res.result) return res.data
48
64
 
@@ -64,6 +80,10 @@ function getItem (key) {
64
80
  }
65
81
 
66
82
  function getStorageInfo (options = {}) {
83
+ if (!isBrowser) {
84
+ throwSSRWarning('getStorageInfo API is running in non browser environments')
85
+ return
86
+ }
67
87
  const { success, fail, complete } = options
68
88
 
69
89
  try {
@@ -80,6 +100,10 @@ function getStorageInfo (options = {}) {
80
100
  }
81
101
 
82
102
  function getStorageInfoSync () {
103
+ if (!isBrowser) {
104
+ throwSSRWarning('getStorageInfoSync API is running in non browser environments')
105
+ return
106
+ }
83
107
  return {
84
108
  keys: Object.keys(window.localStorage),
85
109
  limitSize: null,
@@ -88,6 +112,10 @@ function getStorageInfoSync () {
88
112
  }
89
113
 
90
114
  function removeStorage (options = { key: '' }) {
115
+ if (!isBrowser) {
116
+ throwSSRWarning('removeStorage API is running in non browser environments')
117
+ return
118
+ }
91
119
  const { key, success, fail, complete } = options
92
120
 
93
121
  try {
@@ -104,10 +132,18 @@ function removeStorage (options = { key: '' }) {
104
132
  }
105
133
 
106
134
  function removeStorageSync (key) {
135
+ if (!isBrowser) {
136
+ throwSSRWarning('getStorageInfoSync API is running in non browser environments')
137
+ return
138
+ }
107
139
  window.localStorage.removeItem(key)
108
140
  }
109
141
 
110
142
  function clearStorage (options = {}) {
143
+ if (!isBrowser) {
144
+ throwSSRWarning('clearStorage API is running in non browser environments')
145
+ return
146
+ }
111
147
  const { success, fail, complete } = options
112
148
 
113
149
  try {
@@ -124,6 +160,10 @@ function clearStorage (options = {}) {
124
160
  }
125
161
 
126
162
  function clearStorageSync () {
163
+ if (!isBrowser) {
164
+ throwSSRWarning('clearStorageSync API is running in non browser environments')
165
+ return
166
+ }
127
167
  window.localStorage.clear()
128
168
  }
129
169
 
@@ -1,6 +1,10 @@
1
- import { webHandleSuccess } from '../../../common/js'
1
+ import { isBrowser, throwSSRWarning, webHandleSuccess } from '../../../common/js'
2
2
 
3
3
  function getSystemInfoSync () {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('getSystemInfoSync API is running in non browser environments')
6
+ return
7
+ }
4
8
  const ua = navigator.userAgent.split('(')[1].split(')')[0]
5
9
  const phones = new Map([
6
10
  ['iPhone', /iPhone|iPad|iPod|iOS/i],
@@ -66,6 +70,10 @@ function getSystemInfoSync () {
66
70
  }
67
71
 
68
72
  function getSystemInfo (options = {}) {
73
+ if (!isBrowser) {
74
+ throwSSRWarning('getSystemInfo API is running in non browser environments')
75
+ return
76
+ }
69
77
  const info = getSystemInfoSync()
70
78
  const res = Object.assign({ errMsg: 'getSystemInfo:ok' }, info)
71
79
  webHandleSuccess(res, options.success, options.complete)
@@ -1,8 +1,13 @@
1
1
  import Toast from './Toast'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  let toast = null
4
5
 
5
6
  function showToast (options = { title: '' }) {
7
+ if (!isBrowser) {
8
+ throwSSRWarning('showToast API is running in non browser environments')
9
+ return
10
+ }
6
11
  if (!toast) { toast = new Toast() }
7
12
  return toast.show(options, 'toast')
8
13
  }
@@ -13,6 +18,10 @@ function hideToast (options = {}) {
13
18
  }
14
19
 
15
20
  function showLoading (options = { title: '' }) {
21
+ if (!isBrowser) {
22
+ throwSSRWarning('showLoading API is running in non browser environments')
23
+ return
24
+ }
16
25
  if (!toast) { toast = new Toast() }
17
26
  return toast.show(Object.assign({
18
27
  icon: 'loading',
@@ -1,5 +1,11 @@
1
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
+
1
3
  const allowPlaybackRate = [0.5, 0.8, 1.0, 1.25, 1.5, 2.0]
2
4
  export const createVideoContext = (id, context) => {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('createVideoContext API is running in non browser environments')
7
+ return
8
+ }
3
9
  if (!id) {
4
10
  throw new Error('id为必传参数')
5
11
  }