@mpxjs/api-proxy 2.9.41 → 2.9.52

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.41",
3
+ "version": "2.9.52",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -37,6 +37,7 @@
37
37
  },
38
38
  "homepage": "https://github.com/didi/mpx#readme",
39
39
  "dependencies": {
40
+ "@mpxjs/utils": "^2.9.50",
40
41
  "axios": "^1.6.7"
41
42
  },
42
43
  "peerDependencies": {
@@ -63,5 +64,5 @@
63
64
  "optional": true
64
65
  }
65
66
  },
66
- "gitHead": "0b3ee5d2c1b6c6e0af6a842eb4e12f4a1173c067"
67
+ "gitHead": "d8b89e0ba1f29a16244b247f6c7a8d8197e0d732"
67
68
  }
@@ -101,20 +101,6 @@ function makeMap (arr) {
101
101
  }, {})
102
102
  }
103
103
 
104
- function parseDataset (dataset) {
105
- const parsed = {}
106
- for (const key in dataset) {
107
- if (hasOwn(dataset, key)) {
108
- try {
109
- parsed[key] = JSON.parse(dataset[key])
110
- } catch (e) {
111
- parsed[key] = dataset[key]
112
- }
113
- }
114
- }
115
- return parsed
116
- }
117
-
118
104
  function defineUnsupportedProps (resObj, props) {
119
105
  const defineProps = {}
120
106
  props.forEach((item) => {
@@ -149,7 +135,6 @@ export {
149
135
  hasOwn,
150
136
  throwSSRWarning,
151
137
  ENV_OBJ,
152
- parseDataset,
153
138
  type,
154
139
  defineUnsupportedProps
155
140
  }
@@ -1,5 +1,5 @@
1
1
  import { nextTick } from '../next-tick'
2
- import { parseDataset } from '../../../common/js'
2
+ import { parseDataset } from '@mpxjs/utils'
3
3
 
4
4
  let isInit = true
5
5
 
@@ -1,5 +1,5 @@
1
1
  import NodesRef from './NodesRef'
2
- import { parseDataset } from '../../../common/js'
2
+ import { parseDataset } from '@mpxjs/utils'
3
3
 
4
4
  class SelectQuery {
5
5
  constructor () {
@@ -2,11 +2,6 @@ import { webHandleSuccess, webHandleFail } from '../../../common/js'
2
2
  import { parseQuery } from '@mpxjs/utils'
3
3
 
4
4
  function parseUrl (url) {
5
- if (url.startsWith('/')) {
6
- url = url.slice(1)
7
- } else {
8
- // todo 处理相对路径
9
- }
10
5
  let path = url
11
6
  let query = ''
12
7
  const queryIndex = url.indexOf('?')
@@ -21,12 +16,46 @@ function parseUrl (url) {
21
16
  }
22
17
  }
23
18
 
19
+ function getBasePath (navigation) {
20
+ if (navigation) {
21
+ const state = navigation.getState()
22
+ return '/' + state.routes[state.index].name
23
+ }
24
+ return '/'
25
+ }
26
+
27
+ function resolvePath (relative, base) {
28
+ const firstChar = relative.charAt(0)
29
+ if (firstChar === '/') {
30
+ return relative
31
+ }
32
+ const stack = base.split('/')
33
+ stack.pop()
34
+ // resolve relative path
35
+ const segments = relative.replace(/^\//, '').split('/')
36
+ for (let i = 0; i < segments.length; i++) {
37
+ const segment = segments[i]
38
+ if (segment === '..') {
39
+ stack.pop()
40
+ } else if (segment !== '.') {
41
+ stack.push(segment)
42
+ }
43
+ }
44
+ // ensure leading slash
45
+ if (stack[0] !== '') {
46
+ stack.unshift('')
47
+ }
48
+ return stack.join('/')
49
+ }
50
+
24
51
  function navigateTo (options = {}) {
25
- const navigationRef = global.__navigationRef
52
+ const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
26
53
  const navigationHelper = global.__navigationHelper
27
- if (navigationHelper && navigationRef && navigationRef.isReady()) {
54
+ if (navigation && navigationHelper) {
28
55
  const { path, queryObj } = parseUrl(options.url)
29
- navigationRef.dispatch(navigationHelper.StackActions.push(path, queryObj))
56
+ const basePath = getBasePath(navigation)
57
+ const finalPath = resolvePath(path, basePath).slice(1)
58
+ navigation.push(finalPath, queryObj)
30
59
  navigationHelper.lastSuccessCallback = () => {
31
60
  const res = { errMsg: 'navigateTo:ok' }
32
61
  webHandleSuccess(res, options.success, options.complete)
@@ -39,11 +68,13 @@ function navigateTo (options = {}) {
39
68
  }
40
69
 
41
70
  function redirectTo (options = {}) {
42
- const navigationRef = global.__navigationRef
71
+ const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
43
72
  const navigationHelper = global.__navigationHelper
44
- if (navigationHelper && navigationRef && navigationRef.isReady()) {
73
+ if (navigation && navigationHelper) {
45
74
  const { path, queryObj } = parseUrl(options.url)
46
- navigationRef.dispatch(navigationHelper.StackActions.replace(path, queryObj))
75
+ const basePath = getBasePath(navigation)
76
+ const finalPath = resolvePath(path, basePath).slice(1)
77
+ navigation.replace(finalPath, queryObj)
47
78
  navigationHelper.lastSuccessCallback = () => {
48
79
  const res = { errMsg: 'redirectTo:ok' }
49
80
  webHandleSuccess(res, options.success, options.complete)
@@ -56,10 +87,10 @@ function redirectTo (options = {}) {
56
87
  }
57
88
 
58
89
  function navigateBack (options = {}) {
59
- const navigationRef = global.__navigationRef
90
+ const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
60
91
  const navigationHelper = global.__navigationHelper
61
- if (navigationHelper && navigationRef && navigationRef.isReady()) {
62
- navigationRef.dispatch(navigationHelper.StackActions.pop(options.delta || 1))
92
+ if (navigation && navigationHelper) {
93
+ navigation.pop(options.delta || 1)
63
94
  navigationHelper.lastSuccessCallback = () => {
64
95
  const res = { errMsg: 'navigateBack:ok' }
65
96
  webHandleSuccess(res, options.success, options.complete)
@@ -72,15 +103,17 @@ function navigateBack (options = {}) {
72
103
  }
73
104
 
74
105
  function reLaunch (options = {}) {
75
- const navigationRef = global.__navigationRef
106
+ const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
76
107
  const navigationHelper = global.__navigationHelper
77
- if (navigationHelper && navigationRef && navigationRef.isReady()) {
108
+ if (navigation && navigationHelper) {
78
109
  const { path, queryObj } = parseUrl(options.url)
79
- navigationRef.reset({
110
+ const basePath = getBasePath(navigation)
111
+ const finalPath = resolvePath(path, basePath).slice(1)
112
+ navigation.reset({
80
113
  index: 0,
81
114
  routes: [
82
115
  {
83
- name: path,
116
+ name: finalPath,
84
117
  params: queryObj
85
118
  }
86
119
  ]