Skip to content

Commit

Permalink
Merge pull request #1532 from didi/feat-api-proxy-modification
Browse files Browse the repository at this point in the history
修改utils重复方法
  • Loading branch information
hiyuki authored Aug 8, 2024
2 parents 0d4937a + f7d7385 commit 5599509
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 69 deletions.
2 changes: 1 addition & 1 deletion packages/api-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"homepage": "https://github.com/didi/mpx#readme",
"dependencies": {
"@mpxjs/utils": "^2.9.50",
"axios": "^1.6.7"
"axios": "^1.7.3"
},
"peerDependencies": {
"@react-native-async-storage/async-storage": "^1.23.1",
Expand Down
8 changes: 3 additions & 5 deletions packages/api-proxy/src/common/js/promisify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { getEnvObj } from './utils'

const envObj = getEnvObj()
import { ENV_OBJ } from './utils'

// 特别指定的不进行Promise封装的方法
const blackList = [
Expand Down Expand Up @@ -71,14 +69,14 @@ function promisify (listObj, whiteList, customBlackList) {
const obj = args[0] || {}
// 不需要转换 or 用户已定义回调,则不处理
if (!promisifyFilter(key) || obj.success || obj.fail) {
return listObj[key].apply(envObj, args)
return listObj[key].apply(ENV_OBJ, args)
} else { // 其他情况进行转换
if (!args[0]) args.unshift(obj)
let returned
const promise = new Promise((resolve, reject) => {
obj.success = resolve
obj.fail = reject
returned = listObj[key].apply(envObj, args)
returned = listObj[key].apply(ENV_OBJ, args)
})
promise.__returned = returned
return promise
Expand Down
52 changes: 2 additions & 50 deletions packages/api-proxy/src/common/js/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { hasOwn, noop, getEnvObj } from '@mpxjs/utils'

/**
*
* @param {Object} options 原参数
Expand All @@ -12,16 +14,6 @@
* d: 4 // 增加 d
* })
*/
const hasOwnProperty = Object.prototype.hasOwnProperty

function type (n) {
return Object.prototype.toString.call(n).slice(8, -1)
}

function hasOwn (obj, key) {
return hasOwnProperty.call(obj, key)
}

function changeOpts (options, updateOrRemoveOpt = {}, extraOpt = {}) {
let opts = {}

Expand Down Expand Up @@ -53,31 +45,6 @@ const handleSuccess = (opts, getOptions = noop, thisObj) => {
}
}

function getEnvObj () {
switch (__mpx_mode__) {
case 'wx':
return wx
case 'ali':
return my
case 'swan':
return swan
case 'qq':
return qq
case 'tt':
return tt
case 'jd':
return jd
case 'qa':
return qa
case 'dd':
return dd
case 'web':
case 'ios':
case 'android':
return {}
}
}

function warn (msg) {
console.warn && console.warn(`[@mpxjs/api-proxy warn]:\n ${msg}`)
}
Expand All @@ -91,16 +58,6 @@ function envError (method) {
}
}

function noop () {
}

function makeMap (arr) {
return arr.reduce((obj, item) => {
obj[item] = true
return obj
}, {})
}

function defineUnsupportedProps (resObj, props) {
const defineProps = {}
props.forEach((item) => {
Expand All @@ -125,16 +82,11 @@ const ENV_OBJ = getEnvObj()
export {
changeOpts,
handleSuccess,
getEnvObj,
error,
envError,
warn,
noop,
makeMap,
isBrowser,
hasOwn,
throwSSRWarning,
ENV_OBJ,
type,
defineUnsupportedProps
}
5 changes: 2 additions & 3 deletions packages/api-proxy/src/install.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as platformApi from './platform'
import { getEnvObj } from './common/js'
import { ENV_OBJ } from './common/js'
import promisify from './common/js/promisify'

export default function install (target, options = {}) {
Expand All @@ -9,8 +9,7 @@ export default function install (target, options = {}) {
blackList = [], // 强制不变成 promise 格式的 api
custom = {} // 自定义转化规则
} = options
const envObj = getEnvObj()
const transedApi = Object.assign({}, envObj, platformApi)
const transedApi = Object.assign({}, ENV_OBJ, platformApi)
const promisedApi = usePromise ? promisify(transedApi, whiteList, blackList) : {}
Object.assign(target, transedApi, promisedApi, custom[__mpx_mode__])
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ENV_OBJ, changeOpts, noop } from '../../../common/js'
import { ENV_OBJ, changeOpts } from '../../../common/js'
import { noop } from '@mpxjs/utils'

function showActionSheet (options = {}) {
const opts = changeOpts(options, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ENV_OBJ, noop } from '../../../common/js'
import { ENV_OBJ } from '../../../common/js'
import { noop } from '@mpxjs/utils'

function createSelectorQuery (options = {}) {
const selectorQuery = ENV_OBJ.createSelectorQuery(options)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ENV_OBJ, changeOpts, error, noop } from '../../../common/js'
import { ENV_OBJ, changeOpts, error } from '../../../common/js'
import { noop } from '@mpxjs/utils'

const TIPS_NAME = '支付宝环境 mpx'

Expand Down
3 changes: 2 additions & 1 deletion packages/api-proxy/src/platform/api/socket/SocketTask.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { webHandleSuccess, webHandleFail, type } from '../../../common/js'
import { webHandleSuccess, webHandleFail } from '../../../common/js'
import { type } from '@mpxjs/utils'

const socketTasks = new Set()

Expand Down
4 changes: 3 additions & 1 deletion packages/api-proxy/src/platform/api/storage/index.web.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { webHandleSuccess, webHandleFail, hasOwn, isBrowser, throwSSRWarning } from '../../../common/js'

import { webHandleSuccess, webHandleFail, isBrowser, throwSSRWarning } from '../../../common/js'
import { hasOwn } from '@mpxjs/utils'

function setStorage (options = {}) {
if (!isBrowser) {
Expand Down
3 changes: 2 additions & 1 deletion packages/api-proxy/src/platform/api/storage/rnStorage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import { envError, webHandleSuccess, webHandleFail, hasOwn, defineUnsupportedProps } from '../../../common/js'
import { hasOwn } from '@mpxjs/utils'
import { envError, webHandleSuccess, webHandleFail, defineUnsupportedProps } from '../../../common/js'
function setStorage (options) {
const { key, data, success, fail, complete } = options
let obj = {}
Expand Down
8 changes: 6 additions & 2 deletions packages/api-proxy/src/platform/api/system/index.ali.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ENV_OBJ, changeOpts, envError, handleSuccess } from '../../../common/js'
import { ENV_OBJ, changeOpts, envError, handleSuccess, defineUnsupportedProps } from '../../../common/js'

function getSystemInfo (options = {}) {
const opts = changeOpts(options)
Expand Down Expand Up @@ -32,7 +32,11 @@ function getSystemInfoSync () {
return res
}

const getDeviceInfo = ENV_OBJ.getDeviceInfo || envError('getDeviceInfo')
const getDeviceInfo = function () {
const res = ENV_OBJ.getDeviceBaseInfo
defineUnsupportedProps(res, ['deviceAbi', 'benchmarkLevel', 'cpuType'])
return res
}

const getWindowInfo = ENV_OBJ.getWindowInfo || envError('getWindowInfo')

Expand Down
3 changes: 2 additions & 1 deletion packages/api-proxy/src/platform/api/tab-bar/index.web.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { webHandleSuccess, webHandleFail, hasOwn } from '../../../common/js'
import { webHandleSuccess, webHandleFail } from '../../../common/js'
import { hasOwn } from '@mpxjs/utils'
import Vue from 'vue'

function setTabBarStyle (options = {}) {
Expand Down
1 change: 0 additions & 1 deletion packages/api-proxy/src/platform/api/toast/Toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export default class Toast {
this.title.textContent = opts.title || ''

this.toast.classList.add('show')

opts.duration >= 0 && this.hide({ duration: opts.duration }, type)

const errMsg = type === 'loading' ? 'showLoading:ok' : 'showToast:ok'
Expand Down

0 comments on commit 5599509

Please sign in to comment.