@mpxjs/api-proxy 2.9.0-beta.1 → 2.9.0-beta.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": "@mpxjs/api-proxy",
3
- "version": "2.9.0-beta.1",
3
+ "version": "2.9.0-beta.3",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -39,5 +39,5 @@
39
39
  "dependencies": {
40
40
  "axios": "^0.21.1"
41
41
  },
42
- "gitHead": "5e4b44c9b8132e9192cfd119f63196ecc6c4486c"
42
+ "gitHead": "521f0cec1231962f9c071c8b70499772a0a81fda"
43
43
  }
@@ -97,16 +97,8 @@ class WebIntersectionObserver {
97
97
  nextTick(() => {
98
98
  const marginsTemp = margins || {}
99
99
  const { left = 0, right = 0, top = 0, bottom = 0 } = marginsTemp
100
- this._root = document.querySelector('html')
101
- const viewportWidth = window.innerWidth || document.documentElement.clientWidth
102
- const viewportHeight = window.innerHeight || document.documentElement.clientHeight
103
- const rootWidth = this._root.offsetWidth || 0
104
- const rootHeight = this._root.offsetHeight || 0
105
- if (rootHeight >= viewportHeight) {
106
- this._rootMargin = `${top}px ${viewportWidth - rootWidth + right}px ${viewportHeight - rootHeight + bottom}px ${left}px`
107
- } else {
108
- this._rootMargin = `${top}px ${right}px ${bottom}px ${left}px`
109
- }
100
+ this._root = null
101
+ this._rootMargin = `${top}px ${right}px ${bottom}px ${left}px`
110
102
  this._relativeInfo.push({ selector: null, margins })
111
103
  })
112
104
  return this
@@ -75,7 +75,7 @@ class SelectQuery {
75
75
  }
76
76
 
77
77
  _handleFields (fields, el, selector) {
78
- if (!el) return null
78
+ if (!el || (el && !el.getBoundingClientRect)) return null
79
79
  const { id, dataset, rect, size, scrollOffset, properties = [], computedStyle = [], node } = fields
80
80
  const { left, right, top, bottom, width, height } = el.getBoundingClientRect()
81
81
 
@@ -19,13 +19,13 @@ class EventChannel {
19
19
  }
20
20
  }
21
21
 
22
- off (eventName, EventCallback) {
23
- if (EventCallback) {
22
+ off (eventName, listener) {
23
+ if (listener) {
24
24
  const cbs = this.listener[eventName]
25
25
  const copyCbs = []
26
26
  if (cbs) {
27
27
  cbs.forEach((item) => {
28
- if (item.fn !== EventCallback) {
28
+ if (item.fn !== listener) {
29
29
  copyCbs.push(item)
30
30
  }
31
31
  })
@@ -36,26 +36,25 @@ class EventChannel {
36
36
  }
37
37
  }
38
38
 
39
- on (eventName, EventCallback) {
40
- (this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type: 'on' })
39
+ on (eventName, listener) {
40
+ this._addListener(eventName, listener, 'on')
41
41
  }
42
42
 
43
- once (eventName, EventCallback) {
44
- (this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type: 'once' })
43
+ once (eventName, listener) {
44
+ this._addListener(eventName, listener, 'once')
45
45
  }
46
46
 
47
- _addListener (eventName, EventCallback, type) {
48
- (this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type })
47
+ _addListener (eventName, listener, type) {
48
+ (this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: listener, type })
49
49
  }
50
50
 
51
51
  _addListeners (events) {
52
- if (Object.prototype.toString.call(events) === '[object Object]') {
53
- Object.keys(events).forEach((eventName) => {
54
- (this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: events[eventName], type: 'on' })
55
- })
56
- }
52
+ Object.keys(events).forEach((eventName) => {
53
+ this.on(eventName, events[eventName])
54
+ })
57
55
  }
58
56
  }
57
+
59
58
  export {
60
59
  EventChannel
61
60
  }
@@ -39,7 +39,17 @@ function request (options = { url: '' }) {
39
39
  headers: header,
40
40
  responseType,
41
41
  timeout,
42
- cancelToken: source.token
42
+ cancelToken: source.token,
43
+ transitional: {
44
+ // silent JSON parsing mode
45
+ // `true` - ignore JSON parsing errors and set response.data to null if parsing failed (old behaviour)
46
+ // `false` - throw SyntaxError if JSON parsing failed (Note: responseType must be set to 'json')
47
+ silentJSONParsing: true, // default value for the current Axios version
48
+ // try to parse the response string as JSON even if `responseType` is not 'json'
49
+ forcedJSONParsing: false,
50
+ // throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
51
+ clarifyTimeoutError: false
52
+ }
43
53
  }
44
54
 
45
55
  if (method === 'GET') {
@@ -49,7 +59,7 @@ function request (options = { url: '' }) {
49
59
 
50
60
  const promise = axios(rOptions).then(res => {
51
61
  let data = res.data
52
- if (responseType === 'text' && dataType === 'json') {
62
+ if (dataType === 'json' && typeof data === 'string') {
53
63
  try {
54
64
  data = JSON.parse(data)
55
65
  } catch (e) {
@@ -1,5 +1,8 @@
1
1
  import { webHandleSuccess, webHandleFail, isTabBarPage, throwSSRWarning, isBrowser } from '../../../common/js'
2
2
  import { EventChannel } from '../event-channel'
3
+
4
+ let routeCount = 0
5
+
3
6
  function redirectTo (options = {}) {
4
7
  if (!isBrowser) {
5
8
  throwSSRWarning('redirectTo API is running in non browser environments')
@@ -12,11 +15,17 @@ function redirectTo (options = {}) {
12
15
  webHandleFail(res, options.fail, options.complete)
13
16
  return Promise.reject(res)
14
17
  }
15
- router.__mpxAction = { type: 'redirect' }
18
+ router.__mpxAction = {
19
+ type: 'redirect'
20
+ }
21
+ if (routeCount === 0 && router.currentRoute.query.routeCount) routeCount = router.currentRoute.query.routeCount
16
22
  return new Promise((resolve, reject) => {
17
23
  router.replace(
18
24
  {
19
- path: options.url
25
+ path: options.url,
26
+ query: {
27
+ routeCount: ++routeCount
28
+ }
20
29
  },
21
30
  () => {
22
31
  const res = { errMsg: 'redirectTo:ok' }
@@ -53,10 +62,14 @@ function navigateTo (options = {}) {
53
62
  if (options.events) {
54
63
  eventChannel._addListeners(options.events)
55
64
  }
65
+ if (routeCount === 0 && router.currentRoute.query.routeCount) routeCount = router.currentRoute.query.routeCount
56
66
  return new Promise((resolve, reject) => {
57
67
  router.push(
58
68
  {
59
- path: options.url
69
+ path: options.url,
70
+ query: {
71
+ routeCount: ++routeCount
72
+ }
60
73
  },
61
74
  () => {
62
75
  const res = { errMsg: 'navigateTo:ok', eventChannel }
@@ -80,7 +93,11 @@ function navigateBack (options = {}) {
80
93
  }
81
94
  const router = global.__mpxRouter
82
95
  if (router) {
83
- const delta = options.delta || 1
96
+ let delta = options.delta || 1
97
+ const stackLength = router.stack.length
98
+ if (stackLength > 1 && delta >= stackLength) {
99
+ delta = stackLength - 1
100
+ }
84
101
  router.__mpxAction = {
85
102
  type: 'back',
86
103
  delta
@@ -92,8 +109,6 @@ function navigateBack (options = {}) {
92
109
  }
93
110
  }
94
111
 
95
- let reLaunchCount = 0
96
-
97
112
  function reLaunch (options = {}) {
98
113
  if (!isBrowser) {
99
114
  throwSSRWarning('reLaunch API is running in non browser environments')
@@ -101,14 +116,14 @@ function reLaunch (options = {}) {
101
116
  }
102
117
  const router = global.__mpxRouter
103
118
  if (router) {
104
- if (reLaunchCount === 0 && router.currentRoute.query.reLaunchCount) reLaunchCount = router.currentRoute.query.reLaunchCount
105
- const delta = router.stack.length - 1
119
+ if (routeCount === 0 && router.currentRoute.query.routeCount) routeCount = router.currentRoute.query.routeCount
106
120
  router.__mpxAction = {
107
121
  type: 'reLaunch',
108
122
  path: options.url,
109
- reLaunchCount: ++reLaunchCount,
123
+ routeCount: ++routeCount,
110
124
  replaced: false
111
125
  }
126
+ const delta = router.stack.length - 1
112
127
  // 在需要操作后退时,先操作后退,在beforeEach中基于当前action通过next()进行replace操作,避免部分浏览器的表现不一致
113
128
  if (delta > 0) {
114
129
  router.go(-delta)
@@ -119,7 +134,7 @@ function reLaunch (options = {}) {
119
134
  {
120
135
  path: options.url,
121
136
  query: {
122
- reLaunchCount
137
+ routeCount
123
138
  }
124
139
  },
125
140
  () => {
@@ -156,12 +171,12 @@ function switchTab (options = {}) {
156
171
  webHandleFail(res, options.fail, options.complete)
157
172
  return Promise.reject(res)
158
173
  }
159
- const delta = router.stack.length - 1
160
174
  router.__mpxAction = {
161
175
  type: 'switch',
162
176
  path: options.url,
163
177
  replaced: false
164
178
  }
179
+ const delta = router.stack.length - 1
165
180
  if (delta > 0) {
166
181
  router.go(-delta)
167
182
  } else {