@mpxjs/api-proxy 2.9.11-test.0 → 2.9.13

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.
Files changed (109) hide show
  1. package/package.json +2 -2
  2. package/src/common/js/ToPromise.js +25 -0
  3. package/src/common/js/index.js +1 -0
  4. package/src/common/js/utils.js +1 -32
  5. package/src/index.js +75 -4
  6. package/src/index.web.js +24 -0
  7. package/src/mini/platform/wxToAli.js +667 -0
  8. package/src/mini/platform/wxToQq.js +20 -0
  9. package/src/mini/platform/wxToTt.js +9 -0
  10. package/src/{common/js → mini}/promisify.js +3 -4
  11. package/src/mini/transform.js +71 -0
  12. package/src/{platform → web}/api/action-sheet/ActionSheet.js +6 -6
  13. package/src/web/api/index.js +68 -0
  14. package/src/{platform → web}/api/modal/Modal.js +7 -2
  15. package/src/{platform/api/pull-down/index.web.js → web/api/pull-down/index.js} +22 -14
  16. package/src/{platform/api/request/index.web.js → web/api/request/index.js} +5 -2
  17. package/src/{platform/api/route/index.web.js → web/api/route/index.js} +80 -59
  18. package/src/{platform/api/set-navigation-bar/index.web.js → web/api/set-navigation-bar/index.js} +1 -0
  19. package/src/{platform → web}/api/socket/SocketTask.js +1 -1
  20. package/src/{platform/api/storage/index.web.js → web/api/storage/index.js} +10 -0
  21. package/src/{platform/api/system/index.web.js → web/api/system/index.js} +1 -0
  22. package/src/{platform/api/tab-bar/index.web.js → web/api/tab-bar/index.js} +8 -0
  23. package/src/{platform → web}/api/toast/Toast.js +3 -1
  24. package/src/install.js +0 -62
  25. package/src/platform/api/action-sheet/index.ali.js +0 -28
  26. package/src/platform/api/action-sheet/index.js +0 -9
  27. package/src/platform/api/add-phone-contact/index.ali.js +0 -13
  28. package/src/platform/api/add-phone-contact/index.js +0 -9
  29. package/src/platform/api/animation/index.js +0 -9
  30. package/src/platform/api/app/index.js +0 -24
  31. package/src/platform/api/audio/index.js +0 -9
  32. package/src/platform/api/base/index.js +0 -12
  33. package/src/platform/api/ble-connection/index.ali.js +0 -17
  34. package/src/platform/api/ble-connection/index.js +0 -15
  35. package/src/platform/api/canvas/index.ali.js +0 -16
  36. package/src/platform/api/canvas/index.js +0 -15
  37. package/src/platform/api/check-session/index.js +0 -9
  38. package/src/platform/api/clipboard-data/index.ali.js +0 -30
  39. package/src/platform/api/clipboard-data/index.js +0 -12
  40. package/src/platform/api/create-intersection-observer/index.ali.js +0 -7
  41. package/src/platform/api/create-intersection-observer/index.js +0 -9
  42. package/src/platform/api/create-selector-query/index.ali.js +0 -35
  43. package/src/platform/api/create-selector-query/index.js +0 -9
  44. package/src/platform/api/device/network/index.js +0 -15
  45. package/src/platform/api/file/index.ali.js +0 -35
  46. package/src/platform/api/file/index.js +0 -12
  47. package/src/platform/api/get-user-info/index.ali.js +0 -18
  48. package/src/platform/api/get-user-info/index.js +0 -9
  49. package/src/platform/api/image/index.ali.js +0 -36
  50. package/src/platform/api/image/index.js +0 -12
  51. package/src/platform/api/login/index.ali.js +0 -15
  52. package/src/platform/api/login/index.js +0 -9
  53. package/src/platform/api/make-phone-call/index.ali.js +0 -12
  54. package/src/platform/api/make-phone-call/index.js +0 -9
  55. package/src/platform/api/modal/index.ali.js +0 -32
  56. package/src/platform/api/modal/index.js +0 -9
  57. package/src/platform/api/next-tick/index.ali.js +0 -7
  58. package/src/platform/api/next-tick/index.js +0 -9
  59. package/src/platform/api/next-tick/index.tt.js +0 -7
  60. package/src/platform/api/page-scroll-to/index.js +0 -9
  61. package/src/platform/api/pull-down/index.js +0 -12
  62. package/src/platform/api/request/index.ali.js +0 -26
  63. package/src/platform/api/request/index.js +0 -9
  64. package/src/platform/api/request-payment/index.ali.js +0 -45
  65. package/src/platform/api/request-payment/index.js +0 -9
  66. package/src/platform/api/route/index.js +0 -21
  67. package/src/platform/api/scan-code/index.ali.js +0 -17
  68. package/src/platform/api/scan-code/index.js +0 -9
  69. package/src/platform/api/screen-brightness/index.ali.js +0 -26
  70. package/src/platform/api/screen-brightness/index.js +0 -12
  71. package/src/platform/api/set-navigation-bar/index.ali.js +0 -25
  72. package/src/platform/api/set-navigation-bar/index.js +0 -12
  73. package/src/platform/api/socket/index.js +0 -9
  74. package/src/platform/api/storage/index.ali.js +0 -59
  75. package/src/platform/api/storage/index.js +0 -36
  76. package/src/platform/api/system/index.ali.js +0 -38
  77. package/src/platform/api/system/index.js +0 -12
  78. package/src/platform/api/tab-bar/index.js +0 -18
  79. package/src/platform/api/toast/index.ali.js +0 -39
  80. package/src/platform/api/toast/index.js +0 -18
  81. package/src/platform/api/video/index.js +0 -9
  82. package/src/platform/api/window/index.js +0 -12
  83. package/src/platform/index.js +0 -106
  84. /package/src/{platform/api/action-sheet/index.web.js → web/api/action-sheet/index.js} +0 -0
  85. /package/src/{platform → web}/api/animation/animation.js +0 -0
  86. /package/src/{platform/api/animation/index.web.js → web/api/animation/index.js} +0 -0
  87. /package/src/{platform/api/app/index.web.js → web/api/app/index.js} +0 -0
  88. /package/src/{platform/api/audio/index.web.js → web/api/audio/index.js} +0 -0
  89. /package/src/{platform → web}/api/base/base64.js +0 -0
  90. /package/src/{platform/api/base/index.web.js → web/api/base/index.js} +0 -0
  91. /package/src/{platform → web}/api/create-intersection-observer/IntersectionObserver.js +0 -0
  92. /package/src/{platform/api/create-intersection-observer/index.web.js → web/api/create-intersection-observer/index.js} +0 -0
  93. /package/src/{platform → web}/api/create-selector-query/NodesRef.js +0 -0
  94. /package/src/{platform → web}/api/create-selector-query/SelectQuery.js +0 -0
  95. /package/src/{platform/api/create-selector-query/index.web.js → web/api/create-selector-query/index.js} +0 -0
  96. /package/src/{platform → web}/api/device/network/getNetworkType.js +0 -0
  97. /package/src/{platform/api/device/network/index.web.js → web/api/device/network/index.js} +0 -0
  98. /package/src/{platform → web}/api/device/network/onNetworkStatusChange.js +0 -0
  99. /package/src/{platform/api/event-channel/index.web.js → web/api/event-channel/index.js} +0 -0
  100. /package/src/{platform/api/modal/index.web.js → web/api/modal/index.js} +0 -0
  101. /package/src/{platform/api/next-tick/index.web.js → web/api/next-tick/index.js} +0 -0
  102. /package/src/{platform/api/page-scroll-to/index.web.js → web/api/page-scroll-to/index.js} +0 -0
  103. /package/src/{platform/api/image → web/api/preview-image}/Preview.js +0 -0
  104. /package/src/{platform/api/image/index.web.js → web/api/preview-image/index.js} +0 -0
  105. /package/src/{platform → web}/api/request/RequestTask.js +0 -0
  106. /package/src/{platform/api/socket/index.web.js → web/api/socket/index.js} +0 -0
  107. /package/src/{platform/api/toast/index.web.js → web/api/toast/index.js} +0 -0
  108. /package/src/{platform/api/video/index.web.js → web/api/video/index.js} +0 -0
  109. /package/src/{platform/api/window/index.web.js → web/api/window/index.js} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/api-proxy",
3
- "version": "2.9.11-test.0",
3
+ "version": "2.9.13",
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": "d244d661acb081b709fc99ca8f6add541f793877"
42
+ "gitHead": "2c67b32fe01ff15fc1b728f91b4b5652244c2f35"
43
43
  }
@@ -0,0 +1,25 @@
1
+ class ToPromise {
2
+ constructor () {
3
+ this._resolve = null
4
+ this._reject = null
5
+ }
6
+
7
+ toPromiseInitPromise () {
8
+ return new Promise((resolve, reject) => {
9
+ this._resolve = resolve
10
+ this._reject = reject
11
+ })
12
+ }
13
+
14
+ toPromiseResolve (res) {
15
+ this._resolve(res)
16
+ }
17
+
18
+ toPromiseReject (err) {
19
+ this._reject(err)
20
+ }
21
+ }
22
+
23
+ export {
24
+ ToPromise
25
+ }
@@ -1,2 +1,3 @@
1
+ export * from './ToPromise'
1
2
  export * from './web'
2
3
  export * from './utils'
@@ -29,6 +29,7 @@ function changeOpts (options, updateOrRemoveOpt = {}, extraOpt = {}) {
29
29
  })
30
30
 
31
31
  opts = Object.assign({}, opts, extraOpt)
32
+
32
33
  return opts
33
34
  }
34
35
 
@@ -76,31 +77,6 @@ function getEnvObj () {
76
77
  return qa
77
78
  case 'dd':
78
79
  return dd
79
- case 'web':
80
- return {}
81
- }
82
- }
83
-
84
- function getEnvStr () {
85
- switch (__mpx_mode__) {
86
- case 'wx':
87
- return 'wx'
88
- case 'ali':
89
- return 'ali'
90
- case 'swan':
91
- return 'swan'
92
- case 'qq':
93
- return 'qq'
94
- case 'tt':
95
- return 'tt'
96
- case 'jd':
97
- return 'jd'
98
- case 'qa':
99
- return 'qa'
100
- case 'dd':
101
- return 'dd'
102
- case 'web':
103
- return 'web'
104
80
  }
105
81
  }
106
82
 
@@ -111,11 +87,6 @@ function warn (msg) {
111
87
  function error (msg) {
112
88
  console.error && console.error(`[@mpxjs/api-proxy error]:\n ${msg}`)
113
89
  }
114
- function envError (method) {
115
- return () => {
116
- console.error && console.error(`[@mpxjs/api-proxy error]:\n ${__mpx_mode__}环境不支持${method}方法`)
117
- }
118
- }
119
90
 
120
91
  function noop () {
121
92
  }
@@ -137,9 +108,7 @@ export {
137
108
  handleSuccess,
138
109
  genFromMap,
139
110
  getEnvObj,
140
- getEnvStr,
141
111
  error,
142
- envError,
143
112
  warn,
144
113
  noop,
145
114
  makeMap,
package/src/index.js CHANGED
@@ -1,4 +1,75 @@
1
- import install, { getProxy } from './install'
2
- export * from './platform'
3
- export { getProxy }
4
- export default install
1
+ import transformApi from './mini/transform'
2
+ import promisify from './mini/promisify'
3
+
4
+ export default function install (target, options = {}) {
5
+ const {
6
+ usePromise = false, // 是否转为 promise 格式
7
+ whiteList = [], // 强制变成 promise 格式的 api
8
+ blackList = [], // 强制不变成 promise 格式的 api
9
+ platform = {},
10
+ exclude = ['shareImageMessage'], // 强制不进行代理的api,临时fix微信分享朋友圈白屏
11
+ custom = {}, // 自定义转化规则
12
+ fallbackMap = {} // 对于不支持的API,允许配置一个映射表,接管不存在的API
13
+ } = options
14
+
15
+ let { from = '', to = '' } = platform
16
+ /* eslint-disable camelcase, no-undef */
17
+ if (typeof __mpx_src_mode__ !== 'undefined') {
18
+ if (from && from !== __mpx_src_mode__) {
19
+ console.warn && console.warn('the platform from field inconsistent with the current environment value\n')
20
+ }
21
+ from = `__mpx_src_mode_${__mpx_src_mode__}__`
22
+ } else {
23
+ if (!from) {
24
+ from = 'wx'
25
+ console.warn && console.warn('the platform from field is empty, wx will be used by default\n')
26
+ }
27
+ from = `__mpx_src_mode_${from}__`
28
+ }
29
+
30
+ if (typeof __mpx_mode__ !== 'undefined') {
31
+ to = __mpx_mode__
32
+ } else if (!to) {
33
+ console.warn && console.warn('the platform to field is empty, ali will be used by default\n')
34
+ to = 'wx'
35
+ }
36
+ /* eslint-enable */
37
+
38
+ const transedApi = transformApi({
39
+ exclude,
40
+ from,
41
+ to,
42
+ custom
43
+ })
44
+
45
+ const promisedApi = usePromise ? promisify(transedApi, whiteList, blackList) : {}
46
+ const allApi = Object.assign({}, transedApi, promisedApi)
47
+
48
+ Object.keys(allApi).forEach(api => {
49
+ try {
50
+ if (typeof allApi[api] !== 'function') {
51
+ target[api] = allApi[api]
52
+ return
53
+ }
54
+
55
+ target[api] = (...args) => {
56
+ return allApi[api].apply(target, args)
57
+ }
58
+ } catch (e) {
59
+ } // 支付宝不支持重写 call 方法
60
+ })
61
+
62
+ // Fallback Map option
63
+ Object.keys(fallbackMap)
64
+ .forEach(k => {
65
+ if (!target[k]) {
66
+ target[k] = fallbackMap[k]
67
+ }
68
+ })
69
+ }
70
+
71
+ export function getProxy (options = {}) {
72
+ const apiProxy = {}
73
+ install(apiProxy, options)
74
+ return apiProxy
75
+ }
@@ -0,0 +1,24 @@
1
+ import * as allApi from './web/api'
2
+ import { genFromMap } from './common/js'
3
+
4
+ export default function install (target) {
5
+ const fromMap = genFromMap()
6
+
7
+ Object.keys(allApi).forEach(api => {
8
+ target[api] = function (...args) {
9
+ if (args.length > 0) {
10
+ const from = args.pop()
11
+ if (typeof from !== 'string' || !fromMap[from]) {
12
+ args.push(from)
13
+ }
14
+ }
15
+
16
+ return allApi[api].apply(target, args)
17
+ }
18
+ })
19
+ }
20
+ export function getProxy () {
21
+ const apiProxy = {}
22
+ install(apiProxy)
23
+ return apiProxy
24
+ }